##// END OF EJS Templates
Refactor: split ProjectsController#add into #add (GET) and #create (POST)....
Eric Davis -
r3953:763ab079424e
parent child
Show More
@@ -20,13 +20,13 class ProjectsController < ApplicationController
20 menu_item :roadmap, :only => :roadmap
20 menu_item :roadmap, :only => :roadmap
21 menu_item :settings, :only => :settings
21 menu_item :settings, :only => :settings
22
22
23 before_filter :find_project, :except => [ :index, :list, :add, :copy ]
23 before_filter :find_project, :except => [ :index, :list, :add, :create, :copy ]
24 before_filter :authorize, :except => [ :index, :list, :add, :copy, :archive, :unarchive, :destroy]
24 before_filter :authorize, :except => [ :index, :list, :add, :create, :copy, :archive, :unarchive, :destroy]
25 before_filter :authorize_global, :only => :add
25 before_filter :authorize_global, :only => [:add, :create]
26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27 accept_key_auth :index
27 accept_key_auth :index
28
28
29 after_filter :only => [:add, :edit, :archive, :unarchive, :destroy] do |controller|
29 after_filter :only => [:create, :edit, :archive, :unarchive, :destroy] do |controller|
30 if controller.request.post?
30 if controller.request.post?
31 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
31 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
32 end
32 end
@@ -65,35 +65,41 class ProjectsController < ApplicationController
65 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
65 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
66 @trackers = Tracker.all
66 @trackers = Tracker.all
67 @project = Project.new(params[:project])
67 @project = Project.new(params[:project])
68 if request.get?
68
69 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
69 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
70 @project.trackers = Tracker.all
70 @project.trackers = Tracker.all
71 @project.is_public = Setting.default_projects_public?
71 @project.is_public = Setting.default_projects_public?
72 @project.enabled_module_names = Setting.default_projects_modules
72 @project.enabled_module_names = Setting.default_projects_modules
73 end
74
75 def create
76 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
77 @trackers = Tracker.all
78 @project = Project.new(params[:project])
79
80 @project.enabled_module_names = params[:enabled_modules]
81 if validate_parent_id && @project.save
82 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
83 # Add current user as a project member if he is not admin
84 unless User.current.admin?
85 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
86 m = Member.new(:user => User.current, :roles => [r])
87 @project.members << m
88 end
89 respond_to do |format|
90 format.html {
91 flash[:notice] = l(:notice_successful_create)
92 redirect_to :controller => 'projects', :action => 'settings', :id => @project
93 }
94 format.xml { head :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
95 end
73 else
96 else
74 @project.enabled_module_names = params[:enabled_modules]
97 respond_to do |format|
75 if validate_parent_id && @project.save
98 format.html { render :action => 'add' }
76 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
99 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
77 # Add current user as a project member if he is not admin
78 unless User.current.admin?
79 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
80 m = Member.new(:user => User.current, :roles => [r])
81 @project.members << m
82 end
83 respond_to do |format|
84 format.html {
85 flash[:notice] = l(:notice_successful_create)
86 redirect_to :controller => 'projects', :action => 'settings', :id => @project
87 }
88 format.xml { head :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
89 end
90 else
91 respond_to do |format|
92 format.html
93 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
94 end
95 end
100 end
96 end
101 end
102
97 end
103 end
98
104
99 def copy
105 def copy
@@ -195,9 +195,9 ActionController::Routing::Routes.draw do |map|
195 end
195 end
196
196
197 projects.with_options :conditions => {:method => :post} do |project_actions|
197 projects.with_options :conditions => {:method => :post} do |project_actions|
198 project_actions.connect 'projects/new', :action => 'add'
198 project_actions.connect 'projects/new', :action => 'create'
199 project_actions.connect 'projects', :action => 'add'
199 project_actions.connect 'projects', :action => 'create'
200 project_actions.connect 'projects.:format', :action => 'add', :format => /xml/
200 project_actions.connect 'projects.:format', :action => 'create', :format => /xml/
201 project_actions.connect 'projects/:id/:action', :action => /edit|destroy|archive|unarchive/
201 project_actions.connect 'projects/:id/:action', :action => /edit|destroy|archive|unarchive/
202 project_actions.connect 'projects/:id/files/new', :controller => 'files', :action => 'new'
202 project_actions.connect 'projects/:id/files/new', :controller => 'files', :action => 'new'
203 project_actions.connect 'projects/:id/activities/save', :controller => 'project_enumerations', :action => 'save'
203 project_actions.connect 'projects/:id/activities/save', :controller => 'project_enumerations', :action => 'save'
@@ -46,12 +46,12 end
46 Redmine::AccessControl.map do |map|
46 Redmine::AccessControl.map do |map|
47 map.permission :view_project, {:projects => [:show], :activities => [:index]}, :public => true
47 map.permission :view_project, {:projects => [:show], :activities => [:index]}, :public => true
48 map.permission :search_project, {:search => :index}, :public => true
48 map.permission :search_project, {:search => :index}, :public => true
49 map.permission :add_project, {:projects => :add}, :require => :loggedin
49 map.permission :add_project, {:projects => [:add, :create]}, :require => :loggedin
50 map.permission :edit_project, {:projects => [:settings, :edit]}, :require => :member
50 map.permission :edit_project, {:projects => [:settings, :edit]}, :require => :member
51 map.permission :select_project_modules, {:projects => :modules}, :require => :member
51 map.permission :select_project_modules, {:projects => :modules}, :require => :member
52 map.permission :manage_members, {:projects => :settings, :members => [:new, :edit, :destroy, :autocomplete_for_member]}, :require => :member
52 map.permission :manage_members, {:projects => :settings, :members => [:new, :edit, :destroy, :autocomplete_for_member]}, :require => :member
53 map.permission :manage_versions, {:projects => :settings, :versions => [:new, :edit, :close_completed, :destroy]}, :require => :member
53 map.permission :manage_versions, {:projects => :settings, :versions => [:new, :edit, :close_completed, :destroy]}, :require => :member
54 map.permission :add_subprojects, {:projects => :add}, :require => :member
54 map.permission :add_subprojects, {:projects => [:add, :create]}, :require => :member
55
55
56 map.project_module :issue_tracking do |map|
56 map.project_module :issue_tracking do |map|
57 # Issue categories
57 # Issue categories
@@ -98,9 +98,53 class ProjectsControllerTest < ActionController::TestCase
98 assert_response :success
98 assert_response :success
99 assert_template 'add'
99 assert_template 'add'
100 end
100 end
101
102 end
103
104 context "by non-admin user with add_project permission" do
105 setup do
106 Role.non_member.add_permission! :add_project
107 @request.session[:user_id] = 9
108 end
109
110 should "accept get" do
111 get :add
112 assert_response :success
113 assert_template 'add'
114 assert_no_tag :select, :attributes => {:name => 'project[parent_id]'}
115 end
116 end
117
118 context "by non-admin user with add_subprojects permission" do
119 setup do
120 Role.find(1).remove_permission! :add_project
121 Role.find(1).add_permission! :add_subprojects
122 @request.session[:user_id] = 2
123 end
124
125 should "accept get" do
126 get :add, :parent_id => 'ecookbook'
127 assert_response :success
128 assert_template 'add'
129 # parent project selected
130 assert_tag :select, :attributes => {:name => 'project[parent_id]'},
131 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
132 # no empty value
133 assert_no_tag :select, :attributes => {:name => 'project[parent_id]'},
134 :child => {:tag => 'option', :attributes => {:value => ''}}
135 end
136 end
137
138 end
139
140 context "POST :create" do
141 context "by admin user" do
142 setup do
143 @request.session[:user_id] = 1
144 end
101
145
102 should "accept post" do
146 should "create a new project" do
103 post :add, :project => { :name => "blog",
147 post :create, :project => { :name => "blog",
104 :description => "weblog",
148 :description => "weblog",
105 :identifier => "blog",
149 :identifier => "blog",
106 :is_public => 1,
150 :is_public => 1,
@@ -115,8 +159,8 class ProjectsControllerTest < ActionController::TestCase
115 assert_nil project.parent
159 assert_nil project.parent
116 end
160 end
117
161
118 should "accept post with parent" do
162 should "create a new subproject" do
119 post :add, :project => { :name => "blog",
163 post :create, :project => { :name => "blog",
120 :description => "weblog",
164 :description => "weblog",
121 :identifier => "blog",
165 :identifier => "blog",
122 :is_public => 1,
166 :is_public => 1,
@@ -137,15 +181,8 class ProjectsControllerTest < ActionController::TestCase
137 @request.session[:user_id] = 9
181 @request.session[:user_id] = 9
138 end
182 end
139
183
140 should "accept get" do
184 should "accept create a Project" do
141 get :add
185 post :create, :project => { :name => "blog",
142 assert_response :success
143 assert_template 'add'
144 assert_no_tag :select, :attributes => {:name => 'project[parent_id]'}
145 end
146
147 should "accept post" do
148 post :add, :project => { :name => "blog",
149 :description => "weblog",
186 :description => "weblog",
150 :identifier => "blog",
187 :identifier => "blog",
151 :is_public => 1,
188 :is_public => 1,
@@ -166,7 +203,7 class ProjectsControllerTest < ActionController::TestCase
166
203
167 should "fail with parent_id" do
204 should "fail with parent_id" do
168 assert_no_difference 'Project.count' do
205 assert_no_difference 'Project.count' do
169 post :add, :project => { :name => "blog",
206 post :create, :project => { :name => "blog",
170 :description => "weblog",
207 :description => "weblog",
171 :identifier => "blog",
208 :identifier => "blog",
172 :is_public => 1,
209 :is_public => 1,
@@ -188,20 +225,8 class ProjectsControllerTest < ActionController::TestCase
188 @request.session[:user_id] = 2
225 @request.session[:user_id] = 2
189 end
226 end
190
227
191 should "accept get" do
228 should "create a project with a parent_id" do
192 get :add, :parent_id => 'ecookbook'
229 post :create, :project => { :name => "blog",
193 assert_response :success
194 assert_template 'add'
195 # parent project selected
196 assert_tag :select, :attributes => {:name => 'project[parent_id]'},
197 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
198 # no empty value
199 assert_no_tag :select, :attributes => {:name => 'project[parent_id]'},
200 :child => {:tag => 'option', :attributes => {:value => ''}}
201 end
202
203 should "accept post with parent_id" do
204 post :add, :project => { :name => "blog",
205 :description => "weblog",
230 :description => "weblog",
206 :identifier => "blog",
231 :identifier => "blog",
207 :is_public => 1,
232 :is_public => 1,
@@ -214,7 +239,7 class ProjectsControllerTest < ActionController::TestCase
214
239
215 should "fail without parent_id" do
240 should "fail without parent_id" do
216 assert_no_difference 'Project.count' do
241 assert_no_difference 'Project.count' do
217 post :add, :project => { :name => "blog",
242 post :create, :project => { :name => "blog",
218 :description => "weblog",
243 :description => "weblog",
219 :identifier => "blog",
244 :identifier => "blog",
220 :is_public => 1,
245 :is_public => 1,
@@ -230,7 +255,7 class ProjectsControllerTest < ActionController::TestCase
230 should "fail with unauthorized parent_id" do
255 should "fail with unauthorized parent_id" do
231 assert !User.find(2).member_of?(Project.find(6))
256 assert !User.find(2).member_of?(Project.find(6))
232 assert_no_difference 'Project.count' do
257 assert_no_difference 'Project.count' do
233 post :add, :project => { :name => "blog",
258 post :create, :project => { :name => "blog",
234 :description => "weblog",
259 :description => "weblog",
235 :identifier => "blog",
260 :identifier => "blog",
236 :is_public => 1,
261 :is_public => 1,
@@ -178,8 +178,8 class RoutingTest < ActionController::IntegrationTest
178 should_route :get, "/projects/33/activity", :controller => 'activities', :action => 'index', :id => '33'
178 should_route :get, "/projects/33/activity", :controller => 'activities', :action => 'index', :id => '33'
179 should_route :get, "/projects/33/activity.atom", :controller => 'activities', :action => 'index', :id => '33', :format => 'atom'
179 should_route :get, "/projects/33/activity.atom", :controller => 'activities', :action => 'index', :id => '33', :format => 'atom'
180
180
181 should_route :post, "/projects/new", :controller => 'projects', :action => 'add'
181 should_route :post, "/projects/new", :controller => 'projects', :action => 'create'
182 should_route :post, "/projects.xml", :controller => 'projects', :action => 'add', :format => 'xml'
182 should_route :post, "/projects.xml", :controller => 'projects', :action => 'create', :format => 'xml'
183 should_route :post, "/projects/4223/edit", :controller => 'projects', :action => 'edit', :id => '4223'
183 should_route :post, "/projects/4223/edit", :controller => 'projects', :action => 'edit', :id => '4223'
184 should_route :post, "/projects/64/destroy", :controller => 'projects', :action => 'destroy', :id => '64'
184 should_route :post, "/projects/64/destroy", :controller => 'projects', :action => 'destroy', :id => '64'
185 should_route :post, "/projects/33/files/new", :controller => 'files', :action => 'new', :id => '33'
185 should_route :post, "/projects/33/files/new", :controller => 'files', :action => 'new', :id => '33'
General Comments 0
You need to be logged in to leave comments. Login now