From 2c3f1163265c0dc19dcf83d13b485f6835c987e0 Mon Sep 17 00:00:00 2001 From: cmrd Senya Date: Wed, 9 Aug 2017 16:49:39 +0300 Subject: [PATCH] Add new scopes for the Post model --- app/models/post.rb | 11 ++++++++++ spec/models/post_spec.rb | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index 576717f79..58dbf12cf 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index fd7b44274..550fdc74d 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -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