Add new scopes for the Post model
This commit is contained in:
parent
c63493b0d1
commit
2c3f116326
2 changed files with 55 additions and 0 deletions
|
|
@ -54,6 +54,17 @@ class Post < ActiveRecord::Base
|
||||||
joins(:likes).where(:likes => {:author_id => person.id})
|
joins(:likes).where(:likes => {:author_id => person.id})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scope :subscribed_by, ->(user) {
|
||||||
|
joins(:participations).where(participations: {author_id: user.person_id})
|
||||||
|
}
|
||||||
|
|
||||||
|
scope :reshares, -> { where(type: "Reshare") }
|
||||||
|
|
||||||
|
scope :reshared_by, ->(person) {
|
||||||
|
# we join on the same table, Rails renames "posts" to "reshares_posts" for the right table
|
||||||
|
joins(:reshares).where(reshares_posts: {author_id: person.id})
|
||||||
|
}
|
||||||
|
|
||||||
def post_type
|
def post_type
|
||||||
self.class.name
|
self.class.name
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -174,6 +174,50 @@ describe Post, :type => :model do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".subscribed_by" do
|
||||||
|
let(:user) { FactoryGirl.create(:user) }
|
||||||
|
|
||||||
|
context "when the user has a participation on a post" do
|
||||||
|
let(:post) { FactoryGirl.create(:status_message_with_participations, participants: [user]) }
|
||||||
|
|
||||||
|
it "includes the post to the result set" do
|
||||||
|
expect(Post.subscribed_by(user)).to eq([post])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the user doens't have a participation on a post" do
|
||||||
|
before do
|
||||||
|
FactoryGirl.create(:status_message)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns empty result set" do
|
||||||
|
expect(Post.subscribed_by(user)).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".reshared_by" do
|
||||||
|
let(:person) { FactoryGirl.create(:person) }
|
||||||
|
|
||||||
|
context "when the person has a reshare for a post" do
|
||||||
|
let(:post) { FactoryGirl.create(:reshare, author: person).root }
|
||||||
|
|
||||||
|
it "includes the post to the result set" do
|
||||||
|
expect(Post.reshared_by(person)).to eq([post])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when the person has no reshare for a post" do
|
||||||
|
before do
|
||||||
|
FactoryGirl.create(:status_message)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns empty result set" do
|
||||||
|
expect(Post.reshared_by(person)).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'validations' do
|
describe 'validations' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue