some basic tests for post presenter; added user.reshare!

This commit is contained in:
Maxwell Salzberg 2012-02-21 21:15:53 -08:00
parent 3ec7755c83
commit 7755c254fb
3 changed files with 107 additions and 14 deletions

View file

@ -1,6 +1,6 @@
module User::SocialActions module User::SocialActions
def comment!(target, text, opts={}) def comment!(target, text, opts={})
participations.where(:target_id => target).first || participate!(target) find_or_create_participation!(target)
Comment::Generator.new(self, target, text).create!(opts) Comment::Generator.new(self, target, text).create!(opts)
end end
@ -9,11 +9,23 @@ module User::SocialActions
end end
def like!(target, opts={}) def like!(target, opts={})
participations.where(:target_id => target).first || participate!(target) find_or_create_participation!(target)
Like::Generator.new(self, target).create!(opts) Like::Generator.new(self, target).create!(opts)
end end
def reshare!(target, opts={})
find_or_create_participation!(target)
reshare = build_post(:reshare, :root_guid => target.guid)
reshare.save!
Postzord::Dispatcher.defer_build_and_post(self, reshare)
reshare
end
def build_comment(options={}) def build_comment(options={})
Comment::Generator.new(self, options.delete(:post), options.delete(:text)).build(options) Comment::Generator.new(self, options.delete(:post), options.delete(:text)).build(options)
end end
def find_or_create_participation!(target)
participations.where(:target_id => target).first || participate!(target)
end
end end

View file

@ -18,40 +18,39 @@ class PostPresenter
:participations_count => self.post.participations.count, :participations_count => self.post.participations.count,
:reshares_count => self.post.reshares.count, :reshares_count => self.post.reshares.count,
:user_reshare => self.user_reshare, :user_reshare => self.user_reshare,
:next_post => self.next_post_url, :next_post => self.next_post_path,
:previous_post => self.previous_post_url :previous_post => self.previous_post_path
}), }),
:templateName => TemplatePicker.new(self.post).template_name :templateName => template_name
} }
end end
def user_like def user_like
return unless self.current_user.present? return unless user_signed_in?
if like = Like.where(:target_id => self.post.id, :target_type => "Post", :author_id => current_user.person.id).first if like = post.likes.where(:author_id => person.id).first
like.as_api_response(:backbone) like.as_api_response(:backbone)
end end
end end
def user_participation def user_participation
return unless self.current_user.present? return unless user_signed_in?
if participation = post.participations.where(:author_id => person.id).first
if participation = Participation.where(:target_id => self.post.id, :target_type => "Post", :author_id => current_user.person.id).first
participation.as_api_response(:backbone) participation.as_api_response(:backbone)
end end
end end
def user_reshare def user_reshare
return unless self.current_user.present? return unless user_signed_in?
Reshare.where(:root_guid => self.post.guid, :author_id => current_user.person.id).exists? self.post.reshares.where(:author_id => person.id).first
end end
def next_post_url def next_post_path
if n = next_post if n = next_post
Rails.application.routes.url_helpers.post_path(n) Rails.application.routes.url_helpers.post_path(n)
end end
end end
def previous_post_url def previous_post_path
if p = previous_post if p = previous_post
Rails.application.routes.url_helpers.post_path(p) Rails.application.routes.url_helpers.post_path(p)
end end
@ -65,6 +64,10 @@ class PostPresenter
post_base.previous(post) post_base.previous(post)
end end
def template_name
@template_name ||= TemplatePicker.new(post).template_name
end
protected protected
def post_base def post_base
@ -74,4 +77,12 @@ class PostPresenter
self.post.author.posts.all_public self.post.author.posts.all_public
end end
end end
def person
self.current_user.person
end
def user_signed_in?
current_user.present?
end
end end

View file

@ -0,0 +1,70 @@
require 'spec_helper'
describe PostPresenter do
before do
@sm = Factory(:status_message, :public => true)
@presenter = PostPresenter.new(@sm, bob)
@unauthenticated_presenter = PostPresenter.new(@sm)
end
it 'takes a post and an optional user' do
@presenter.should_not be_nil
end
describe '#to_json' do
it 'works with a user' do
@presenter.to_json.should be_a Hash
end
it 'works without a user' do
@unauthenticated_presenter.to_json.should be_a Hash
end
end
describe '#user_like' do
it 'includes the users like' do
bob.like!(@sm)
@presenter.user_like.should be_present
end
it 'is nil if the user is not authenticated' do
@unauthenticated_presenter.user_like.should be_nil
end
end
describe '#user_reshare' do
it 'includes the users reshare' do
bob.reshare!(@sm)
@presenter.user_reshare.should be_present
end
it 'is nil if the user is not authenticated' do
@unauthenticated_presenter.user_reshare.should be_nil
end
end
describe '#user_participation' do
it 'includes the users participation' do
bob.participate!(@sm)
@presenter.user_participation.should be_present
end
it 'is nil if the user is not authenticated' do
@unauthenticated_presenter.user_participation.should be_nil
end
end
describe '#next_post_path' do
it 'returns a string of the users next post' do
@presenter.should_receive(:next_post).and_return(@sm)
@presenter.next_post_path.should == Rails.application.routes.url_helpers.post_path(@sm)
end
end
describe '#previous_post_path' do
it 'returns a string of the users next post' do
@presenter.should_receive(:previous_post).and_return(@sm)
@presenter.previous_post_path.should == Rails.application.routes.url_helpers.post_path(@sm)
end
end
end