diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-01-28 11:16:58 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-01-28 11:16:58 +0000 |
commit | 83e7ee6729cd0207219719556b3e2aed0a33f360 (patch) | |
tree | 8fa089641bcaa6baec5aee080431e4e38c731a51 /vendor | |
parent | d4d27bd2d8b57e5d2597b75fbb7b963b6c3e37f7 (diff) | |
download | redmine-83e7ee6729cd0207219719556b3e2aed0a33f360.tar.gz redmine-83e7ee6729cd0207219719556b3e2aed0a33f360.zip |
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8717 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'vendor')
-rw-r--r-- | vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb index f6c036a1f..943cc441c 100644 --- a/vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb +++ b/vendor/plugins/acts_as_customizable/lib/acts_as_customizable.rb @@ -1,5 +1,5 @@ # Redmine - project management software -# Copyright (C) 2006-2011 Jean-Philippe Lang +# Copyright (C) 2006-2012 Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -30,12 +30,10 @@ module Redmine has_many :custom_values, :as => :customized, :include => :custom_field, :order => "#{CustomField.table_name}.position", - :dependent => :delete_all - before_validation { |customized| customized.custom_field_values if customized.new_record? } - # Trigger validation only if custom values were changed - validates_associated :custom_values, :on => :update, :if => Proc.new { |customized| customized.custom_field_values_changed? } + :dependent => :delete_all, + :validate => false send :include, Redmine::Acts::Customizable::InstanceMethods - # Save custom values when saving the customized object + validate :validate_custom_field_values after_save :save_custom_field_values end end @@ -66,15 +64,28 @@ module Redmine # Sets the values of the object's custom fields # values is a hash like {'1' => 'foo', 2 => 'bar'} def custom_field_values=(values) - @custom_field_values_changed = true values = values.stringify_keys - custom_field_values.each do |custom_value| - custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s) - end if values.is_a?(Hash) + + custom_field_values.each do |custom_field_value| + key = custom_field_value.custom_field_id.to_s + if values.has_key?(key) + value = values[key] + custom_field_value.value = value + end + end + @custom_field_values_changed = true end def custom_field_values - @custom_field_values ||= available_custom_fields.collect { |x| custom_values.detect { |v| v.custom_field == x } || custom_values.build(:customized => self, :custom_field => x, :value => nil) } + @custom_field_values ||= available_custom_fields.collect do |field| + x = CustomFieldValue.new + x.custom_field = field + x.customized = self + cv = custom_values.detect { |v| v.custom_field == field } + cv ||= custom_values.build(:customized => self, :custom_field => field, :value => nil) + x.value = cv.value + x + end end def visible_custom_field_values @@ -90,18 +101,34 @@ module Redmine custom_values.detect {|v| v.custom_field_id == field_id } end + def custom_field_value(c) + field_id = (c.is_a?(CustomField) ? c.id : c.to_i) + custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value) + end + + def validate_custom_field_values + if new_record? || custom_field_values_changed? + custom_field_values.each(&:validate_value) + end + end + def save_custom_field_values - self.custom_values = custom_field_values - custom_field_values.each(&:save) + target_custom_values = [] + custom_field_values.each do |custom_field_value| + target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field} + target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field) + target.value = custom_field_value.value + target_custom_values << target + end + self.custom_values = target_custom_values + custom_values.each(&:save) @custom_field_values_changed = false - @custom_field_values = nil + true end def reset_custom_values! @custom_field_values = nil @custom_field_values_changed = true - values = custom_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h} - custom_values.each {|cv| cv.destroy unless custom_field_values.include?(cv)} end module ClassMethods |