Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #46 from Safing/feature/activities
Browse files Browse the repository at this point in the history
Activities
  • Loading branch information
davegson authored Jan 31, 2019
2 parents 2ff1e46 + 86ef5b9 commit ae65deb
Show file tree
Hide file tree
Showing 52 changed files with 1,018 additions and 212 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ gem 'redcarpet'

gem 'bootsnap', require: false

# WARNING: when updating public_activity
# assert our patch works: models/concerns/public_activity
# and the specs: specs/models/concerns/public_activity
gem 'devise'
gem 'public_activity'
gem 'pundit'
gem 'state_machines-activerecord'

Expand Down Expand Up @@ -55,5 +59,6 @@ group :test do
gem 'capybara'
gem 'launchy'
gem 'selenium-webdriver'
gem 'fakeredis'
# check if the release is out yet https://github.com/thoughtbot/shoulda-matchers/milestone/13
end
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ GEM
railties (>= 3.0.0)
faker (1.9.1)
i18n (>= 0.7)
fakeredis (0.7.0)
redis (>= 3.2, < 5.0)
ffi (1.9.25)
fix-db-schema-conflicts (3.0.2)
rubocop (>= 0.38.0)
Expand Down Expand Up @@ -211,6 +213,11 @@ GEM
pry-byebug (3.6.0)
byebug (~> 10.0)
pry (~> 0.10)
public_activity (1.6.3)
actionpack (>= 3.0.0)
activerecord (>= 3.0)
i18n (>= 0.5.0)
railties (>= 3.0.0)
public_suffix (3.0.3)
pundit (2.0.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -364,6 +371,7 @@ DEPENDENCIES
devise
factory_bot_rails
faker
fakeredis
fix-db-schema-conflicts
font_awesome5_rails
grape
Expand All @@ -378,6 +386,7 @@ DEPENDENCIES
letter_opener
pg
pry-byebug
public_activity
pundit
pundit-matchers
rails (~> 5.2.1)
Expand Down
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
guard :rspec,
results_file: File.expand_path('tmp/rspec_guard_result'),
cmd: 'bundle exec rspec -fd',
cmd: 'bundle exec rspec -fd --exclude-pattern "**/features/**/*_spec.rb"',
spec_paths: ['spec'] do
watch('spec/spec_helper.rb') { 'spec' }

Expand Down
4 changes: 3 additions & 1 deletion app/controllers/apps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ def new

def create
@app = App.new(app_params)
@app.user = current_user
authorize(@app)

if @app.save
@app.create_activity :create, owner: current_user
redirect_to(app_path(@app.id), flash: { success: 'App created successfully' })
else
render 'new'
end
end

# NOTE: create activity when adding #edit #update actions

def index
@apps = App.all
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def create
@comment.user = current_user

if @comment.save
@comment.create_activity :create, owner: current_user, recipient: @commentable
redirect_to(@commentable, flash: { success: 'Comment created' })
else
instance_variable_set "@#{@resource.singularize}".to_sym, @commentable
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/domains_controller.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
class DomainsController < ApplicationController
def show
@domain = Domain.find_by(name: params[:name])
authorize(@domain)
end

def new
@domain = Domain.new
authorize(@domain)
end

# TODO: refactor this
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def create
@domain = Domain.new(domain_params)
authorize(@domain)

if @domain.valid?
@domain = Domain.find_or_initialize_by(name: domain_params[:name])

if @domain.new_record?
if @domain.url_exists?
@domain.user = current_user
@domain.save!
@domain.create_activity :create, owner: current_user

@state = 'success'
@link = domain_path(@domain.name)
Expand Down Expand Up @@ -52,8 +55,6 @@ def index
# rubocop:enable Metrics/AbcSize

def domain_params
params.require(:domain)
.permit(:name)
.merge(user: current_user)
params.require(:domain).permit(:name)
end
end
1 change: 1 addition & 0 deletions app/controllers/stamps/flags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def create
authorize @stamp

if @stamp.save
@stamp.create_activity key: @stamp.key_for(action: :create), owner: current_user, recipient: @stamp.stampable
redirect_to(flag_stamp_path(@stamp.id), flash: { success: 'Stamp created successfully' })
else
render 'new'
Expand Down
1 change: 1 addition & 0 deletions app/controllers/stamps/labels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def create
authorize @stamp

if @stamp.save
@stamp.create_activity key: @stamp.key_for(action: :create), owner: current_user, recipient: @stamp.stampable
redirect_to(label_stamp_path(@stamp.id), flash: { success: 'Stamp created successfully' })
else
load_labels
Expand Down
1 change: 1 addition & 0 deletions app/controllers/votes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def create
@vote.power = current_user.voting_power

if @vote.save
@vote.create_activity :create, owner: current_user, recipient: @votable
redirect_to(@votable, flash: { success: 'Successfully voted ' })
else
redirect_to(@votable, flash: { error: 'Cannot vote twice' })
Expand Down
5 changes: 3 additions & 2 deletions app/models/app.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class App < ApplicationRecord
include PublicActivity::Common

# needed to load uuid that is generated by postgres
# https://github.com/rails/rails/issues/21627#issuecomment-142625429
# https://www.devmynd.com/blog/db-generated-values-and-activerecord/
Expand All @@ -9,15 +11,14 @@ class App < ApplicationRecord
h[os] = [:boolean, default: false]
end)

