adds scopes for every actionable class to activity
This commit is contained in:
@@ -7,6 +7,12 @@ class Activity < ActiveRecord::Base
|
||||
|
||||
validates :action, inclusion: {in: VALID_ACTIONS}
|
||||
|
||||
scope :on_proposals, -> { where(actionable_type: 'Proposal') }
|
||||
scope :on_debates, -> { where(actionable_type: 'Debate') }
|
||||
scope :on_users, -> { where(actionable_type: 'User') }
|
||||
scope :on_comments, -> { where(actionable_type: 'Comment') }
|
||||
scope :for_render, -> { includes(user: [:moderator, :administrator]).includes(:actionable) }
|
||||
|
||||
def self.log(user, action, actionable)
|
||||
create(user: user, action: action.to_s, actionable: actionable)
|
||||
end
|
||||
|
||||
@@ -66,4 +66,23 @@ describe Activity do
|
||||
end
|
||||
end
|
||||
|
||||
describe "scopes by actionable" do
|
||||
it "should filter by actionable type" do
|
||||
on_proposal = create(:activity, actionable: create(:proposal))
|
||||
on_debate = create(:activity, actionable: create(:debate))
|
||||
on_comment = create(:activity, actionable: create(:comment))
|
||||
on_user = create(:activity, actionable: create(:user))
|
||||
|
||||
expect(Activity.on_proposals.size).to eq 1
|
||||
expect(Activity.on_debates.size).to eq 1
|
||||
expect(Activity.on_comments.size).to eq 1
|
||||
expect(Activity.on_users.size).to eq 1
|
||||
|
||||
expect(Activity.on_proposals.first).to eq on_proposal
|
||||
expect(Activity.on_debates.first).to eq on_debate
|
||||
expect(Activity.on_comments.first).to eq on_comment
|
||||
expect(Activity.on_users.first).to eq on_user
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user