Skip to content

Commit

Permalink
Fix deprecations on ActiveModel::Errors (#3869)
Browse files Browse the repository at this point in the history
* Fix deprecation: ActiveModel::Errors#to_xml

* Fix: Enumerating ActiveModel::Errors as a hash
  • Loading branch information
jlledom authored Aug 28, 2024
1 parent df68b4d commit 987cd2c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/admin/api/signups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions app/lib/three_scale/errors_to_xml.rb
Original file line number Diff line number Diff line change
@@ -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
# # =>
# # <?xml version=\"1.0\" encoding=\"UTF-8\"?>
# # <errors>
# # <error>name can't be blank</error>
# # <error>name must be specified</error>
# # </errors>
def to_xml(options = {})
to_a.to_xml({ root: "errors", skip_types: true }.merge!(options))
end
end
end
5 changes: 5 additions & 0 deletions config/initializers/active_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

Rails.application.config.to_prepare do
ActiveModel::Errors.prepend ThreeScale::ErrorsToXml
end

0 comments on commit 987cd2c

Please sign in to comment.