belongs_to :user
has_many :stamps, as: :stampable

# Strip https:// or http://
before_validation(on: :create) do
self.link = link.gsub(%r{https?:\/\/}, '') if attribute_present?('link')
end

validates_presence_of %i[description link name user]
validates_presence_of %i[description link name]
# db will insert a default value
validates :uuid, presence: true, unless: proc { |obj| obj.new_record? }
validate :supports_one_or_more_operating_systems
Expand Down
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Comment < ApplicationRecord
include PublicActivity::Common

belongs_to :commentable, polymorphic: true
belongs_to :user

Expand Down
45 changes: 45 additions & 0 deletions app/models/concerns/public_activity/common_with_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module PublicActivity
module CommonWithSystem
# WARNING: when updating PublicActivity assert this still works!

extend ActiveSupport::Concern

included do
include PublicActivity::Common

def create_system_activity(*args)
@system_activity = true
create_activity(*args)
end

private

# called by #create_activity
# github.com/chaps-io/public_activity/blob/1-6-stable/lib/public_activity/common.rb#L252
# we set or overwrite the settings with our fixed #system_options
# this way we assure an Activity with owner_id: -1 & owner_type: 'System' is created

def prepare_settings(*args)
if @system_activity
# reset so a normal activity can be created by this instance
@system_activity = nil

super(*args).merge(system_options)
else
super(*args)
end
end

def system_options
{
owner_id: -1,
owner_type: 'System'
}
end

def system_activity
@system_activity ||= false
end
end
end
end
22 changes: 22 additions & 0 deletions app/models/concerns/user/relations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class User < ApplicationRecord
module Relations
extend ActiveSupport::Concern

included do
has_one :api_key

has_many :comments
has_many :domains, foreign_key: :user_id
has_many :stamps, foreign_key: :user_id
has_many :votes

def activities
PublicActivity::Activity.where(owner_id: self.id)
end

def domains
Domain.where(id: activities.where(key: 'domain.create').select(:trackable_id))
end
end
end
end
9 changes: 7 additions & 2 deletions app/models/concerns/votable/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ module State
before_transition in_progress: :accepted do |votable, _|
votable.archive_accepted_siblings!
end

before_transition do |votable, transition|
votable.create_system_activity(
key: votable.key_for(action: transition.to_name),
recipient: votable.stampable
)
end
end
end

Expand Down Expand Up @@ -50,8 +57,6 @@ def scheduled_job
end.first
end

private

