Add new scopes for the Post model

This commit is contained in:
cmrd Senya 2017-08-09 16:49:39 +03:00
parent c63493b0d1
commit 2c3f116326
No known key found for this signature in database
GPG key ID: 5FCC5BA680E67BFE
2 changed files with 55 additions and 0 deletions

View file

@ -54,6 +54,17 @@ class Post < ActiveRecord::Base
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
self.class.name
end

View file

@ -174,6 +174,50 @@ describe Post, :type => :model do
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
describe 'validations' do