Skip to content
Extraits de code Groupes Projets
Valider 2fb0fce1 rédigé par Raphael Sofaer's avatar Raphael Sofaer
Parcourir les fichiers

Nest likes and comments routes in the post route

parent 875eaffd
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -9,7 +9,7 @@ class LikesController < ApplicationController ...@@ -9,7 +9,7 @@ class LikesController < ApplicationController
respond_to :html, :mobile, :json respond_to :html, :mobile, :json
def create def create
target = current_user.find_visible_post_by_id params[:status_message_id] target = current_user.find_visible_post_by_id params[:post_id]
positive = (params[:positive] == 'true') ? true : false positive = (params[:positive] == 'true') ? true : false
if target if target
@like = current_user.build_like(:positive => positive, :post => target) @like = current_user.build_like(:positive => positive, :post => target)
...@@ -32,7 +32,7 @@ class LikesController < ApplicationController ...@@ -32,7 +32,7 @@ class LikesController < ApplicationController
end end
def destroy def destroy
if @like = Like.where(:id => params[:id], :author_id => current_user.person.id, :post_id => params[:status_message_id]).first if @like = Like.where(:id => params[:id], :author_id => current_user.person.id, :post_id => params[:post_id]).first
current_user.retract(@like) current_user.retract(@like)
else else
respond_to do |format| respond_to do |format|
...@@ -43,7 +43,7 @@ class LikesController < ApplicationController ...@@ -43,7 +43,7 @@ class LikesController < ApplicationController
end end
def index def index
if target = current_user.find_visible_post_by_id(params[:status_message_id]) if target = current_user.find_visible_post_by_id(params[:post_id])
@likes = target.likes.includes(:author => :profile) @likes = target.likes.includes(:author => :profile)
render :layout => false render :layout => false
else else
......
...@@ -10,9 +10,9 @@ module LikesHelper ...@@ -10,9 +10,9 @@ module LikesHelper
def like_action(post, current_user=current_user) def like_action(post, current_user=current_user)
if current_user.liked?(post) if current_user.liked?(post)
link_to t('shared.stream_element.unlike'), status_message_like_path(post, current_user.like_for(post)), :method => :delete, :class => 'unlike', :remote => true link_to t('shared.stream_element.unlike'), post_like_path(post, current_user.like_for(post)), :method => :delete, :class => 'unlike', :remote => true
else else
link_to t('shared.stream_element.like'), status_message_likes_path(post, :positive => 'true'), :method => :post, :class => 'like', :remote => true link_to t('shared.stream_element.like'), post_likes_path(post, :positive => 'true'), :method => :post, :class => 'like', :remote => true
end end
end end
end end
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
%li.comment.posted{:data=>{:guid => comment.id}, :class => ("hidden" if(defined? hidden))} %li.comment.posted{:data=>{:guid => comment.id}, :class => ("hidden" if(defined? hidden))}
- if current_user && (current_user.owns?(comment) || current_user.owns?(post)) - if current_user && (current_user.owns?(comment) || current_user.owns?(post))
.right.controls .right.controls
= link_to image_tag('deletelabel.png'), comment_path(comment), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete comment_delete", :title => t('delete') = link_to image_tag('deletelabel.png'), post_comment_path(comment.post_id, comment), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete comment_delete", :title => t('delete')
= person_image_link(comment.author) = person_image_link(comment.author)
.content .content
%span.from %span.from
......
...@@ -2,12 +2,11 @@ ...@@ -2,12 +2,11 @@
-# licensed under the Affero General Public License version 3 or later. See -# licensed under the Affero General Public License version 3 or later. See
-# the COPYRIGHT file. -# the COPYRIGHT file.
= form_tag( comments_path, :id => "new_comment_on_#{post_id}", :class => 'new_comment', :remote => true) do = form_tag( post_comments_path(post_id), :id => "new_comment_on_#{post_id}", :class => 'new_comment', :remote => true) do
= person_image_tag(current_user) = person_image_tag(current_user)
%p %p
= label_tag "comment_text_on_#{post_id}", t('.comment') = label_tag "comment_text_on_#{post_id}", t('.comment')
= text_area_tag :text, nil, :rows => 2, :class => "comment_box",:id => "comment_text_on_#{post_id}" = text_area_tag :text, nil, :rows => 2, :class => "comment_box",:id => "comment_text_on_#{post_id}"
= hidden_field_tag :post_id, post_id, :id => "post_id_on_#{post_id}"
.submit_button .submit_button
= submit_tag t('.comment'), :id => "comment_submit_#{post_id}", :class => "comment_submit button creation", :disable_with => t('.commenting') = submit_tag t('.comment'), :id => "comment_submit_#{post_id}", :class => "comment_submit button creation", :disable_with => t('.commenting')
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
-# licensed under the Affero General Public License version 3 or later. See -# licensed under the Affero General Public License version 3 or later. See
-# the COPYRIGHT file. -# the COPYRIGHT file.
= form_tag( comments_path, :id => "new_comment_on_#{post_id}", :class => 'new_comment') do = form_tag( post_comments_path(post_id), :id => "new_comment_on_#{post_id}", :class => 'new_comment') do
= hidden_field_tag :post_id, post_id, :id => "post_id_on_#{post_id}" = hidden_field_tag :post_id, post_id, :id => "post_id_on_#{post_id}"
= text_area_tag :text, nil, :rows => 2, :class => "comment_box",:id => "comment_text_on_#{post_id}" = text_area_tag :text, nil, :rows => 2, :class => "comment_box",:id => "comment_text_on_#{post_id}"
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
.likes_container .likes_container
.likes .likes
= image_tag('icons/heart.svg') = image_tag('icons/heart.svg')
= link_to t('likes.likes.people_like_this', :count => likes_count), status_message_likes_path(post_id), :class => "expand_likes" = link_to t('likes.likes.people_like_this', :count => likes_count), post_likes_path(post_id), :class => "expand_likes"
%span.hidden.likes_list %span.hidden.likes_list
/= render 'likes/likes', :likes => likes /= render 'likes/likes', :likes => likes
...@@ -12,11 +12,12 @@ Diaspora::Application.routes.draw do ...@@ -12,11 +12,12 @@ Diaspora::Application.routes.draw do
end end
resources :status_messages, :only => [:new, :create] do resources :status_messages, :only => [:new, :create] do
resources :likes, :only => [:create, :destroy, :index]
end end
resources :comments, :only => [:create, :destroy] resources :posts, :only => [:show, :destroy] do
resources :posts, :only => [:show, :destroy] resources :likes, :only => [:create, :destroy, :index]
resources :comments, :only => [:create, :destroy]
end
get 'bookmarklet' => 'status_messages#bookmarklet' get 'bookmarklet' => 'status_messages#bookmarklet'
get 'p/:id' => 'publics#post', :as => 'public_post' get 'p/:id' => 'publics#post', :as => 'public_post'
......
...@@ -80,13 +80,13 @@ describe CommentsController do ...@@ -80,13 +80,13 @@ describe CommentsController do
it 'lets the user delete his comment' do it 'lets the user delete his comment' do
alice.should_receive(:retract).with(@comment) alice.should_receive(:retract).with(@comment)
delete :destroy, :format => "js", :id => @comment.id delete :destroy, :format => "js", :post_id => 1, :id => @comment.id
response.status.should == 204 response.status.should == 204
end end
it "lets the user destroy other people's comments" do it "lets the user destroy other people's comments" do
alice.should_receive(:retract).with(@comment2) alice.should_receive(:retract).with(@comment2)
delete :destroy, :format => "js", :id => @comment2.id delete :destroy, :format => "js", :post_id => 1, :id => @comment2.id
response.status.should == 204 response.status.should == 204
end end
end end
...@@ -101,18 +101,18 @@ describe CommentsController do ...@@ -101,18 +101,18 @@ describe CommentsController do
it 'let the user delete his comment' do it 'let the user delete his comment' do
alice.should_receive(:retract).with(@comment) alice.should_receive(:retract).with(@comment)
delete :destroy, :format => "js", :id => @comment.id delete :destroy, :format => "js", :post_id => 1, :id => @comment.id
response.status.should == 204 response.status.should == 204
end end
it 'does not let the user destroy comments he does not own' do it 'does not let the user destroy comments he does not own' do
alice.should_not_receive(:retract).with(@comment2) alice.should_not_receive(:retract).with(@comment2)
delete :destroy, :format => "js", :id => @comment3.id delete :destroy, :format => "js", :post_id => 1, :id => @comment3.id
response.status.should == 403 response.status.should == 403
end end
end end
it 'renders nothing and 404 on a nonexistent comment' do it 'renders nothing and 404 on a nonexistent comment' do
delete :destroy, :id => 343415 delete :destroy, :post_id => 1, :id => 343415
response.status.should == 404 response.status.should == 404
response.body.strip.should be_empty response.body.strip.should be_empty
end end
......
...@@ -18,11 +18,11 @@ describe LikesController do ...@@ -18,11 +18,11 @@ describe LikesController do
describe '#create' do describe '#create' do
let(:like_hash) { let(:like_hash) {
{:positive => 1, {:positive => 1,
:status_message_id => "#{@post.id}"} :post_id => "#{@post.id}"}
} }
let(:dislike_hash) { let(:dislike_hash) {
{:positive => 0, {:positive => 0,
:status_message_id => "#{@post.id}"} :post_id => "#{@post.id}"}
} }
context "on my own post" do context "on my own post" do
...@@ -77,19 +77,19 @@ describe LikesController do ...@@ -77,19 +77,19 @@ describe LikesController do
end end
it 'returns a 404 for a post not visible to the user' do it 'returns a 404 for a post not visible to the user' do
sign_in eve sign_in eve
get :index, :status_message_id => @message.id get :index, :post_id => @message.id
end end
it 'returns an array of likes for a post' do it 'returns an array of likes for a post' do
like = bob.build_like(:positive => true, :post => @message) like = bob.build_like(:positive => true, :post => @message)
like.save! like.save!
get :index, :status_message_id => @message.id get :index, :post_id => @message.id
assigns[:likes].map(&:id).should == @message.likes.map(&:id) assigns[:likes].map(&:id).should == @message.likes.map(&:id)
end end
it 'returns an empty array for a post with no likes' do it 'returns an empty array for a post with no likes' do
get :index, :status_message_id => @message.id get :index, :post_id => @message.id
assigns[:likes].should == [] assigns[:likes].should == []
end end
end end
...@@ -103,7 +103,7 @@ describe LikesController do ...@@ -103,7 +103,7 @@ describe LikesController do
it 'lets a user destroy their like' do it 'lets a user destroy their like' do
expect { expect {
delete :destroy, :format => "js", :status_message_id => @like.post_id, :id => @like.id delete :destroy, :format => "js", :post_id => @like.post_id, :id => @like.id
}.should change(Like, :count).by(-1) }.should change(Like, :count).by(-1)
response.status.should == 200 response.status.should == 200
end end
...@@ -113,7 +113,7 @@ describe LikesController do ...@@ -113,7 +113,7 @@ describe LikesController do
like2.save like2.save
expect { expect {
delete :destroy, :format => "js", :status_message_id => like2.post_id, :id => like2.id delete :destroy, :format => "js", :post_id => like2.post_id, :id => like2.id
}.should_not change(Like, :count) }.should_not change(Like, :count)
response.status.should == 403 response.status.should == 403
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter