From a604dd2aa6286b0cd0dddf4e4ec41b695990d69e Mon Sep 17 00:00:00 2001 From: Pistos Date: Tue, 4 Oct 2011 12:42:28 -0400 Subject: [PATCH] Refactor implementation of #reshare_count. The old implementation which uses Post.where causes undefined method `abstract_class?' for Object:Class in spec/controllers/posts_controller_spec.rb. Not sure whether that's a problem with Rails, or rspec, or what. --- app/models/post.rb | 2 +- spec/models/post_spec.rb | 43 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index e161f8fca..00a253447 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -54,7 +54,7 @@ class Post < ActiveRecord::Base end def reshare_count - @reshare_count ||= Post.where(:root_guid => self.guid).count + @reshare_count ||= Post.find_all_by_root_guid(self.guid).count end # @return Returns true if this Post will accept updates (i.e. updates to the caption of a photo). diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index fa546987e..dd872466b 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -24,7 +24,7 @@ describe Post do StatusMessage.owned_or_visible_by_user(@you).should include(@post_from_contact) end - it 'returns your posts' do + it 'returns your posts' do StatusMessage.owned_or_visible_by_user(@you).should include(@your_post) end @@ -378,4 +378,45 @@ describe Post do end end end + + describe '#reshare_count' do + before :each do + @post = @user.post :status_message, :text => "hello", :to => @aspect.id, :public => true + @post.reshares.size.should == 0 + end + + describe 'when post has not been reshared' do + it 'returns zero' do + @post.reshare_count.should == 0 + end + end + + describe 'when post has been reshared exactly 1 time' do + before :each do + @post.reshares.size.should == 0 + @reshare = Factory.create(:reshare, :root => @post) + @post.reload + @post.reshares.size.should == 1 + end + + it 'returns 1' do + @post.reshare_count.should == 1 + end + end + + describe 'when post has been reshared more than once' do + before :each do + @post.reshares.size.should == 0 + Factory.create(:reshare, :root => @post) + Factory.create(:reshare, :root => @post) + Factory.create(:reshare, :root => @post) + @post.reload + @post.reshares.size.should == 3 + end + + it 'returns the number of reshares' do + @post.reshare_count.should == 3 + end + end + end end