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
This commit is contained in:
Jean-Philippe Lang
2012-01-28 11:16:58 +00:00
parent d4d27bd2d8
commit 83e7ee6729
11 changed files with 249 additions and 193 deletions
@@ -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