From 401197a895680f824f9aec9d82b25ae686e6b206 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Thu, 23 Sep 2010 15:20:19 +0000 Subject: [PATCH] Refactor: move #destroy_comment method to CommentsController#destroy git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4172 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/comments_controller.rb | 6 ++++++ app/controllers/news_controller.rb | 5 ----- app/views/news/show.rhtml | 4 ++-- config/routes.rb | 1 + lib/redmine.rb | 2 +- test/functional/comments_controller_test.rb | 11 +++++++++++ test/functional/news_controller_test.rb | 9 --------- test/integration/routing_test.rb | 2 ++ 8 files changed, 23 insertions(+), 17 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index c0d524a25..7432f831f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,6 +16,12 @@ class CommentsController < ApplicationController redirect_to :controller => 'news', :action => 'show', :id => @news end + verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } + def destroy + @news.comments.find(params[:comment_id]).destroy + redirect_to :controller => 'news', :action => 'show', :id => @news + end + private # ApplicationController's find_model_object sets it based on the controller diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index d15ae3291..47f467862 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -73,11 +73,6 @@ class NewsController < ApplicationController end end - def destroy_comment - @news.comments.find(params[:comment_id]).destroy - redirect_to :action => 'show', :id => @news - end - def destroy @news.destroy redirect_to :action => 'index', :project_id => @project diff --git a/app/views/news/show.rhtml b/app/views/news/show.rhtml index 9b8f2510e..7861a3d01 100644 --- a/app/views/news/show.rhtml +++ b/app/views/news/show.rhtml @@ -39,8 +39,8 @@ <% @comments.each do |comment| %> <% next if comment.new_record? %>
- <%= link_to_if_authorized image_tag('delete.png'), {:controller => 'news', :action => 'destroy_comment', :id => @news, :comment_id => comment}, - :confirm => l(:text_are_you_sure), :method => :post, :title => l(:button_delete) %> + <%= link_to_if_authorized image_tag('delete.png'), {:controller => 'comments', :action => 'destroy', :id => @news, :comment_id => comment}, + :confirm => l(:text_are_you_sure), :method => :delete, :title => l(:button_delete) %>

<%= avatar(comment.author, :size => "24") %><%= authoring comment.created_on, comment.author %>

<%= textilizable(comment.comments) %> diff --git a/config/routes.rb b/config/routes.rb index 6c765dab1..42e778d0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -154,6 +154,7 @@ ActionController::Routing::Routes.draw do |map| news_routes.connect 'news/:id/edit', :action => 'update', :conditions => {:method => :put} news_routes.connect 'news/:id/comments', :controller => 'comments', :action => 'create', :conditions => {:method => :post} + news_routes.connect 'news/:id/comments/:comment_id', :controller => 'comments', :action => 'destroy', :conditions => {:method => :delete} end map.connect 'projects/:id/members/new', :controller => 'members', :action => 'new' diff --git a/lib/redmine.rb b/lib/redmine.rb index 198d37304..eac459c2f 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -91,7 +91,7 @@ Redmine::AccessControl.map do |map| end map.project_module :news do |map| - map.permission :manage_news, {:news => [:new, :create, :edit, :update, :destroy, :destroy_comment]}, :require => :member + map.permission :manage_news, {:news => [:new, :create, :edit, :update, :destroy], :comments => [:destroy]}, :require => :member map.permission :view_news, {:news => [:index, :show]}, :public => true map.permission :comment_news, {:comments => :create} end diff --git a/test/functional/comments_controller_test.rb b/test/functional/comments_controller_test.rb index 1a9d628d8..1887c4896 100644 --- a/test/functional/comments_controller_test.rb +++ b/test/functional/comments_controller_test.rb @@ -43,4 +43,15 @@ class CommentsControllerTest < ActionController::TestCase assert_redirected_to 'news/1' end end + + def test_destroy_comment + comments_count = News.find(1).comments.size + @request.session[:user_id] = 2 + delete :destroy, :id => 1, :comment_id => 2 + assert_redirected_to 'news/1' + assert_nil Comment.find_by_id(2) + assert_equal comments_count - 1, News.find(1).comments.size + end + + end diff --git a/test/functional/news_controller_test.rb b/test/functional/news_controller_test.rb index ddc8b0c2c..219c4d7a7 100644 --- a/test/functional/news_controller_test.rb +++ b/test/functional/news_controller_test.rb @@ -111,15 +111,6 @@ class NewsControllerTest < ActionController::TestCase :content => /1 error/ end - def test_destroy_comment - comments_count = News.find(1).comments.size - @request.session[:user_id] = 2 - post :destroy_comment, :id => 1, :comment_id => 2 - assert_redirected_to 'news/1' - assert_nil Comment.find_by_id(2) - assert_equal comments_count - 1, News.find(1).comments.size - end - def test_destroy @request.session[:user_id] = 2 post :destroy, :id => 1 diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 4ce8fe4a7..3376f9313 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -163,6 +163,8 @@ class RoutingTest < ActionController::IntegrationTest should_route :post, "/news/567/comments", :controller => 'comments', :action => 'create', :id => '567' should_route :put, "/news/567/edit", :controller => 'news', :action => 'update', :id => '567' + + should_route :delete, "/news/567/comments/15", :controller => 'comments', :action => 'destroy', :id => '567', :comment_id => '15' end context "projects" do -- 2.39.5