OSDN Git Service

Moves project attributes default assignments from ProjectsController#new to the model...
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 3 Dec 2010 16:15:16 +0000 (16:15 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 3 Dec 2010 16:15:16 +0000 (16:15 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4460 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/projects_controller.rb
app/models/project.rb
app/views/projects/new.html.erb
test/integration/api_test/projects_test.rb
test/unit/project_test.rb

index d94dec1..8ec9472 100644 (file)
@@ -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
index 6eb41cc..d68958e 100644 (file)
@@ -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
   #
index c8a9c76..aad9c2c 100644 (file)
@@ -10,6 +10,8 @@
     <%= l_or_humanize(m, :prefix => "project_module_") %>
     </label>
 <% end %>
+<%= hidden_field_tag 'enabled_modules[]', '' %>
+
 </fieldset>
 
 <%= submit_tag l(:button_save) %>
index c743349..aaf40e1 100644 (file)
@@ -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
index d57813a..c77d7e6 100644 (file)
@@ -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"