DC DG; added LastThreeComments decorator so we don't have to load those comments when not needed (i.e. new profile pages)

This commit is contained in:
Dennis Collinson 2012-05-10 13:52:49 -07:00
parent 9f586e915f
commit 51b1ade3bc
9 changed files with 22 additions and 50 deletions

View file

@ -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());
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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