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:
parent
69b9b5768f
commit
0fc399243b
4 changed files with 78 additions and 96 deletions
|
|
@ -1,11 +1,6 @@
|
||||||
app.views.PostViewerNav = app.views.Base.extend({
|
app.views.PostViewerNav = app.views.Base.extend({
|
||||||
|
|
||||||
templateName: "post-viewer/nav",
|
templateName: "post-viewer/nav",
|
||||||
|
|
||||||
events : {
|
|
||||||
"click a" : "pjax"
|
|
||||||
},
|
|
||||||
|
|
||||||
postRenderTemplate : function() {
|
postRenderTemplate : function() {
|
||||||
var mappings = {"#forward" : "next_post",
|
var mappings = {"#forward" : "next_post",
|
||||||
"#back" : "previous_post"};
|
"#back" : "previous_post"};
|
||||||
|
|
@ -17,14 +12,5 @@ app.views.PostViewerNav = app.views.Base.extend({
|
||||||
|
|
||||||
setArrow : function(arrow, loc) {
|
setArrow : function(arrow, loc) {
|
||||||
loc ? arrow.attr('href', loc) : arrow.remove()
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -9,6 +9,7 @@ class PostsController < ApplicationController
|
||||||
|
|
||||||
before_filter :authenticate_user!, :except => [:show, :iframe, :oembed]
|
before_filter :authenticate_user!, :except => [:show, :iframe, :oembed]
|
||||||
before_filter :set_format_if_malformed_from_status_net, :only => :show
|
before_filter :set_format_if_malformed_from_status_net, :only => :show
|
||||||
|
before_filter :find_post, :only => [:show, :next, :previous]
|
||||||
|
|
||||||
layout 'post'
|
layout 'post'
|
||||||
|
|
||||||
|
|
@ -24,9 +25,7 @@ class PostsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@post = find_by_guid_or_id_with_current_user(params[:id])
|
return log_and_redirect_back unless @post
|
||||||
|
|
||||||
if @post
|
|
||||||
# @commenting_disabled = can_not_comment_on_post?
|
# @commenting_disabled = can_not_comment_on_post?
|
||||||
# mark corresponding notification as read
|
# mark corresponding notification as read
|
||||||
if user_signed_in? && notification = Notification.where(:recipient_id => current_user.id, :target_id => @post.id).first
|
if user_signed_in? && notification = Notification.where(:recipient_id => current_user.id, :target_id => @post.id).first
|
||||||
|
|
@ -40,13 +39,6 @@ class PostsController < ApplicationController
|
||||||
format.mobile{render 'posts/show.mobile.haml', :layout => "application"}
|
format.mobile{render 'posts/show.mobile.haml', :layout => "application"}
|
||||||
format.json{ render :json => postJson }
|
format.json{ render :json => postJson }
|
||||||
end
|
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
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def iframe
|
def iframe
|
||||||
|
|
@ -88,8 +80,31 @@ class PostsController < ApplicationController
|
||||||
end
|
end
|
||||||
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
|
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
|
def postJson
|
||||||
PostPresenter.new(@post, current_user).to_json
|
PostPresenter.new(@post, current_user).to_json
|
||||||
end
|
end
|
||||||
|
|
@ -101,7 +116,6 @@ class PostsController < ApplicationController
|
||||||
else
|
else
|
||||||
Post.where(key => id, :public => true).includes(:author, :comments => :author).first
|
Post.where(key => id, :public => true).includes(:author, :comments => :author).first
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_format_if_malformed_from_status_net
|
def set_format_if_malformed_from_status_net
|
||||||
|
|
|
||||||
|
|
@ -4,116 +4,94 @@ class PostPresenter
|
||||||
attr_accessor :post, :current_user
|
attr_accessor :post, :current_user
|
||||||
|
|
||||||
def initialize(post, current_user = nil)
|
def initialize(post, current_user = nil)
|
||||||
self.post = post
|
@post = post
|
||||||
self.current_user = current_user
|
@current_user = current_user
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_json(options = {})
|
def to_json(options = {})
|
||||||
self.post.as_api_response(:backbone).update(
|
@post.as_api_response(:backbone).update(
|
||||||
{
|
{
|
||||||
:user_like => self.user_like,
|
:user_like => user_like,
|
||||||
:user_participation => self.user_participation,
|
:user_participation => user_participation,
|
||||||
:likes_count => self.post.likes.count,
|
:likes_count => @post.likes.count,
|
||||||
:participations_count => self.post.participations.count,
|
:participations_count => @post.participations.count,
|
||||||
:reshares_count => self.post.reshares.count,
|
:reshares_count => @post.reshares.count,
|
||||||
:user_reshare => self.user_reshare,
|
:user_reshare => user_reshare,
|
||||||
:next_post => self.next_post_path,
|
:next_post => next_post_path,
|
||||||
:previous_post => self.previous_post_path,
|
:previous_post => previous_post_path,
|
||||||
:likes => self.likes,
|
:likes => likes,
|
||||||
:reshares => self.reshares,
|
:reshares => reshares,
|
||||||
:comments => self.comments,
|
:comments => comments,
|
||||||
:participations => self.participations,
|
:participations => participations,
|
||||||
:frame_name => self.post.frame_name || template_name,
|
:frame_name => @post.frame_name || template_name,
|
||||||
:title => title
|
:title => title
|
||||||
})
|
})
|
||||||
end
|
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
|
def comments
|
||||||
as_api(post.comments)
|
as_api(@post.comments)
|
||||||
end
|
end
|
||||||
|
|
||||||
def likes
|
def likes
|
||||||
as_api(post.likes)
|
as_api(@post.likes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def reshares
|
def reshares
|
||||||
as_api(post.reshares)
|
as_api(@post.reshares)
|
||||||
end
|
end
|
||||||
|
|
||||||
def participations
|
def participations
|
||||||
as_api(post.participations)
|
as_api(@post.participations)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def user_like
|
def user_like
|
||||||
return unless user_signed_in?
|
return unless user_signed_in?
|
||||||
if like = post.likes.where(:author_id => person.id).first
|
@post.likes.where(:author_id => person.id).first.try(:as_api_response, :backbone)
|
||||||
like.as_api_response(:backbone)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_participation
|
def user_participation
|
||||||
return unless user_signed_in?
|
return unless user_signed_in?
|
||||||
if participation = post.participations.where(:author_id => person.id).first
|
@post.participations.where(:author_id => person.id).first.try(:as_api_response, :backbone)
|
||||||
participation.as_api_response(:backbone)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_reshare
|
def user_reshare
|
||||||
return unless user_signed_in?
|
return unless user_signed_in?
|
||||||
self.post.reshares.where(:author_id => person.id).first
|
@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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def title
|
def title
|
||||||
if post.text.present?
|
if @post.text.present?
|
||||||
post.text(:plain_text => true)
|
@post.text(:plain_text => true)
|
||||||
else
|
else
|
||||||
I18n.translate('posts.presenter.title', :name => post.author.name)
|
I18n.translate('posts.presenter.title', :name => @post.author.name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def template_name
|
def template_name #kill me, lol, I should be client side
|
||||||
@template_name ||= TemplatePicker.new(post).template_name
|
@template_name ||= TemplatePicker.new(@post).template_name
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def next_post
|
|
||||||
post_base.newer(post)
|
|
||||||
end
|
|
||||||
|
|
||||||
def previous_post
|
|
||||||
post_base.older(post)
|
|
||||||
end
|
|
||||||
|
|
||||||
def as_api(collection)
|
def as_api(collection)
|
||||||
collection.includes(:author => :profile).all.map do |element|
|
collection.includes(:author => :profile).all.map do |element|
|
||||||
element.as_api_response(:backbone)
|
element.as_api_response(:backbone)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_base
|
|
||||||
Post.visible_from_author(self.post.author, current_user)
|
|
||||||
end
|
|
||||||
|
|
||||||
def person
|
def person
|
||||||
self.current_user.person
|
@current_user.person
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_signed_in?
|
def user_signed_in?
|
||||||
current_user.present?
|
@current_user.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,10 @@ Diaspora::Application.routes.draw do
|
||||||
resources :status_messages, :only => [:new, :create]
|
resources :status_messages, :only => [:new, :create]
|
||||||
|
|
||||||
resources :posts do
|
resources :posts do
|
||||||
|
member do
|
||||||
|
get :next
|
||||||
|
get :previous
|
||||||
|
end
|
||||||
resources :likes, :only => [:create, :destroy, :index]
|
resources :likes, :only => [:create, :destroy, :index]
|
||||||
resources :participations, :only => [:create, :destroy, :index]
|
resources :participations, :only => [:create, :destroy, :index]
|
||||||
resources :comments, :only => [:new, :create, :destroy, :index]
|
resources :comments, :only => [:new, :create, :destroy, :index]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue