merge Pistos's reshare branch into master

This commit is contained in:
danielgrippi 2011-12-20 14:40:19 -08:00
commit 0f42c98a7b
11 changed files with 139 additions and 22 deletions

View file

@ -25,8 +25,7 @@ module ResharesHelper
:remote => true,
:confirm => t('reshares.reshare.reshare_confirmation', :author => post.root.author.name)
else
link_to t("reshares.reshare.reshare",
:count => post.reshares.size),
link_to t('shared.reshare.reshare'),
reshares_path(:root_guid => post.guid),
:method => :post,
:remote => true,

View file

@ -53,10 +53,6 @@ class Post < ActiveRecord::Base
new_post
end
def reshare_count
@reshare_count ||= Post.where(: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
@ -36,7 +44,7 @@ class Reshare < Post
def notification_type(user, person)
Notifications::Reshared if root.author == user.person
end
private
def after_parse
@ -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
@ -71,7 +79,7 @@ class Reshare < Post
def root_must_be_public
if self.root && !self.root.public
errors[:base] << "you must reshare public posts"
errors[:base] << "Only posts which are public may be reshared."
return false
end
end

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,6 +32,10 @@
%span.timeago
= link_to(how_long_ago(post), post_path(post))
- if post.reshares_count > 0
%span.num_reshares
= t("reshares.reshare.reshare", :count => post.reshares.size)
.post-content
= nsfw_sheild(post)

View file

@ -0,0 +1,35 @@
class CounterCacheOnPostReshares < ActiveRecord::Migration
class Post < ActiveRecord::Base; end
def self.up
add_column :posts, :reshares_count, :integer, :default => 0
if postgres?
execute %{
UPDATE posts
SET reshares_count = (
SELECT COUNT(*)
FROM posts p2
WHERE
p2.type = 'Reshare'
AND p2.root_guid = posts.guid
)
}
else # mysql
execute "CREATE TEMPORARY TABLE posts_reshared SELECT * FROM posts WHERE type = 'Reshare'"
execute %{
UPDATE posts p1
SET reshares_count = (
SELECT COUNT(*)
FROM posts_reshared p2
WHERE p2.root_guid = p1.guid
)
}
end
end
def self.down
remove_column :posts, :reshares_count
end
end

View file

@ -314,6 +314,7 @@ ActiveRecord::Schema.define(:version => 20111217042006) 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
t.integer "photos_count", :default => 0
end

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

@ -1,15 +1,5 @@
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the ResharesHelper. For example:
#
# describe ResharesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe ResharesHelper do
include StreamHelper
@ -20,5 +10,41 @@ describe ResharesHelper do
reshare_link(reshare)
}.should_not raise_error
end
describe 'for a typical post' do
before :each do
aspect = alice.aspects.first
@post = alice.build_post :status_message, :text => "ohai", :to => aspect.id, :public => true
@post.save!
alice.add_to_streams(@post, [aspect])
alice.dispatch_post @post, :to => aspect.id
end
describe 'which has not been reshared' do
before :each do
@post.reshares_count.should == 0
end
it 'has "Reshare" as the English text' do
reshare_link(@post).should =~ %r{>Reshare</a>}
end
end
describe 'which has been reshared' do
before :each do
@reshare = Factory.create(:reshare, :root => @post)
@post.reload
@post.reshares_count.should == 1
end
it 'has "Reshare" as the English text' do
reshare_link(@post).should =~ %r{>Reshare</a>}
end
it 'its reshare has "Reshare original" as the English text' do
reshare_link(@reshare).should =~ %r{>Reshare original</a>}
end
end
end
end
end

View file

@ -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 '#reshares_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.reshares_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.reshares_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.reshares_count.should == 3
end
end
end
end

View file

@ -18,7 +18,9 @@ describe Reshare do
end
it 'require public root' do
Factory.build(:reshare, :root => Factory.build(:status_message, :public => false)).should_not be_valid
reshare = Factory.build(:reshare, :root => Factory.build(:status_message, :public => false))
reshare.should_not be_valid
reshare.errors[:base].should include('Only posts which are public may be reshared.')
end
it 'forces public' do