fix next post and previous post, more efficient

make controller actions
make the presenter reflect that it is instance data ;-p

needs tests lol.
This commit is contained in:
Dennis Collinson 2012-05-04 18:24:25 -07:00
parent 69b9b5768f
commit 0fc399243b
4 changed files with 78 additions and 96 deletions

View file

@ -1,11 +1,6 @@
app.views.PostViewerNav = app.views.Base.extend({
templateName: "post-viewer/nav",
events : {
"click a" : "pjax"
},
postRenderTemplate : function() {
var mappings = {"#forward" : "next_post",
"#back" : "previous_post"};
@ -17,14 +12,5 @@ app.views.PostViewerNav = app.views.Base.extend({
setArrow : function(arrow, loc) {
loc ? arrow.attr('href', loc) : arrow.remove()
},
pjax : function(evt) {
if(evt) { evt.preventDefault(); }
var link;
evt.target.tagName != "A" ? link = $(evt.target).closest("a") : link = $(evt.target)
app.router.navigate(link.attr("href").substring(1), true)
}
});

View file

@ -9,6 +9,7 @@ class PostsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :iframe, :oembed]
before_filter :set_format_if_malformed_from_status_net, :only => :show
before_filter :find_post, :only => [:show, :next, :previous]
layout 'post'
@ -24,28 +25,19 @@ class PostsController < ApplicationController
end
def show
@post = find_by_guid_or_id_with_current_user(params[:id])
return log_and_redirect_back unless @post
# @commenting_disabled = can_not_comment_on_post?
# mark corresponding notification as read
if user_signed_in? && notification = Notification.where(:recipient_id => current_user.id, :target_id => @post.id).first
notification.unread = false
notification.save
end
if @post
# @commenting_disabled = can_not_comment_on_post?
# mark corresponding notification as read
if user_signed_in? && notification = Notification.where(:recipient_id => current_user.id, :target_id => @post.id).first
notification.unread = false
notification.save
end
respond_to do |format|
format.html{ gon.post = postJson; render 'posts/show.html.haml' }
format.xml{ render :xml => @post.to_diaspora_xml }
format.mobile{render 'posts/show.mobile.haml', :layout => "application"}
format.json{ render :json => postJson }
end
else
user_id = (user_signed_in? ? current_user : nil)
Rails.logger.info(":event => :link_to_nonexistent_post, :ref => #{request.env['HTTP_REFERER']}, :user_id => #{user_id}, :post_id => #{params[:id]}")
flash[:error] = I18n.t('posts.show.not_found')
redirect_to :back
respond_to do |format|
format.html{ gon.post = postJson; render 'posts/show.html.haml' }
format.xml{ render :xml => @post.to_diaspora_xml }
format.mobile{render 'posts/show.mobile.haml', :layout => "application"}
format.json{ render :json => postJson }
end
end
@ -88,8 +80,31 @@ class PostsController < ApplicationController
end
end
def next
redirect_to post_path(post_base.newer(@post))
end
def previous
redirect_to post_path(post_base.older(@post))
end
protected
def log_and_redirect_back #preserving old functionality, but this should probably be removed
user_id = (user_signed_in? ? current_user : nil)
Rails.logger.info(":event => :link_to_nonexistent_post, :ref => #{request.env['HTTP_REFERER']}, :user_id => #{user_id}, :post_id => #{params[:id]}")
flash[:error] = I18n.t('posts.show.not_found')
redirect_to :back
end
def find_post
@post = find_by_guid_or_id_with_current_user(params[:id])
end
def post_base
Post.visible_from_author(@post.author, current_user)
end
def postJson
PostPresenter.new(@post, current_user).to_json
end
@ -101,7 +116,6 @@ class PostsController < ApplicationController
else
Post.where(key => id, :public => true).includes(:author, :comments => :author).first
end
end
def set_format_if_malformed_from_status_net

View file

@ -4,116 +4,94 @@ class PostPresenter
attr_accessor :post, :current_user
def initialize(post, current_user = nil)
self.post = post
self.current_user = current_user
@post = post
@current_user = current_user
end
def to_json(options = {})
self.post.as_api_response(:backbone).update(
@post.as_api_response(:backbone).update(
{
:user_like => self.user_like,
:user_participation => self.user_participation,
:likes_count => self.post.likes.count,
:participations_count => self.post.participations.count,
:reshares_count => self.post.reshares.count,
:user_reshare => self.user_reshare,
:next_post => self.next_post_path,
:previous_post => self.previous_post_path,
:likes => self.likes,
:reshares => self.reshares,
:comments => self.comments,
:participations => self.participations,
:frame_name => self.post.frame_name || template_name,
:user_like => user_like,
:user_participation => user_participation,
:likes_count => @post.likes.count,
:participations_count => @post.participations.count,
:reshares_count => @post.reshares.count,
:user_reshare => user_reshare,
:next_post => next_post_path,
:previous_post => previous_post_path,
:likes => likes,
:reshares => reshares,
:comments => comments,
:participations => participations,
:frame_name => @post.frame_name || template_name,
:title => title
})
end
def next_post_path
Rails.application.routes.url_helpers.next_post_path(@post)
end
def previous_post_path
Rails.application.routes.url_helpers.previous_post_path(@post)
end
def comments
as_api(post.comments)
as_api(@post.comments)
end
def likes
as_api(post.likes)
as_api(@post.likes)
end
def reshares
as_api(post.reshares)
as_api(@post.reshares)
end
def participations
as_api(post.participations)
as_api(@post.participations)
end
def user_like
return unless user_signed_in?
if like = post.likes.where(:author_id => person.id).first
like.as_api_response(:backbone)
end
@post.likes.where(:author_id => person.id).first.try(:as_api_response, :backbone)
end
def user_participation
return unless user_signed_in?
if participation = post.participations.where(:author_id => person.id).first
participation.as_api_response(:backbone)
end
@post.participations.where(:author_id => person.id).first.try(:as_api_response, :backbone)
end
def user_reshare
return unless user_signed_in?
self.post.reshares.where(:author_id => person.id).first
end
def next_post_path
if n = next_post
Rails.application.routes.url_helpers.post_path(n)
end
end
def previous_post_path
if p = previous_post
Rails.application.routes.url_helpers.post_path(p)
end
@post.reshares.where(:author_id => person.id).first
end
def title
if post.text.present?
post.text(:plain_text => true)
if @post.text.present?
@post.text(:plain_text => true)
else
I18n.translate('posts.presenter.title', :name => post.author.name)
I18n.translate('posts.presenter.title', :name => @post.author.name)
end
end
def template_name
@template_name ||= TemplatePicker.new(post).template_name
def template_name #kill me, lol, I should be client side
@template_name ||= TemplatePicker.new(@post).template_name
end
protected
def next_post
post_base.newer(post)
end
def previous_post
post_base.older(post)
end
def as_api(collection)
collection.includes(:author => :profile).all.map do |element|
element.as_api_response(:backbone)
end
end
def post_base
Post.visible_from_author(self.post.author, current_user)
end
def person
self.current_user.person
@current_user.person
end
def user_signed_in?
current_user.present?
@current_user.present?
end
end

View file

@ -12,6 +12,10 @@ Diaspora::Application.routes.draw do
resources :status_messages, :only => [:new, :create]
resources :posts do
member do
get :next
get :previous
end
resources :likes, :only => [:create, :destroy, :index]
resources :participations, :only => [:create, :destroy, :index]
resources :comments, :only => [:new, :create, :destroy, :index]