diff --git a/app/models/project.rb b/app/models/project.rb index 7ce0051ed..e4db6d867 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -287,10 +287,14 @@ class Project < ActiveRecord::Base end def enabled_module_names=(module_names) - enabled_modules.clear - module_names = [] unless module_names && module_names.is_a?(Array) - module_names.each do |name| - enabled_modules << EnabledModule.new(:name => name.to_s) + if module_names && module_names.is_a?(Array) + module_names = module_names.collect(&:to_s) + # remove disabled modules + enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} + # add new modules + module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)} + else + enabled_modules.clear end end diff --git a/test/unit/project_test.rb b/test/unit/project_test.rb index 94177f433..27cff13d6 100644 --- a/test/unit/project_test.rb +++ b/test/unit/project_test.rb @@ -18,7 +18,9 @@ require File.dirname(__FILE__) + '/../test_helper' class ProjectTest < Test::Unit::TestCase - fixtures :projects, :issues, :issue_statuses, :journals, :journal_details, :users, :members, :roles, :projects_trackers, :trackers, :boards + fixtures :projects, :enabled_modules, + :issues, :issue_statuses, :journals, :journal_details, + :users, :members, :roles, :projects_trackers, :trackers, :boards def setup @ecookbook = Project.find(1) @@ -218,4 +220,17 @@ class ProjectTest < Test::Unit::TestCase Project.delete_all assert_nil Project.next_identifier end + + def test_enabled_module_names_should_not_recreate_enabled_modules + project = Project.find(1) + # Remove one module + modules = project.enabled_modules.slice(0..-2) + assert modules.any? + assert_difference 'EnabledModule.count', -1 do + project.enabled_module_names = modules.collect(&:name) + end + project.reload + # Ids should be preserved + assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort + end end