From 36bb955f7c09b5021a300bcaed14fa2de16ccf1d Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Fri, 27 Feb 2015 17:19:26 +0100 Subject: [PATCH] add custom value errors to customizable - allows to relate error messages to their custom field - allows to use errors.full_message correctly for custom values - might break specs... we will see that soon --- config/initializers/10-patches.rb | 47 ++++++------------- lib/api/errors/validation.rb | 2 +- .../lib/acts_as_customizable.rb | 14 +++++- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/config/initializers/10-patches.rb b/config/initializers/10-patches.rb index 71a1f1003d..44634c046e 100644 --- a/config/initializers/10-patches.rb +++ b/config/initializers/10-patches.rb @@ -73,39 +73,22 @@ end module ActiveModel class Errors - def full_messages - full_messages = [] - - @messages.each_key do |attribute| - @messages[attribute].each do |message| - next unless message - - if attribute == :base - full_messages << message - elsif attribute == :custom_values - # Replace the generic "custom values is invalid" - # with the errors on custom values - @base.custom_values.each do |value| - full_messages += value.errors.map do |_, message| - I18n.t(:"errors.format", - default: '%{attribute} %{message}', - attribute: value.custom_field.name, - message: message - ) - end - end - else - attr_name = attribute.to_s.gsub('.', '_').humanize - attr_name = @base.class.human_attribute_name(attribute, default: attr_name) - full_messages << I18n.t(:"errors.format", - default: '%{attribute} %{message}', - attribute: attr_name, - message: message - ) - end - end + def full_message(attribute, message) + return message if attribute == :base + + attr_name_override = nil + match = /\Acustom_field_(?\d+)\z/.match(attribute) + if match + attr_name_override = CustomField.find_by_id(match[:id]).name end - full_messages + + attr_name = attribute.to_s.gsub('.', '_').humanize + attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) + I18n.t(:"errors.format", { + :default => "%{attribute} %{message}", + :attribute => attr_name_override || attr_name, + :message => message + }) end end end diff --git a/lib/api/errors/validation.rb b/lib/api/errors/validation.rb index a0e59f5c47..2c2eb7e05a 100644 --- a/lib/api/errors/validation.rb +++ b/lib/api/errors/validation.rb @@ -47,7 +47,7 @@ module API end end - hash[attribute] = ::API::Errors::Validation.new(messages) + hash[attribute.to_s.camelize(:lower)] = ::API::Errors::Validation.new(messages) end end diff --git a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb index a80343b2b9..0ceb99096e 100644 --- a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb +++ b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb @@ -44,8 +44,8 @@ module Redmine 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: -> (customized) { customized.custom_field_values_changed? } + validate :validate_custom_values, on: :update, + if: -> (customized) { customized.custom_field_values_changed? } send :include, Redmine::Acts::Customizable::InstanceMethods # Save custom values when saving the customized object after_save :save_custom_field_values @@ -116,6 +116,16 @@ module Redmine custom_values.each { |cv| cv.destroy unless custom_field_values.include?(cv) } end + def validate_custom_values + custom_values.each do |custom_value| + unless custom_value.valid? + custom_value.errors.each do |_, message| + errors.add("custom_field_#{custom_value.custom_field.id}".to_sym, message) + end + end + end + end + module ClassMethods end end