From f5ebd47751b3fed859d7e9b992d9090c098123ad Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 28 Dec 2021 00:23:47 +0000 Subject: [PATCH] Replace customisations with explicit patch in an initializer See previous commit. This isn't nice, but it does the same job as the gem did. We can also update this code now as Rails changes (the gem was incompatible with Rails 7). --- config/initializers/custom_errors.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 config/initializers/custom_errors.rb diff --git a/config/initializers/custom_errors.rb b/config/initializers/custom_errors.rb new file mode 100644 index 0000000000..a7396eb59b --- /dev/null +++ b/config/initializers/custom_errors.rb @@ -0,0 +1,22 @@ +# This patch customises ActiveModel error messages, as previously handled by the custom_error_messages gem +# See: https://github.com/jeremydurham/custom-err-msg + +module ActiveModel + class Error + def self.full_message(attribute, message, base) + return message if attribute == :base + + attr_name = attribute.to_s.tr(".", "_").humanize + attr_name = base.class.human_attribute_name(attribute, { + default: attr_name, + base: base, + }) + + if message.start_with?("^") + I18n.t("errors.format.full_message", default: "%{message}", message: message[1..-1], attribute: attr_name) + else + I18n.t("errors.format", default: "%{attribute} %{message}", message: message, attribute: attr_name) + end + end + end +end