next and previous now return post as json
clean up controller
This commit is contained in:
parent
79b3feec33
commit
bd6a9cfe00
2 changed files with 77 additions and 24 deletions
|
|
@ -26,18 +26,14 @@ class PostsController < ApplicationController
|
||||||
|
|
||||||
def show
|
def show
|
||||||
return log_and_redirect_back unless @post
|
return log_and_redirect_back unless @post
|
||||||
# @commenting_disabled = can_not_comment_on_post?
|
|
||||||
# mark corresponding notification as read
|
mark_corresponding_notification_read if user_signed_in?
|
||||||
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|
|
respond_to do |format|
|
||||||
format.html{ gon.post = postJson; render 'posts/show.html.haml' }
|
format.html{ gon.post = post_json(@post); render 'posts/show.html.haml' }
|
||||||
format.xml{ render :xml => @post.to_diaspora_xml }
|
format.xml{ render :xml => @post.to_diaspora_xml }
|
||||||
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 => post_json(@post) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -81,11 +77,21 @@ class PostsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def next
|
def next
|
||||||
redirect_to post_path(post_base.newer(@post))
|
next_post = visible_posts_from_author.newer(@post)
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.html{ redirect_to post_path(next_post) }
|
||||||
|
format.json{ render :json => post_json(next_post) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def previous
|
def previous
|
||||||
redirect_to post_path(post_base.older(@post))
|
previous_post = visible_posts_from_author.older(@post)
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.html{ redirect_to post_path(previous_post) }
|
||||||
|
format.json{ render :json => post_json(previous_post) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
@ -101,12 +107,12 @@ class PostsController < ApplicationController
|
||||||
@post = find_by_guid_or_id_with_current_user(params[:id])
|
@post = find_by_guid_or_id_with_current_user(params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_base
|
def visible_posts_from_author
|
||||||
Post.visible_from_author(@post.author, current_user)
|
Post.visible_from_author(@post.author, current_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
def postJson
|
def post_json(post)
|
||||||
PostPresenter.new(@post, current_user).to_json
|
PostPresenter.new(post, current_user).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_by_guid_or_id_with_current_user(id)
|
def find_by_guid_or_id_with_current_user(id)
|
||||||
|
|
@ -122,17 +128,10 @@ class PostsController < ApplicationController
|
||||||
request.format = :html if request.format == 'application/html+xml'
|
request.format = :html if request.format == 'application/html+xml'
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_not_comment_on_post?
|
def mark_corresponding_notification_read
|
||||||
if !user_signed_in?
|
if notification = Notification.where(:recipient_id => current_user.id, :target_id => @post.id).first
|
||||||
true
|
notification.unread = false
|
||||||
elsif @post.public && @post.author.local?
|
notification.save
|
||||||
false
|
|
||||||
elsif current_user.contact_for(@post.author)
|
|
||||||
false
|
|
||||||
elsif current_user.owns?(@post)
|
|
||||||
false
|
|
||||||
else
|
|
||||||
true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -167,4 +167,58 @@ describe PostsController do
|
||||||
StatusMessage.exists?(message.id).should be_true
|
StatusMessage.exists?(message.id).should be_true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "next" do
|
||||||
|
before do
|
||||||
|
sign_in alice
|
||||||
|
#lets make a class and unit test it, because this is still not working
|
||||||
|
@controller.stub_chain(:visible_posts_from_author, :newer).and_return(next_post)
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:next_post){ mock_model(StatusMessage, :id => 34)}
|
||||||
|
|
||||||
|
context "GET .json" do
|
||||||
|
let(:mock_presenter) { mock(:to_json => {:title => "the unbearable lightness of being"}) }
|
||||||
|
|
||||||
|
it "should return a show presenter the next post" do
|
||||||
|
PostPresenter.should_receive(:new).with(next_post, alice).and_return(mock_presenter)
|
||||||
|
get :next, :id => 14, :format => :json
|
||||||
|
response.body.should == {:title => "the unbearable lightness of being"}.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "GET .html" do
|
||||||
|
it "should redirect to the next post" do
|
||||||
|
get :next, :id => 14
|
||||||
|
response.should redirect_to(post_path(next_post))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "previous" do
|
||||||
|
before do
|
||||||
|
sign_in alice
|
||||||
|
#lets make a class and unit test it, because this is still not working
|
||||||
|
@controller.stub_chain(:visible_posts_from_author, :older).and_return(previous_post)
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:previous_post){ mock_model(StatusMessage, :id => 11)}
|
||||||
|
|
||||||
|
context "GET .json" do
|
||||||
|
let(:mock_presenter) { mock(:to_json => {:title => "existential crises"})}
|
||||||
|
|
||||||
|
it "should return a show presenter the next post" do
|
||||||
|
PostPresenter.should_receive(:new).with(previous_post, alice).and_return(mock_presenter)
|
||||||
|
get :previous, :id => 14, :format => :json
|
||||||
|
response.body.should == {:title => "existential crises"}.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "GET .html" do
|
||||||
|
it "should redirect to the next post" do
|
||||||
|
get :previous, :id => 14
|
||||||
|
response.should redirect_to(post_path(previous_post))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue