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

Support Rails 6.1 #88

Merged
merged 4 commits into from
Mar 19, 2021
Merged

Support Rails 6.1 #88

merged 4 commits into from
Mar 19, 2021

Conversation

y-yagi
Copy link
Contributor

@y-yagi y-yagi commented Feb 10, 2021

The behavior of Active Model errors was changed by rails/rails#32313.
This PR has fixed to accommodate that change.

Fixes #86.

In Rails 6.1, `messages` is an instance of `ActiveModel::DeprecationHandlingMessageArray`.
So can't handle with the Reform's `filter_for`.
Copy link
Member

@apotonick apotonick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty pretty cool @y-yagi thanks so much for this effort! ❤️

@@ -143,7 +143,8 @@ def to_s

def add(key, error_text)
# use rails magic to get the correct error_text and make sure we still update details and fields
text = @amv_errors.add(key, error_text)
error = @amv_errors.add(key, error_text)
error = [error.message] if error.respond_to?(:message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's happening when error doesn't have a message method? Is that Rails < 6.1? I'd refrain from introducing #respond_to in TRB gems. https://youtu.be/mjsnd8dJbew?t=1113

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, in the case of Rails < 6.1, error doesn't have a message method and causes an error.

NoMethodError: undefined method `message' for ["error_text"]:Array
    lib/reform/form/active_model/validations.rb:147:in `add' 

I'd refrain from introducing #respond_to in TRB gems

Oh, I see. So what do you think about checking Rails' version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated to use is_a? instead of respond_to?.

Now `ActiveModel::Errors#add` returns an instance of
`ActiveModel::Error`. So can't use it as is.
`ActiveModel::Errors#messages` now only returns error attributes.
@jamesbebbington
Copy link

Hi, is there anything blocking this PR from being merged and released? I would very much like to upgrade our app to rails 6.1 and this is the final blocker. Thanks.

@samstickland
Copy link
Collaborator

Yup. This is the thing that is also preventing us from upgrading to Rails 6.1 as well

@apotonick apotonick merged commit 26d3388 into trailblazer:master Mar 19, 2021
@samstickland
Copy link
Collaborator

Awesome, thanks!

@jamesbebbington
Copy link

Yup, we're now happily upgraded to rails 6.1. Many thanks @y-yagi @seuros and @apotonick!

nested_errors = v.select { |attr_key, val| attr_key.is_a?(Integer) && val.is_a?(Array) && val.any? }
v = nested_errors.to_a if nested_errors.any?
end
v.is_a?(Array) || v.class.to_s == "ActiveModel::DeprecationHandlingMessageArray"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did similar as a monkey patch to get our 6.1 upgrade in. Curious why v.class.to_s vs v.is_a?(ActiveModel::DeprecationHandlingMessageArray)?

is_a?() seems more direct than stringifying the class name and comparing the string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v.is_a?(ActiveModel::DeprecationHandlingMessageArray doesn' work on Rails <= 6.0. It will raise NameError.

@y-yagi y-yagi deleted the support-rails61 branch March 20, 2021 03:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reform::Rails doesn't work with Rails 6.1 well
6 participants