From 8b85f078da0d7b7bd7fe30a8d16180742684a0c1 Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Tue, 6 Dec 2011 14:48:03 +0100 Subject: [PATCH] include custom_values into the full error messages directly: * delete generic error message for custom values --- config/initializers/10-patches.rb | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/config/initializers/10-patches.rb b/config/initializers/10-patches.rb index 50047e197e..b14f120847 100644 --- a/config/initializers/10-patches.rb +++ b/config/initializers/10-patches.rb @@ -30,28 +30,36 @@ module ActiveRecord def full_messages(options = {}) full_messages = [] - @errors.each_key do |attr| - @errors[attr].each do |message| + errors = errors_including_custom_values + + errors.each_key do |attr| + errors[attr].each do |message| next unless message if attr == "base" full_messages << message - elsif attr == "custom_values" - # Replace the generic "custom values is invalid" - # with the errors on custom values - @base.custom_values.each do |value| - value.errors.each do |attr, msg| - full_messages << value.custom_field.name + ' ' + msg - end - end else - attr_name = @base.class.human_attribute_name(attr) + attr_name = @base.respond_to?(attr) ? @base.class.human_attribute_name(attr) : attr full_messages << attr_name + ' ' + message.to_s end end end full_messages end + + def errors_including_custom_values + errors = @errors.dup + + if errors["custom_values"].present? + @base.custom_values.select{ |v| v.errors.length > 0 }.each do |value| + errors[value.custom_field.name] = value.errors.instance_variable_get("@errors").values + end + + errors.delete("custom_values") + end + + errors + end end end