Added a counter cache for the number of reshares of a post.

This commit is contained in:
Pistos 2011-11-10 23:47:40 -05:00
parent e951fa6a9c
commit f83e56e5f3
9 changed files with 47 additions and 14 deletions

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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