Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 2c3f1163 rédigé par cmrd Senya's avatar cmrd Senya
Parcourir les fichiers

Add new scopes for the Post model

parent c63493b0
Branches
Étiquettes
Aucune requête de fusion associée trouvée
...@@ -54,6 +54,17 @@ class Post < ActiveRecord::Base ...@@ -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 ...@@ -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
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter