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
pull/2550/head
Jan Sandbrink 10 years ago
parent 28e0ec21c0
commit 36bb955f7c
  1. 47
      config/initializers/10-patches.rb
  2. 2
      lib/api/errors/validation.rb
  3. 14
      lib/plugins/acts_as_customizable/lib/acts_as_customizable.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_(?<id>\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

@ -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

@ -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

Loading…
Cancel
Save