@@ -29,6 +29,7 class CustomField < ActiveRecord::Base | |||
|
29 | 29 | |
|
30 | 30 | validate :validate_custom_field |
|
31 | 31 | before_validation :set_searchable |
|
32 | after_save :handle_multiplicity_change | |
|
32 | 33 | |
|
33 | 34 | scope :sorted, lambda { order("#{table_name}.position ASC") } |
|
34 | 35 | |
@@ -335,4 +336,20 class CustomField < ActiveRecord::Base | |||
|
335 | 336 | end |
|
336 | 337 | errs |
|
337 | 338 | end |
|
339 | ||
|
340 | # Removes multiple values for the custom field after setting the multiple attribute to false | |
|
341 | # We kepp the value with the highest id for each customized object | |
|
342 | def handle_multiplicity_change | |
|
343 | if !new_record? && multiple_was && !multiple | |
|
344 | ids = custom_values. | |
|
345 | where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" + | |
|
346 | " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" + | |
|
347 | " AND cve.id > #{CustomValue.table_name}.id)"). | |
|
348 | pluck(:id) | |
|
349 | ||
|
350 | if ids.any? | |
|
351 | custom_values.where(:id => ids).delete_all | |
|
352 | end | |
|
353 | end | |
|
354 | end | |
|
338 | 355 | end |
@@ -5,7 +5,7 | |||
|
5 | 5 | <p><%= f.select :field_format, custom_field_formats_for_select(@custom_field), {}, :disabled => !@custom_field.new_record? %></p> |
|
6 | 6 | |
|
7 | 7 | <% if @custom_field.format_in? 'list', 'user', 'version' %> |
|
8 | <p><%= f.check_box :multiple, :disabled => @custom_field.multiple && !@custom_field.new_record? %></p> | |
|
8 | <p><%= f.check_box :multiple %></p> | |
|
9 | 9 | <% end %> |
|
10 | 10 | |
|
11 | 11 | <% unless @custom_field.format_in? 'list', 'bool', 'date', 'user', 'version' %> |
@@ -209,6 +209,24 class CustomFieldTest < ActiveSupport::TestCase | |||
|
209 | 209 | assert !f.valid_field_value?(['value1', 'abc']) |
|
210 | 210 | end |
|
211 | 211 | |
|
212 | def test_changing_multiple_to_false_should_delete_multiple_values | |
|
213 | field = ProjectCustomField.create!(:name => 'field', :field_format => 'list', :multiple => 'true', :possible_values => ['field1', 'field2']) | |
|
214 | other = ProjectCustomField.create!(:name => 'other', :field_format => 'list', :multiple => 'true', :possible_values => ['other1', 'other2']) | |
|
215 | ||
|
216 | item_with_multiple_values = Project.generate!(:custom_field_values => {field.id => ['field1', 'field2'], other.id => ['other1', 'other2']}) | |
|
217 | item_with_single_values = Project.generate!(:custom_field_values => {field.id => ['field1'], other.id => ['other2']}) | |
|
218 | ||
|
219 | assert_difference 'CustomValue.count', -1 do | |
|
220 | field.multiple = false | |
|
221 | field.save! | |
|
222 | end | |
|
223 | ||
|
224 | item_with_multiple_values = Project.find(item_with_multiple_values.id) | |
|
225 | assert_kind_of String, item_with_multiple_values.custom_field_value(field) | |
|
226 | assert_kind_of Array, item_with_multiple_values.custom_field_value(other) | |
|
227 | assert_equal 2, item_with_multiple_values.custom_field_value(other).size | |
|
228 | end | |
|
229 | ||
|
212 | 230 | def test_value_class_should_return_the_class_used_for_fields_values |
|
213 | 231 | assert_equal User, CustomField.new(:field_format => 'user').value_class |
|
214 | 232 | assert_equal Version, CustomField.new(:field_format => 'version').value_class |
General Comments 0
You need to be logged in to leave comments.
Login now