diff --git a/app/models/post.rb b/app/models/post.rb index 00a253447..109943797 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -53,10 +53,6 @@ class Post < ActiveRecord::Base new_post end - def reshare_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). def mutable? false diff --git a/app/models/reshare.rb b/app/models/reshare.rb index 15a1dc7db..7d945ff62 100644 --- a/app/models/reshare.rb +++ b/app/models/reshare.rb @@ -17,6 +17,14 @@ class Reshare < Post self.public = true end + after_create do + self.root.update_reshares_counter + end + + after_destroy do + self.root.update_reshares_counter + end + def root_diaspora_id self.root.author.diaspora_handle end @@ -46,7 +54,7 @@ class Reshare < Post return if Post.exists?(:guid => self.root_guid) fetched_post = self.class.fetch_post(root_author, self.root_guid) - + if fetched_post #Why are we checking for this? if root_author.diaspora_handle != fetched_post.diaspora_handle diff --git a/app/views/reshares/_reshare.haml b/app/views/reshares/_reshare.haml index 4ccee89f1..c61948502 100644 --- a/app/views/reshares/_reshare.haml +++ b/app/views/reshares/_reshare.haml @@ -16,7 +16,7 @@ %span.timeago = link_to(how_long_ago(post), post_path(post)) – - = t("reshares.reshare.reshare", :count => post.reshare_count) + = t("reshares.reshare.reshare", :count => post.reshares_count) - if post.activity_streams? = link_to image_tag(post.image_url, 'data-small-photo' => post.image_url, 'data-full-photo' => post.image_url, :class => 'stream-photo'), post.object_url, :class => "stream-photo-link" diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml index 638a09fbf..d76699d6e 100644 --- a/app/views/shared/_stream_element.html.haml +++ b/app/views/shared/_stream_element.html.haml @@ -32,7 +32,7 @@ – %span.timeago = link_to(how_long_ago(post), post_path(post)) - - if post.reshare_count > 0 + - if post.reshares_count > 0 – %span.num_reshares = t("reshares.reshare.reshare", :count => post.reshares.size) diff --git a/db/migrate/20111111025358_counter_cache_on_post_reshares.rb b/db/migrate/20111111025358_counter_cache_on_post_reshares.rb new file mode 100644 index 000000000..cc2384d49 --- /dev/null +++ b/db/migrate/20111111025358_counter_cache_on_post_reshares.rb @@ -0,0 +1,22 @@ +class CounterCacheOnPostReshares < ActiveRecord::Migration + class Post < ActiveRecord::Base; end + + def self.up + add_column :posts, :reshares_count, :integer, :default => 0 + + execute %{ + UPDATE posts + SET reshares_count = ( + SELECT COUNT(*) + FROM posts p2 + WHERE + p2.type = 'Reshare' + AND p2.root_guid = posts.guid + ) + } + end + + def self.down + remove_column :posts, :reshares_count + end +end diff --git a/db/schema.rb b/db/schema.rb index 4a3992302..cb1257af1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20111101202137) do +ActiveRecord::Schema.define(:version => 20111111025358) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -307,6 +307,7 @@ ActiveRecord::Schema.define(:version => 20111101202137) do t.integer "likes_count", :default => 0 t.integer "comments_count", :default => 0 t.integer "o_embed_cache_id" + t.integer "reshares_count", :default => 0 end add_index "posts", ["author_id", "root_guid"], :name => "index_posts_on_author_id_and_root_guid", :unique => true diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index 1ba5746ef..541cc882c 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -107,6 +107,11 @@ module Diaspora end end + # @return [Integer] + def update_reshares_counter + self.class.where(:id => self.id). + update_all(:reshares_count => self.reshares.count) + end protected diff --git a/spec/helpers/reshares_helper_spec.rb b/spec/helpers/reshares_helper_spec.rb index 9f145495e..5907fcca4 100644 --- a/spec/helpers/reshares_helper_spec.rb +++ b/spec/helpers/reshares_helper_spec.rb @@ -22,7 +22,7 @@ describe ResharesHelper do describe 'which has not been reshared' do before :each do - @post.reshare_count.should == 0 + @post.reshares_count.should == 0 end it 'has "Reshare" as the English text' do @@ -33,7 +33,8 @@ describe ResharesHelper do describe 'which has been reshared' do before :each do @reshare = Factory.create(:reshare, :root => @post) - @post.reshare_count.should == 1 + @post.reload + @post.reshares_count.should == 1 end it 'has "Reshare" as the English text' do diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index dd872466b..aaf881e1d 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -379,7 +379,7 @@ describe Post do end end - describe '#reshare_count' do + describe '#reshares_count' do before :each do @post = @user.post :status_message, :text => "hello", :to => @aspect.id, :public => true @post.reshares.size.should == 0 @@ -387,7 +387,7 @@ describe Post do describe 'when post has not been reshared' do it 'returns zero' do - @post.reshare_count.should == 0 + @post.reshares_count.should == 0 end end @@ -400,7 +400,7 @@ describe Post do end it 'returns 1' do - @post.reshare_count.should == 1 + @post.reshares_count.should == 1 end end @@ -415,7 +415,7 @@ describe Post do end it 'returns the number of reshares' do - @post.reshare_count.should == 3 + @post.reshares_count.should == 3 end end end