From 987cd2caf751adbcdfcac0c121c9275cfa4fbb91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Lled=C3=B3?= Date: Wed, 28 Aug 2024 10:27:27 +0200 Subject: [PATCH] Fix deprecations on `ActiveModel::Errors` (#3869) * Fix deprecation: ActiveModel::Errors#to_xml * Fix: Enumerating ActiveModel::Errors as a hash --- .../admin/api/signups_controller.rb | 4 +-- app/lib/three_scale/errors_to_xml.rb | 25 +++++++++++++++++++ config/initializers/active_model.rb | 5 ++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 app/lib/three_scale/errors_to_xml.rb create mode 100644 config/initializers/active_model.rb diff --git a/app/controllers/admin/api/signups_controller.rb b/app/controllers/admin/api/signups_controller.rb index bab13db282..bbb9f70861 100644 --- a/app/controllers/admin/api/signups_controller.rb +++ b/app/controllers/admin/api/signups_controller.rb @@ -25,8 +25,8 @@ def check_creation_errors # and not respond with pathetic error raise ActiveRecord::RecordNotFound if @signup_result.errors[:plans].present? - @signup_result.user.errors.each do |attr, error| - @signup_result.account.errors.add(attr, error) + @signup_result.user.errors.each do |error| + @signup_result.account.errors.add(error.attribute, error.message) end end diff --git a/app/lib/three_scale/errors_to_xml.rb b/app/lib/three_scale/errors_to_xml.rb new file mode 100644 index 0000000000..800feabee0 --- /dev/null +++ b/app/lib/three_scale/errors_to_xml.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module ThreeScale + module ErrorsToXml + # This was deprecated with no replacement in rails 6. Because allegedly + # nobody uses it. So we have to keep a copy until we completely stop + # supporting XMLs in our APIs. + # https://github.com/rails/rails/pull/32313 + # + # Returns an xml formatted representation of the Errors hash. + # + # person.errors.add(:name, :blank, message: "can't be blank") + # person.errors.add(:name, :not_specified, message: "must be specified") + # person.errors.to_xml + # # => + # # + # # + # # name can't be blank + # # name must be specified + # # + def to_xml(options = {}) + to_a.to_xml({ root: "errors", skip_types: true }.merge!(options)) + end + end +end diff --git a/config/initializers/active_model.rb b/config/initializers/active_model.rb new file mode 100644 index 0000000000..3c41923267 --- /dev/null +++ b/config/initializers/active_model.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +Rails.application.config.to_prepare do + ActiveModel::Errors.prepend ThreeScale::ErrorsToXml +end