##// END OF EJS Templates
Refactor: move NewsController#add_comment to CommentsController#create...
Eric Davis -
r4056:1f2f9536875a
parent child
Show More
@@ -0,0 +1,30
1 class CommentsController < ApplicationController
2 default_search_scope :news
3 model_object News
4 before_filter :find_model_object
5 before_filter :find_project_from_association
6 before_filter :authorize
7
8 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
9 def create
10 @comment = Comment.new(params[:comment])
11 @comment.author = User.current
12 if @news.comments << @comment
13 flash[:notice] = l(:label_comment_added)
14 end
15
16 redirect_to :controller => 'news', :action => 'show', :id => @news
17 end
18
19 private
20
21 # ApplicationController's find_model_object sets it based on the controller
22 # name so it needs to be overriden and set to @news instead
23 def find_model_object
24 super
25 @news = @object
26 @comment = nil
27 @news
28 end
29
30 end
@@ -0,0 +1,46
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.dirname(__FILE__) + '/../test_helper'
19
20 class CommentsControllerTest < ActionController::TestCase
21 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :news, :comments
22
23 def setup
24 User.current = nil
25 end
26
27 def test_add_comment
28 @request.session[:user_id] = 2
29 post :create, :id => 1, :comment => { :comments => 'This is a test comment' }
30 assert_redirected_to 'news/1'
31
32 comment = News.find(1).comments.find(:first, :order => 'created_on DESC')
33 assert_not_nil comment
34 assert_equal 'This is a test comment', comment.comments
35 assert_equal User.find(2), comment.author
36 end
37
38 def test_empty_comment_should_not_be_added
39 @request.session[:user_id] = 2
40 assert_no_difference 'Comment.count' do
41 post :create, :id => 1, :comment => { :comments => '' }
42 assert_response :redirect
43 assert_redirected_to 'news/1'
44 end
45 end
46 end
@@ -73,18 +73,6 class NewsController < ApplicationController
73 end
73 end
74 end
74 end
75
75
76 def add_comment
77 @comment = Comment.new(params[:comment])
78 @comment.author = User.current
79 if @news.comments << @comment
80 flash[:notice] = l(:label_comment_added)
81 redirect_to :action => 'show', :id => @news
82 else
83 show
84 render :action => 'show'
85 end
86 end
87
88 def destroy_comment
76 def destroy_comment
89 @news.comments.find(params[:comment_id]).destroy
77 @news.comments.find(params[:comment_id]).destroy
90 redirect_to :action => 'show', :id => @news
78 redirect_to :action => 'show', :id => @news
@@ -47,9 +47,9
47 <% end if @comments.any? %>
47 <% end if @comments.any? %>
48 </div>
48 </div>
49
49
50 <% if authorize_for 'news', 'add_comment' %>
50 <% if authorize_for 'comments', 'create' %>
51 <p><%= toggle_link l(:label_comment_add), "add_comment_form", :focus => "comment_comments" %></p>
51 <p><%= toggle_link l(:label_comment_add), "add_comment_form", :focus => "comment_comments" %></p>
52 <% form_tag({:action => 'add_comment', :id => @news}, :id => "add_comment_form", :style => "display:none;") do %>
52 <% form_tag({:controller => 'comments', :action => 'create', :id => @news}, :id => "add_comment_form", :style => "display:none;") do %>
53 <div class="box">
53 <div class="box">
54 <%= text_area 'comment', 'comments', :cols => 80, :rows => 15, :class => 'wiki-edit' %>
54 <%= text_area 'comment', 'comments', :cols => 80, :rows => 15, :class => 'wiki-edit' %>
55 <%= wikitoolbar_for 'comment_comments' %>
55 <%= wikitoolbar_for 'comment_comments' %>
@@ -152,6 +152,8 ActionController::Routing::Routes.draw do |map|
152 news_actions.connect 'news/:id/destroy', :action => 'destroy'
152 news_actions.connect 'news/:id/destroy', :action => 'destroy'
153 end
153 end
154 news_routes.connect 'news/:id/edit', :action => 'update', :conditions => {:method => :put}
154 news_routes.connect 'news/:id/edit', :action => 'update', :conditions => {:method => :put}
155
156 news_routes.connect 'news/:id/comments', :controller => 'comments', :action => 'create', :conditions => {:method => :post}
155 end
157 end
156
158
157 map.connect 'projects/:id/members/new', :controller => 'members', :action => 'new'
159 map.connect 'projects/:id/members/new', :controller => 'members', :action => 'new'
@@ -93,7 +93,7 Redmine::AccessControl.map do |map|
93 map.project_module :news do |map|
93 map.project_module :news do |map|
94 map.permission :manage_news, {:news => [:new, :create, :edit, :update, :destroy, :destroy_comment]}, :require => :member
94 map.permission :manage_news, {:news => [:new, :create, :edit, :update, :destroy, :destroy_comment]}, :require => :member
95 map.permission :view_news, {:news => [:index, :show]}, :public => true
95 map.permission :view_news, {:news => [:index, :show]}, :public => true
96 map.permission :comment_news, {:news => :add_comment}
96 map.permission :comment_news, {:comments => :create}
97 end
97 end
98
98
99 map.project_module :documents do |map|
99 map.project_module :documents do |map|
@@ -111,26 +111,6 class NewsControllerTest < ActionController::TestCase
111 :content => /1 error/
111 :content => /1 error/
112 end
112 end
113
113
114 def test_add_comment
115 @request.session[:user_id] = 2
116 post :add_comment, :id => 1, :comment => { :comments => 'This is a NewsControllerTest comment' }
117 assert_redirected_to 'news/1'
118
119 comment = News.find(1).comments.find(:first, :order => 'created_on DESC')
120 assert_not_nil comment
121 assert_equal 'This is a NewsControllerTest comment', comment.comments
122 assert_equal User.find(2), comment.author
123 end
124
125 def test_empty_comment_should_not_be_added
126 @request.session[:user_id] = 2
127 assert_no_difference 'Comment.count' do
128 post :add_comment, :id => 1, :comment => { :comments => '' }
129 assert_response :success
130 assert_template 'show'
131 end
132 end
133
134 def test_destroy_comment
114 def test_destroy_comment
135 comments_count = News.find(1).comments.size
115 comments_count = News.find(1).comments.size
136 @request.session[:user_id] = 2
116 @request.session[:user_id] = 2
@@ -160,7 +160,8 class RoutingTest < ActionController::IntegrationTest
160
160
161 should_route :post, "/projects/567/news", :controller => 'news', :action => 'create', :project_id => '567'
161 should_route :post, "/projects/567/news", :controller => 'news', :action => 'create', :project_id => '567'
162 should_route :post, "/news/567/destroy", :controller => 'news', :action => 'destroy', :id => '567'
162 should_route :post, "/news/567/destroy", :controller => 'news', :action => 'destroy', :id => '567'
163
163 should_route :post, "/news/567/comments", :controller => 'comments', :action => 'create', :id => '567'
164
164 should_route :put, "/news/567/edit", :controller => 'news', :action => 'update', :id => '567'
165 should_route :put, "/news/567/edit", :controller => 'news', :action => 'update', :id => '567'
165 end
166 end
166
167
General Comments 0
You need to be logged in to leave comments. Login now