From 3f555eefc4d552a015b93989c8d944b4a654f35f Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Mon, 11 Jul 2011 09:11:16 -0700 Subject: [PATCH] Server side of ajaxy comments done, need to modify comments/_comments and make it work. --- app/controllers/comments_controller.rb | 12 ++++++++++- app/views/comments/_comments.haml | 4 ++-- app/views/comments/index.js.erb | 2 ++ config/routes.rb | 2 +- spec/controllers/comments_controller_spec.rb | 21 ++++++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 app/views/comments/index.js.erb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index d472e457f..33dde5433 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -6,7 +6,8 @@ class CommentsController < ApplicationController include ApplicationHelper before_filter :authenticate_user! - respond_to :html, :mobile + respond_to :html, :mobile, :only => [:create, :destroy] + respond_to :js, :only => [:index] rescue_from ActiveRecord::RecordNotFound do render :nothing => true, :status => 404 @@ -53,4 +54,13 @@ class CommentsController < ApplicationController end end + def index + @post = current_user.find_visible_post_by_id(params[:post_id]) + if @post + @comments = @post.comments + else + raise ActiveRecord::RecordNotFound.new + end + end + end diff --git a/app/views/comments/_comments.haml b/app/views/comments/_comments.haml index a4e155b99..e9a4cbe8b 100644 --- a/app/views/comments/_comments.haml +++ b/app/views/comments/_comments.haml @@ -3,9 +3,9 @@ -# the COPYRIGHT file. - unless comments_expanded - %ul.show_comments{:class => ("hidden" if comments.size <= 3)} + %ul.show_comments{:class => ("hidden" if post.comments.count <= 3)} %li - %b= comment_toggle(comments.size) + %b= comment_toggle( post.comments.count ) %ul.comments{:id => post.id, :class => ("hidden" if comments.size == 0 && !comments_expanded)} -if comments.size > 3 diff --git a/app/views/comments/index.js.erb b/app/views/comments/index.js.erb new file mode 100644 index 000000000..57ade3710 --- /dev/null +++ b/app/views/comments/index.js.erb @@ -0,0 +1,2 @@ +var html = <%= render :partial => 'comments/comment', :collection => @comments, :locals => {:post => @post} %> +$('.comments', '.stream_element[data-guid="<%= @post.id %>"]').html(html); diff --git a/config/routes.rb b/config/routes.rb index b65608746..1a7926762 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,7 +16,7 @@ Diaspora::Application.routes.draw do resources :posts, :only => [:show, :destroy] do resources :likes, :only => [:create, :destroy, :index] - resources :comments, :only => [:create, :destroy] + resources :comments, :only => [:create, :destroy, :index] end get 'bookmarklet' => 'status_messages#bookmarklet' diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index 31a0e3ad8..e7a38b330 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -117,4 +117,25 @@ describe CommentsController do response.body.strip.should be_empty end end + + describe '#index' do + before do + @message = bob.post(:status_message, :text => "hey", :to => bob.aspects.first.id) + @comments = [alice, bob, eve].map{ |u| u.comment("hey", :post => @message) } + end + it 'returns all the comments for a post' do + get :index, :post_id => @message.id, :format => 'js' + assigns[:comments].should == @comments + end + it 'returns a 404 on a nonexistent post' do + get :index, :post_id => 235236, :format => 'js' + response.status.should == 404 + end + it 'returns a 404 on a post that is not visible to the signed in user' do + message = eve.post(:status_message, :text => "hey", :to => eve.aspects.first.id) + bob.comment("hey", :post => @message) + get :index, :post_id => message.id, :format => 'js' + response.status.should == 404 + end + end end