From 9284a32c9ac7b932feb72cf0d2d5fb1626ec7862 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 3 Dec 2010 16:15:16 +0000 Subject: [PATCH] Moves project attributes default assignments from ProjectsController#new to the model (#6064). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4460 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/projects_controller.rb | 7 +----- app/models/project.rb | 25 ++++++++++++++++++- app/views/projects/new.html.erb | 2 ++ test/integration/api_test/projects_test.rb | 2 ++ test/unit/project_test.rb | 29 ++++++++++++++++++++++ 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index d94dec1c..8ec94725 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -68,11 +68,6 @@ class ProjectsController < ApplicationController @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") @trackers = Tracker.all @project = Project.new(params[:project]) - - @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? - @project.trackers = Tracker.all - @project.is_public = Setting.default_projects_public? - @project.enabled_module_names = Setting.default_projects_modules end def create @@ -80,7 +75,7 @@ class ProjectsController < ApplicationController @trackers = Tracker.all @project = Project.new(params[:project]) - @project.enabled_module_names = params[:enabled_modules] + @project.enabled_module_names = params[:enabled_modules] if params[:enabled_modules] if validate_parent_id && @project.save @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') # Add current user as a project member if he is not admin diff --git a/app/models/project.rb b/app/models/project.rb index 6eb41cc8..d68958e2 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -84,6 +84,24 @@ class Project < ActiveRecord::Base named_scope :all_public, { :conditions => { :is_public => true } } named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } + def initialize(attributes = nil) + super + + initialized = (attributes || {}).stringify_keys + if !initialized.key?('identifier') && Setting.sequential_project_identifiers? + self.identifier = Project.next_identifier + end + if !initialized.key?('is_public') + self.is_public = Setting.default_projects_public? + end + if !initialized.key?('enabled_module_names') + self.enabled_module_names = Setting.default_projects_modules + end + if !initialized.key?('trackers') && !initialized.key?('tracker_ids') + self.trackers = Tracker.all + end + end + def identifier=(identifier) super unless identifier_frozen? end @@ -492,7 +510,7 @@ class Project < ActiveRecord::Base def enabled_module_names=(module_names) if module_names && module_names.is_a?(Array) - module_names = module_names.collect(&:to_s) + module_names = module_names.collect(&:to_s).reject(&:blank?) # remove disabled modules enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} # add new modules @@ -501,6 +519,11 @@ class Project < ActiveRecord::Base enabled_modules.clear end end + + # Returns an array of the enabled modules names + def enabled_module_names + enabled_modules.collect(&:name) + end # Returns an array of projects that are in this project's hierarchy # diff --git a/app/views/projects/new.html.erb b/app/views/projects/new.html.erb index c8a9c760..aad9c2c2 100644 --- a/app/views/projects/new.html.erb +++ b/app/views/projects/new.html.erb @@ -10,6 +10,8 @@ <%= l_or_humanize(m, :prefix => "project_module_") %> <% end %> +<%= hidden_field_tag 'enabled_modules[]', '' %> + <%= submit_tag l(:button_save) %> diff --git a/test/integration/api_test/projects_test.rb b/test/integration/api_test/projects_test.rb index c7433495..aaf40e1d 100644 --- a/test/integration/api_test/projects_test.rb +++ b/test/integration/api_test/projects_test.rb @@ -103,6 +103,7 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest context "POST /projects" do context "with valid parameters" do setup do + Setting.default_projects_modules = ['issue_tracking', 'repository'] @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}} end @@ -121,6 +122,7 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest project = Project.first(:order => 'id DESC') assert_equal 'API test', project.name assert_equal 'api-test', project.identifier + assert_equal ['issue_tracking', 'repository'], project.enabled_module_names assert_response :created assert_equal 'application/xml', @response.content_type diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index d57813ae..c77d7e61 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -60,6 +60,35 @@ class ProjectTest < ActiveSupport::TestCase assert_equal "eCookbook", @ecookbook.name end + def test_default_attributes + with_settings :default_projects_public => '1' do + assert_equal true, Project.new.is_public + assert_equal false, Project.new(:is_public => false).is_public + end + + with_settings :default_projects_public => '0' do + assert_equal false, Project.new.is_public + assert_equal true, Project.new(:is_public => true).is_public + end + + with_settings :sequential_project_identifiers => '1' do + assert !Project.new.identifier.blank? + assert Project.new(:identifier => '').identifier.blank? + end + + with_settings :sequential_project_identifiers => '0' do + assert Project.new.identifier.blank? + assert !Project.new(:identifier => 'test').blank? + end + + with_settings :default_projects_modules => ['issue_tracking', 'repository'] do + assert_equal ['issue_tracking', 'repository'], Project.new.enabled_module_names + end + + assert_equal Tracker.all, Project.new.trackers + assert_equal Tracker.find(1, 3), Project.new(:tracker_ids => [1, 3]).trackers + end + def test_update assert_equal "eCookbook", @ecookbook.name @ecookbook.name = "eCook"