diff --git a/app/assets/javascripts/app/views/publisher_view.js b/app/assets/javascripts/app/views/publisher_view.js index 55bde36a5..24b61ff7f 100644 --- a/app/assets/javascripts/app/views/publisher_view.js +++ b/app/assets/javascripts/app/views/publisher_view.js @@ -39,7 +39,6 @@ app.views.Publisher = Backbone.View.extend({ $(app.publisher.el).trigger('ajax:success'); } if(app.stream) { - statusMessage.set({"user_participation": new app.models.Participation}); app.stream.items.add(statusMessage.toJSON()); } } diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 31edd3505..b1be75ec9 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -127,7 +127,7 @@ class PeopleController < ApplicationController end end - format.json { render :json => PostPresenter.collection_json(@stream.stream_posts, current_user) } + format.json { render :json => @stream.stream_posts.map { |p| LastThreeCommentsDecorator.new(PostPresenter.new(p, current_user)) }} end end diff --git a/app/controllers/streams_controller.rb b/app/controllers/streams_controller.rb index 1f25dee19..31d85f695 100644 --- a/app/controllers/streams_controller.rb +++ b/app/controllers/streams_controller.rb @@ -65,7 +65,7 @@ class StreamsController < ApplicationController respond_with do |format| format.html { render 'layouts/main_stream' } format.mobile { render 'layouts/main_stream' } - format.json { render :json => PostPresenter.collection_json(@stream.stream_posts, current_user) } + format.json { render :json => @stream.stream_posts.map {|p| LastThreeCommentsDecorator.new(PostPresenter.new(p, current_user)) }} end end diff --git a/app/models/post.rb b/app/models/post.rb index 0b1f810f2..1dc27a93d 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -14,8 +14,7 @@ class Post < ActiveRecord::Base has_many :participations, :dependent => :delete_all, :as => :target - attr_accessor :user_like, - :user_participation + attr_accessor :user_like xml_attr :provider_display_name diff --git a/app/presenters/last_three_comments_decorator.rb b/app/presenters/last_three_comments_decorator.rb new file mode 100644 index 000000000..884d0dc49 --- /dev/null +++ b/app/presenters/last_three_comments_decorator.rb @@ -0,0 +1,9 @@ +class LastThreeCommentsDecorator + def initialize(presenter) + @presenter = presenter + end + + def as_json(options={}) + @presenter.as_json.merge({:last_three_comments => CommentPresenter.as_collection(@presenter.post.last_three_comments)}) + end +end \ No newline at end of file diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 84dc074b4..3399228dd 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -27,9 +27,8 @@ class PostPresenter :post_type => @post.post_type, :image_url => @post.image_url, :object_url => @post.object_url, - :nsfw => @post.nsfw, :favorite => @post.favorite, - :last_three_comments => CommentPresenter.as_collection(@post.last_three_comments), + :nsfw => @post.nsfw, :author => @post.author.as_api_response(:backbone), :o_embed_cache => @post.o_embed_cache.try(:as_api_response, :backbone), :mentioned_people => @post.mentioned_people.as_api_response(:backbone), @@ -40,8 +39,7 @@ class PostPresenter :next_post => next_post_path, :previous_post => previous_post_path, :user_like => user_like, - :user_participation => user_participation, - :user_reshare => user_reshare, + :user_reshare => user_reshare } end @@ -53,18 +51,6 @@ class PostPresenter Rails.application.routes.url_helpers.previous_post_path(@post) end - def user_like - @post.like_for(@current_user).try(:as_api_response, :backbone) - end - - def user_participation - @post.participation_for(@current_user).try(:as_api_response, :backbone) - end - - def user_reshare - @post.reshare_for(@current_user) - end - def title @post.text.present? ? @post.text(:plain_text => true) : I18n.translate('posts.presenter.title', :name => @post.author.name) end @@ -77,6 +63,14 @@ class PostPresenter PostPresenter.new(@post.root, current_user).as_json if @post.respond_to?(:root) end + def user_like + @post.like_for(@current_user).try(:as_api_response, :backbone) + end + + def user_reshare + @post.reshare_for(@current_user) + end + protected def person diff --git a/lib/stream/base.rb b/lib/stream/base.rb index 27281581a..b3af708ca 100644 --- a/lib/stream/base.rb +++ b/lib/stream/base.rb @@ -40,7 +40,6 @@ class Stream::Base def stream_posts self.posts.for_a_stream(max_time, order, self.user).tap do |posts| like_posts_for_stream!(posts) #some sql person could probably do this with joins. - participation_posts_for_stream!(posts) end end @@ -113,22 +112,6 @@ class Stream::Base end end - # @return [void] - def participation_posts_for_stream!(posts) - return posts unless @user - - participations = Participation.where(:author_id => @user.person.id, :target_id => posts.map(&:id), :target_type => "Post") - - participation_hash = participations.inject({}) do |hash, participation| - hash[participation.target_id] = participation - hash - end - - posts.each do |post| - post.user_participation = participation_hash[post.id] - end - end - # @return [Hash] def publisher_opts {} diff --git a/spec/lib/stream/base_spec.rb b/spec/lib/stream/base_spec.rb index 2fa37b9c5..29c8e7b12 100644 --- a/spec/lib/stream/base_spec.rb +++ b/spec/lib/stream/base_spec.rb @@ -16,7 +16,6 @@ describe Stream::Base do posts = mock @stream.stub(:posts).and_return(posts) @stream.stub(:like_posts_for_stream!) - @stream.stub(:participation_posts_for_stream!) posts.should_receive(:for_a_stream).with(anything, anything, alice).and_return(posts) @stream.stream_posts diff --git a/spec/presenters/post_presenter_spec.rb b/spec/presenters/post_presenter_spec.rb index bde8b9b6d..2e87f2bb9 100644 --- a/spec/presenters/post_presenter_spec.rb +++ b/spec/presenters/post_presenter_spec.rb @@ -43,17 +43,6 @@ describe PostPresenter do 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.next_post_path.should == "#{Rails.application.routes.url_helpers.post_path(@sm)}/next"