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
This commit is contained in:
Jean-Philippe Lang 2010-12-03 16:15:16 +00:00
parent f6c633212a
commit 9284a32c9a
5 changed files with 58 additions and 7 deletions

View File

@ -68,11 +68,6 @@ class ProjectsController < ApplicationController
@issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
@trackers = Tracker.all @trackers = Tracker.all
@project = Project.new(params[:project]) @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 end
def create def create
@ -80,7 +75,7 @@ class ProjectsController < ApplicationController
@trackers = Tracker.all @trackers = Tracker.all
@project = Project.new(params[:project]) @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 if validate_parent_id && @project.save
@project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') @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 # Add current user as a project member if he is not admin

View File

@ -84,6 +84,24 @@ class Project < ActiveRecord::Base
named_scope :all_public, { :conditions => { :is_public => true } } named_scope :all_public, { :conditions => { :is_public => true } }
named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } 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) def identifier=(identifier)
super unless identifier_frozen? super unless identifier_frozen?
end end
@ -492,7 +510,7 @@ class Project < ActiveRecord::Base
def enabled_module_names=(module_names) def enabled_module_names=(module_names)
if module_names && module_names.is_a?(Array) 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 # remove disabled modules
enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
# add new modules # add new modules
@ -501,6 +519,11 @@ class Project < ActiveRecord::Base
enabled_modules.clear enabled_modules.clear
end end
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 # Returns an array of projects that are in this project's hierarchy
# #

View File

@ -10,6 +10,8 @@
<%= l_or_humanize(m, :prefix => "project_module_") %> <%= l_or_humanize(m, :prefix => "project_module_") %>
</label> </label>
<% end %> <% end %>
<%= hidden_field_tag 'enabled_modules[]', '' %>
</fieldset> </fieldset>
<%= submit_tag l(:button_save) %> <%= submit_tag l(:button_save) %>

View File

@ -103,6 +103,7 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
context "POST /projects" do context "POST /projects" do
context "with valid parameters" do context "with valid parameters" do
setup do setup do
Setting.default_projects_modules = ['issue_tracking', 'repository']
@parameters = {:project => {:name => 'API test', :identifier => 'api-test'}} @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
end end
@ -121,6 +122,7 @@ class ApiTest::ProjectsTest < ActionController::IntegrationTest
project = Project.first(:order => 'id DESC') project = Project.first(:order => 'id DESC')
assert_equal 'API test', project.name assert_equal 'API test', project.name
assert_equal 'api-test', project.identifier assert_equal 'api-test', project.identifier
assert_equal ['issue_tracking', 'repository'], project.enabled_module_names
assert_response :created assert_response :created
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type

View File

@ -60,6 +60,35 @@ class ProjectTest < ActiveSupport::TestCase
assert_equal "eCookbook", @ecookbook.name assert_equal "eCookbook", @ecookbook.name
end 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 def test_update
assert_equal "eCookbook", @ecookbook.name assert_equal "eCookbook", @ecookbook.name
@ecookbook.name = "eCook" @ecookbook.name = "eCook"