def archive_accepted_siblings!
siblings.accepted.each(&:archive!)
end
Expand Down
5 changes: 3 additions & 2 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
class Domain < ApplicationRecord
include PublicActivity::Common

# rubocop:disable LineLength
# got the regex from https://stackoverflow.com/a/26987741/2235594
NAME_REGEX = /(((?!\-))(xn\-\-)?[a-z0-9\-_]{0,61}[a-z0-9]{1,1}\.)*(xn\-\-)?([a-z0-9\-]{1,61}|[a-z0-9\-]{1,30})\.[a-z]{2,}/.freeze
NAME_REGEX_WITH_ANCHORS = /\A(((?!\-))(xn\-\-)?[a-z0-9\-_]{0,61}[a-z0-9]{1,1}\.)*(xn\-\-)?([a-z0-9\-]{1,61}|[a-z0-9\-]{1,30})\.[a-z]{2,}\z/.freeze

# rubocop:enable LineLength

belongs_to :user
belongs_to :parent, class_name: 'Domain', optional: true
has_many :children, class_name: 'Domain', foreign_key: 'parent_id'
has_many :stamps, as: :stampable

validates :name, format: { with: NAME_REGEX_WITH_ANCHORS, message: 'is not a valid domain name' }
validates_presence_of %i[name user]
validates_presence_of :name

def parent_name
parent.name if parent_id.present?
Expand Down
10 changes: 10 additions & 0 deletions app/models/stamp.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Stamp < ApplicationRecord
include PublicActivity::CommonWithSystem

TYPES = %w[Stamp::Flag Stamp::Label Stamp::Identifier].freeze

include Votable
Expand Down Expand Up @@ -51,6 +53,14 @@ def sti_name
to_s
end

def param_key(base_class: true)
(base_class ? self.class.base_class : self.class).model_name.param_key
end

def key_for(base_class: true, action:)
[param_key(base_class: base_class), action].join('.')
end

class << self
# ActiveModel::Naming.model_name has provides Model helpers, such as #route_key, #param_key...
# https://github.com/rails/rails/blob/master/activemodel/lib/active_model/naming.rb
Expand Down
10 changes: 10 additions & 0 deletions app/models/system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Used for system activites. It basically enables PublicActivity to create a system activity
# so instead of: {owner_type: User, owner_id: 5}
# it creates: {owner_type: System, owner_id: -1}
# enables: System.new.activities
# => returns all system activities
class System < User
def id
-1
end
end
8 changes: 1 addition & 7 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
class User < ApplicationRecord
include Roles

has_one :api_key

has_many :comments
has_many :domains, foreign_key: :user_id
has_many :stamps, foreign_key: :user_id
has_many :votes
include Relations

validates_presence_of %i[role username]

Expand Down
2 changes: 2 additions & 0 deletions app/models/vote.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Vote < ApplicationRecord
include PublicActivity::Common

before_validation :cache_users_voting_power, on: :create

belongs_to :user
Expand Down
6 changes: 0 additions & 6 deletions app/presenters/domain_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ class Entity < Grape::Entity
required: true
}

expose :user_id, documentation: {
type: 'Integer',
desc: 'User id of creator',
required: true
}

with_options(expose_nil: false) do
expose :parent_id, documentation: {
type: 'Integer',
Expand Down
4 changes: 0 additions & 4 deletions app/views/domains/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
Domain
%th
Stamps
%th
User
%th
Link
%tbody
Expand All @@ -23,8 +21,6 @@
= domain.name
%td
= domain.stamps.count
%td
= domain.user.username
%td
= link_to domain.href do
%i.purple.external.icon
5 changes: 0 additions & 5 deletions app/views/domains/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@
%div.five.wide.right.floated.column
%div.ui.basic.segment
%div.ui.relaxed.list
%div.item
%i.user.icon
%div.content
= link_to user_path(@domain.user_id) do
= @domain.user.username
%div.item
%i.clock.outline.icon
%div.content
Expand Down
Loading

0 comments on commit ae65deb

Please sign in to comment.