]> source.dussan.org Git - redmine.git/commitdiff
Ability to uncheck "Multiple values" for existing custom fields (#12251).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 12 Jan 2013 09:12:09 +0000 (09:12 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 12 Jan 2013 09:12:09 +0000 (09:12 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11167 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/custom_field.rb
app/views/custom_fields/_form.html.erb
test/unit/custom_field_test.rb

index c5a1ca6efd20d6a63275fbc2d672d1fecb737131..3cd4c4431cb14f22312cce0d499f337f924ea5a2 100644 (file)
@@ -29,6 +29,7 @@ class CustomField < ActiveRecord::Base
 
   validate :validate_custom_field
   before_validation :set_searchable
+  after_save :handle_multiplicity_change
 
   scope :sorted, lambda { order("#{table_name}.position ASC") }
 
@@ -335,4 +336,20 @@ class CustomField < ActiveRecord::Base
     end
     errs
   end
+
+  # Removes multiple values for the custom field after setting the multiple attribute to false
+  # We kepp the value with the highest id for each customized object
+  def handle_multiplicity_change
+    if !new_record? && multiple_was && !multiple
+      ids = custom_values.
+        where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" + 
+          " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" + 
+          " AND cve.id > #{CustomValue.table_name}.id)").
+        pluck(:id)
+
+      if ids.any?
+        custom_values.where(:id => ids).delete_all
+      end
+    end
+  end
 end
index d3fd1de69093cd845c05c50222d74f93233ebf37..50075775bcc9c891825eb8b1dd631600c274e6dd 100644 (file)
@@ -5,7 +5,7 @@
 <p><%= f.select :field_format, custom_field_formats_for_select(@custom_field), {}, :disabled => !@custom_field.new_record? %></p>
 
 <% if @custom_field.format_in? 'list', 'user', 'version' %>
-<p><%= f.check_box :multiple, :disabled => @custom_field.multiple && !@custom_field.new_record? %></p>
+<p><%= f.check_box :multiple %></p>
 <% end %>
 
 <% unless @custom_field.format_in? 'list', 'bool', 'date', 'user', 'version' %>
index c3e9962f236e06ed60fd11ef63042bad6645d158..ee78fb23ff4df4483e6d93b7964843e7b1b3cb52 100644 (file)
@@ -209,6 +209,24 @@ class CustomFieldTest < ActiveSupport::TestCase
     assert !f.valid_field_value?(['value1', 'abc'])
   end
 
+  def test_changing_multiple_to_false_should_delete_multiple_values
+    field = ProjectCustomField.create!(:name => 'field', :field_format => 'list', :multiple => 'true', :possible_values => ['field1', 'field2'])
+    other = ProjectCustomField.create!(:name => 'other', :field_format => 'list', :multiple => 'true', :possible_values => ['other1', 'other2'])
+
+    item_with_multiple_values = Project.generate!(:custom_field_values => {field.id => ['field1', 'field2'], other.id => ['other1', 'other2']})
+    item_with_single_values = Project.generate!(:custom_field_values => {field.id => ['field1'], other.id => ['other2']})
+
+    assert_difference 'CustomValue.count', -1 do
+      field.multiple = false
+      field.save!
+    end
+
+    item_with_multiple_values = Project.find(item_with_multiple_values.id)
+    assert_kind_of String, item_with_multiple_values.custom_field_value(field)
+    assert_kind_of Array, item_with_multiple_values.custom_field_value(other)
+    assert_equal 2, item_with_multiple_values.custom_field_value(other).size
+  end
+
   def test_value_class_should_return_the_class_used_for_fields_values
     assert_equal User, CustomField.new(:field_format => 'user').value_class
     assert_equal Version, CustomField.new(:field_format => 'version').value_class