diff --git a/app/models/user/social_actions.rb b/app/models/user/social_actions.rb index 3244b9bad..ca8c8874e 100644 --- a/app/models/user/social_actions.rb +++ b/app/models/user/social_actions.rb @@ -1,6 +1,6 @@ module User::SocialActions 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) end @@ -9,11 +9,23 @@ module User::SocialActions end def like!(target, opts={}) - participations.where(:target_id => target).first || participate!(target) + find_or_create_participation!(target) Like::Generator.new(self, target).create!(opts) 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={}) Comment::Generator.new(self, options.delete(:post), options.delete(:text)).build(options) end + + def find_or_create_participation!(target) + participations.where(:target_id => target).first || participate!(target) + end end \ No newline at end of file diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 722ba5854..f076fd4d3 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -18,40 +18,39 @@ class PostPresenter :participations_count => self.post.participations.count, :reshares_count => self.post.reshares.count, :user_reshare => self.user_reshare, - :next_post => self.next_post_url, - :previous_post => self.previous_post_url + :next_post => self.next_post_path, + :previous_post => self.previous_post_path }), - :templateName => TemplatePicker.new(self.post).template_name + :templateName => template_name } end def user_like - return unless self.current_user.present? - if like = Like.where(:target_id => self.post.id, :target_type => "Post", :author_id => current_user.person.id).first + return unless user_signed_in? + if like = post.likes.where(:author_id => person.id).first like.as_api_response(:backbone) end end def user_participation - return unless self.current_user.present? - - if participation = Participation.where(:target_id => self.post.id, :target_type => "Post", :author_id => current_user.person.id).first + return unless user_signed_in? + if participation = post.participations.where(:author_id => person.id).first participation.as_api_response(:backbone) end end def user_reshare - return unless self.current_user.present? - Reshare.where(:root_guid => self.post.guid, :author_id => current_user.person.id).exists? + return unless user_signed_in? + self.post.reshares.where(:author_id => person.id).first end - def next_post_url + def next_post_path if n = next_post Rails.application.routes.url_helpers.post_path(n) end end - def previous_post_url + def previous_post_path if p = previous_post Rails.application.routes.url_helpers.post_path(p) end @@ -65,6 +64,10 @@ class PostPresenter post_base.previous(post) end + def template_name + @template_name ||= TemplatePicker.new(post).template_name + end + protected def post_base @@ -74,4 +77,12 @@ class PostPresenter self.post.author.posts.all_public end end + + def person + self.current_user.person + end + + def user_signed_in? + current_user.present? + end end diff --git a/spec/presenters/post_presenter_spec.rb b/spec/presenters/post_presenter_spec.rb new file mode 100644 index 000000000..ce2fc9d29 --- /dev/null +++ b/spec/presenters/post_presenter_spec.rb @@ -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 \ No newline at end of file