diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2013-01-12 09:12:09 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2013-01-12 09:12:09 +0000 |
commit | 5c1039a69e93fe815d4254a8d0edb4f073692b42 (patch) | |
tree | 3c7ab664cb254cf008354e717922cdf4fdaacad8 /app | |
parent | 44ceb513ed1707eef2d3f66d4639207df5585e1f (diff) | |
download | redmine-5c1039a69e93fe815d4254a8d0edb4f073692b42.tar.gz redmine-5c1039a69e93fe815d4254a8d0edb4f073692b42.zip |
Ability to uncheck "Multiple values" for existing custom fields (#12251).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11167 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r-- | app/models/custom_field.rb | 17 | ||||
-rw-r--r-- | app/views/custom_fields/_form.html.erb | 2 |
2 files changed, 18 insertions, 1 deletions
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index c5a1ca6ef..3cd4c4431 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -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 diff --git a/app/views/custom_fields/_form.html.erb b/app/views/custom_fields/_form.html.erb index d3fd1de69..50075775b 100644 --- a/app/views/custom_fields/_form.html.erb +++ b/app/views/custom_fields/_form.html.erb @@ -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' %> |