From b8963144757e5033dd650f32d30e666735fc2600 Mon Sep 17 00:00:00 2001 From: rob mathews Date: Mon, 9 Aug 2021 14:24:56 -0500 Subject: [PATCH 1/3] add ruby 3.0 compatibility --- lib/active_model/dynamic_errors.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/active_model/dynamic_errors.rb b/lib/active_model/dynamic_errors.rb index e5447d4..6c67383 100644 --- a/lib/active_model/dynamic_errors.rb +++ b/lib/active_model/dynamic_errors.rb @@ -27,12 +27,12 @@ def full_messages messages.each do |m| if m =~ /^\^/ options[:default] = "%{message}" - full_messages << I18n.t(:"errors.dynamic_format", options.merge(:message => m[1..-1])) + full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => m[1..-1])) elsif m.is_a? Proc options[:default] = "%{message}" - full_messages << I18n.t(:"errors.dynamic_format", options.merge(:message => m.call(@base))) + full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => m.call(@base))) else - full_messages << I18n.t(:"errors.format", options.merge(:message => m)) + full_messages << I18n.t(:"errors.format", **options.merge(:message => m)) end end end @@ -44,4 +44,4 @@ def full_messages end require 'active_support/i18n' -I18n.load_path << File.dirname(__FILE__) + '/locale/en.yml' \ No newline at end of file +I18n.load_path << File.dirname(__FILE__) + '/locale/en.yml' From bd9d4d9d92e724340be5f13e8b239537f29e2d90 Mon Sep 17 00:00:00 2001 From: rob mathews Date: Tue, 10 Aug 2021 15:32:54 -0500 Subject: [PATCH 2/3] fix error hash deprecation --- lib/active_model/dynamic_errors.rb | 31 +++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/active_model/dynamic_errors.rb b/lib/active_model/dynamic_errors.rb index 6c67383..643ed20 100644 --- a/lib/active_model/dynamic_errors.rb +++ b/lib/active_model/dynamic_errors.rb @@ -13,27 +13,22 @@ class Errors def full_messages full_messages = [] - each do |attribute, messages| - messages = Array.wrap(messages) - next if messages.empty? - - if attribute == :base - messages.each {|m| full_messages << m } + each do |error| + if error.attribute == :base + full_messages << error.message else - attr_name = attribute.to_s.gsub('.', '_').humanize - attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) + attr_name = error.attribute.to_s.gsub('.', '_').humanize + attr_name = @base.class.human_attribute_name(error.attribute, :default => attr_name) options = { :default => "%{attribute} %{message}", :attribute => attr_name } - messages.each do |m| - if m =~ /^\^/ - options[:default] = "%{message}" - full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => m[1..-1])) - elsif m.is_a? Proc - options[:default] = "%{message}" - full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => m.call(@base))) - else - full_messages << I18n.t(:"errors.format", **options.merge(:message => m)) - end + if error.message =~ /^\^/ + options[:default] = "%{message}" + full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => error.message[1..-1])) + elsif error.message.is_a? Proc + options[:default] = "%{message}" + full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => error.message.call(@base))) + else + full_messages << I18n.t(:"errors.format", **options.merge(:message => error.message)) end end end From 548a6e861c8bd8ac1d21710af5770448af1d4848 Mon Sep 17 00:00:00 2001 From: Josh Welker Date: Wed, 8 May 2024 13:49:26 -0500 Subject: [PATCH 3/3] Rails 7 removed Enumerable.sum, so switched to using .map which is equivalent in this case --- lib/action_view/helpers/dynamic_form.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/action_view/helpers/dynamic_form.rb b/lib/action_view/helpers/dynamic_form.rb index f127ff9..2cb178d 100644 --- a/lib/action_view/helpers/dynamic_form.rb +++ b/lib/action_view/helpers/dynamic_form.rb @@ -221,7 +221,7 @@ def error_messages_for(*params) message = options.include?(:message) ? options[:message] : locale.t(:body) - error_messages = objects.sum do |object| + error_messages = objects.map do |object| object.errors.full_messages.map do |msg| content_tag(:li, msg) end