Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rails 6 active model error deprecations #22

Merged

Conversation

AlessSala
Copy link
Contributor

@AlessSala AlessSala commented Jun 10, 2021

This pull request aims to fix the following issues raised using Rails 6.1.x:

  • "ActiveModel::Errors.each" deprecation warning
  • 'is invalid' error deletion from ActiveModel::Errors raises "FrozenError: can't modify frozen Array: ["is invalid"]"
  • While testing: "Failure/Error: DatabaseCleaner.cleaning { example.run } ActiveRecord::ConnectionNotEstablished: No connection pool for 'ActiveRecord::Base' found."
    Please check the commit messages for an in-depth explanation: sources and links are also there.
    Rspec test run smoothly.
    Thank you for your useful gem - feel free to scold me if anything sounds wrong to you.

Greetings, Alessandro.

AlessSala added 3 commits June 10, 2021 14:34
This fix resolves the following warning :
"
DEPRECATION WARNING: Enumerating ActiveModel::Errors as a hash has been deprecated.
 In Rails 6.1, `errors` is an array of Error objects,
 therefore it should be accessed by a block with a single block
 parameter like this:

 person.errors.each do |error|
   attribute = error.attribute
   message = error.message
 end

 You are passing a block expecting two parameters,
 so the old hash behavior is simulated. As this is deprecated,
 **this will result in an ArgumentError in Rails 6.2**.
"

see also https://api.rubyonrails.org/v6.1.3.2/classes/ActiveModel/Errors.html#method-i-each
This fix resolves the following error:
"
FrozenError:
       can't modify frozen Array: ["is invalid"]
"

The ActiveModel::Error is not a simple Hash anymore: now is a first citizen Object.
See also:
* https://api.rubyonrails.org/v6.1.3.2/classes/ActiveModel/Error.html
* https://blog.saeloun.com/2020/06/17/rails-active-model-errors.html#after
This fix resolves the following error during the tests execution:
"
Failure/Error: DatabaseCleaner.cleaning { example.run }

          ActiveRecord::ConnectionNotEstablished:
            No connection pool for 'ActiveRecord::Base' found.
"

Change the original gem to a more specific one: database_cleaner-mongoid
DBCleaner configuration moved to a distinct file

see https://github.com/DatabaseCleaner/database_cleaner-mongoid
@mjstallard
Copy link

Hey there @knovoselic - assuming you're still a maintainer for this repo, would it be possible to get this merged and released? Given Rails 7 is out, my assumption is that those deprecation warnings will now turn into errors. It'd be great to be able to keep using this helpful gem!

Thanks!

@AlessSala
Copy link
Contributor Author

Glad to see some other user of this gem! 😄

@rpalackas
Copy link
Collaborator

@AlessSala sorry it took so long for us to look at this we haven't touched this gem in ages. @knovoselic this looks good to me. Only thing we might want to consider is adding a rails version requirement in mongoid-embedded-errors.gemspec?

@rpalackas
Copy link
Collaborator

Playing around with different versions we need to add a dependency of activemodel >= 6.1

@knovoselic
Copy link
Collaborator

@AlessSala thanks for the PR and so sorry for the long wait. We'll try to do better from now!

@knovoselic knovoselic merged commit 11e9a5b into glooko:master Aug 25, 2022
@AlessSala AlessSala deleted the fix-rails-6-active-model-error-deprecations branch August 26, 2022 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants