From 969bf2107b2ebcdb480cbb56f523ecb72643146d Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Thu, 16 Sep 2010 18:27:33 +0000 Subject: [PATCH] Refactor: split VersionsController#new into #new and #create git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4090 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/versions_controller.rb | 19 +++++++++++++++---- app/views/issues/_attributes.rhtml | 2 +- app/views/versions/new.html.erb | 4 ++-- config/routes.rb | 1 + lib/redmine.rb | 2 +- test/functional/versions_controller_test.rb | 8 ++++---- test/integration/routing_test.rb | 2 +- 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/controllers/versions_controller.rb b/app/controllers/versions_controller.rb index 96c240c50..331935d9b 100644 --- a/app/controllers/versions_controller.rb +++ b/app/controllers/versions_controller.rb @@ -18,9 +18,9 @@ class VersionsController < ApplicationController menu_item :roadmap model_object Version - before_filter :find_model_object, :except => [:index, :new, :close_completed] - before_filter :find_project_from_association, :except => [:index, :new, :close_completed] - before_filter :find_project, :only => [:index, :new, :close_completed] + before_filter :find_model_object, :except => [:index, :new, :create, :close_completed] + before_filter :find_project_from_association, :except => [:index, :new, :create, :close_completed] + before_filter :find_project, :only => [:index, :new, :create, :close_completed] before_filter :authorize helper :custom_fields @@ -63,6 +63,17 @@ class VersionsController < ApplicationController attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing']) @version.attributes = attributes end + end + + def create + # TODO: refactor with code above in #new + @version = @project.versions.build + if params[:version] + attributes = params[:version].dup + attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing']) + @version.attributes = attributes + end + if request.post? if @version.save respond_to do |format| @@ -79,7 +90,7 @@ class VersionsController < ApplicationController end else respond_to do |format| - format.html + format.html { render :action => 'new' } format.js do render(:update) {|page| page.alert(@version.errors.full_messages.join('\n')) } end diff --git a/app/views/issues/_attributes.rhtml b/app/views/issues/_attributes.rhtml index f9f3f94ea..e10858b09 100644 --- a/app/views/issues/_attributes.rhtml +++ b/app/views/issues/_attributes.rhtml @@ -23,7 +23,7 @@ <%= prompt_to_remote(image_tag('add.png', :style => 'vertical-align: middle;'), l(:label_version_new), 'version[name]', - {:controller => 'versions', :action => 'new', :project_id => @project}, + {:controller => 'versions', :action => 'create', :project_id => @project}, :title => l(:label_version_new), :tabindex => 200) if authorize_for('versions', 'new') %>

diff --git a/app/views/versions/new.html.erb b/app/views/versions/new.html.erb index a7ef03e76..9fbd94178 100644 --- a/app/views/versions/new.html.erb +++ b/app/views/versions/new.html.erb @@ -1,6 +1,6 @@

<%=l(:label_version_new)%>

-<% labelled_tabular_form_for :version, @version, :url => { :action => 'new' } do |f| %> +<% labelled_tabular_form_for :version, @version, :url => { :action => 'create', :project_id => @project } do |f| %> <%= render :partial => 'versions/form', :locals => { :f => f } %> <%= submit_tag l(:button_create) %> -<% end %> \ No newline at end of file +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 9f9a4e3f0..3197df01a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -208,6 +208,7 @@ ActionController::Routing::Routes.draw do |map| versions.connect 'versions/:action/:id', :conditions => {:method => :get} versions.with_options :conditions => {:method => :post} do |version_actions| + version_actions.connect 'projects/:project_id/versions', :action => 'create' version_actions.connect 'versions/update/:id', :action => 'update' version_actions.connect 'projects/:project_id/versions/close_completed', :action => 'close_completed' end diff --git a/lib/redmine.rb b/lib/redmine.rb index 2b27c206b..8acae48a8 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -50,7 +50,7 @@ Redmine::AccessControl.map do |map| map.permission :edit_project, {:projects => [:settings, :edit, :update]}, :require => :member map.permission :select_project_modules, {:projects => :modules}, :require => :member map.permission :manage_members, {:projects => :settings, :members => [:new, :edit, :destroy, :autocomplete_for_member]}, :require => :member - map.permission :manage_versions, {:projects => :settings, :versions => [:new, :edit, :update, :close_completed, :destroy]}, :require => :member + map.permission :manage_versions, {:projects => :settings, :versions => [:new, :create, :edit, :update, :close_completed, :destroy]}, :require => :member map.permission :add_subprojects, {:projects => [:new, :create]}, :require => :member map.project_module :issue_tracking do |map| diff --git a/test/functional/versions_controller_test.rb b/test/functional/versions_controller_test.rb index 9e8cdf90d..1b33ba816 100644 --- a/test/functional/versions_controller_test.rb +++ b/test/functional/versions_controller_test.rb @@ -75,10 +75,10 @@ class VersionsControllerTest < ActionController::TestCase assert_tag :tag => 'h2', :content => /1.0/ end - def test_new + def test_create @request.session[:user_id] = 2 # manager assert_difference 'Version.count' do - post :new, :project_id => '1', :version => {:name => 'test_add_version'} + post :create, :project_id => '1', :version => {:name => 'test_add_version'} end assert_redirected_to '/projects/ecookbook/settings/versions' version = Version.find_by_name('test_add_version') @@ -86,10 +86,10 @@ class VersionsControllerTest < ActionController::TestCase assert_equal 1, version.project_id end - def test_new_from_issue_form + def test_create_from_issue_form @request.session[:user_id] = 2 # manager assert_difference 'Version.count' do - xhr :post, :new, :project_id => '1', :version => {:name => 'test_add_version_from_issue_form'} + xhr :post, :create, :project_id => '1', :version => {:name => 'test_add_version_from_issue_form'} end assert_response :success assert_select_rjs :replace, 'issue_fixed_version_id' diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index ba4948008..794a7780e 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -257,7 +257,7 @@ class RoutingTest < ActionController::IntegrationTest should_route :get, "/versions/show/1", :controller => 'versions', :action => 'show', :id => '1' should_route :get, "/versions/edit/1", :controller => 'versions', :action => 'edit', :id => '1' - should_route :post, "/projects/foo/versions/new", :controller => 'versions', :action => 'new', :project_id => 'foo' + should_route :post, "/projects/foo/versions", :controller => 'versions', :action => 'create', :project_id => 'foo' should_route :post, "/versions/update/1", :controller => 'versions', :action => 'update', :id => '1' should_route :delete, "/versions/destroy/1", :controller => 'versions', :action => 'destroy', :id => '1' -- 2.39.5