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:
parent
9f586e915f
commit
51b1ade3bc
9 changed files with 22 additions and 50 deletions
|
|
@ -39,7 +39,6 @@ app.views.Publisher = Backbone.View.extend({
|
||||||
$(app.publisher.el).trigger('ajax:success');
|
$(app.publisher.el).trigger('ajax:success');
|
||||||
}
|
}
|
||||||
if(app.stream) {
|
if(app.stream) {
|
||||||
statusMessage.set({"user_participation": new app.models.Participation});
|
|
||||||
app.stream.items.add(statusMessage.toJSON());
|
app.stream.items.add(statusMessage.toJSON());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -127,7 +127,7 @@ class PeopleController < ApplicationController
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ class StreamsController < ApplicationController
|
||||||
respond_with do |format|
|
respond_with do |format|
|
||||||
format.html { render 'layouts/main_stream' }
|
format.html { render 'layouts/main_stream' }
|
||||||
format.mobile { 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,7 @@ class Post < ActiveRecord::Base
|
||||||
|
|
||||||
has_many :participations, :dependent => :delete_all, :as => :target
|
has_many :participations, :dependent => :delete_all, :as => :target
|
||||||
|
|
||||||
attr_accessor :user_like,
|
attr_accessor :user_like
|
||||||
:user_participation
|
|
||||||
|
|
||||||
xml_attr :provider_display_name
|
xml_attr :provider_display_name
|
||||||
|
|
||||||
|
|
|
||||||
9
app/presenters/last_three_comments_decorator.rb
Normal file
9
app/presenters/last_three_comments_decorator.rb
Normal 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
|
||||||
|
|
@ -27,9 +27,8 @@ class PostPresenter
|
||||||
:post_type => @post.post_type,
|
:post_type => @post.post_type,
|
||||||
:image_url => @post.image_url,
|
:image_url => @post.image_url,
|
||||||
:object_url => @post.object_url,
|
:object_url => @post.object_url,
|
||||||
:nsfw => @post.nsfw,
|
|
||||||
:favorite => @post.favorite,
|
:favorite => @post.favorite,
|
||||||
:last_three_comments => CommentPresenter.as_collection(@post.last_three_comments),
|
:nsfw => @post.nsfw,
|
||||||
:author => @post.author.as_api_response(:backbone),
|
:author => @post.author.as_api_response(:backbone),
|
||||||
:o_embed_cache => @post.o_embed_cache.try(: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),
|
:mentioned_people => @post.mentioned_people.as_api_response(:backbone),
|
||||||
|
|
@ -40,8 +39,7 @@ class PostPresenter
|
||||||
:next_post => next_post_path,
|
:next_post => next_post_path,
|
||||||
:previous_post => previous_post_path,
|
:previous_post => previous_post_path,
|
||||||
:user_like => user_like,
|
:user_like => user_like,
|
||||||
:user_participation => user_participation,
|
:user_reshare => user_reshare
|
||||||
:user_reshare => user_reshare,
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -53,18 +51,6 @@ class PostPresenter
|
||||||
Rails.application.routes.url_helpers.previous_post_path(@post)
|
Rails.application.routes.url_helpers.previous_post_path(@post)
|
||||||
end
|
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
|
def title
|
||||||
@post.text.present? ? @post.text(:plain_text => true) : I18n.translate('posts.presenter.title', :name => @post.author.name)
|
@post.text.present? ? @post.text(:plain_text => true) : I18n.translate('posts.presenter.title', :name => @post.author.name)
|
||||||
end
|
end
|
||||||
|
|
@ -77,6 +63,14 @@ class PostPresenter
|
||||||
PostPresenter.new(@post.root, current_user).as_json if @post.respond_to?(:root)
|
PostPresenter.new(@post.root, current_user).as_json if @post.respond_to?(:root)
|
||||||
end
|
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
|
protected
|
||||||
|
|
||||||
def person
|
def person
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,6 @@ class Stream::Base
|
||||||
def stream_posts
|
def stream_posts
|
||||||
self.posts.for_a_stream(max_time, order, self.user).tap do |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.
|
like_posts_for_stream!(posts) #some sql person could probably do this with joins.
|
||||||
participation_posts_for_stream!(posts)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -113,22 +112,6 @@ class Stream::Base
|
||||||
end
|
end
|
||||||
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]
|
# @return [Hash]
|
||||||
def publisher_opts
|
def publisher_opts
|
||||||
{}
|
{}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ describe Stream::Base do
|
||||||
posts = mock
|
posts = mock
|
||||||
@stream.stub(:posts).and_return(posts)
|
@stream.stub(:posts).and_return(posts)
|
||||||
@stream.stub(:like_posts_for_stream!)
|
@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)
|
posts.should_receive(:for_a_stream).with(anything, anything, alice).and_return(posts)
|
||||||
@stream.stream_posts
|
@stream.stream_posts
|
||||||
|
|
|
||||||
|
|
@ -43,17 +43,6 @@ describe PostPresenter do
|
||||||
end
|
end
|
||||||
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
|
describe '#next_post_path' do
|
||||||
it 'returns a string of the users next post' 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"
|
@presenter.next_post_path.should == "#{Rails.application.routes.url_helpers.post_path(@sm)}/next"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue