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 bug on added? method #31117
fix bug on added? method #31117
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kamipo (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
message = message.call if message.respond_to?(:call) | ||
message = normalize_message(attribute, message, options) | ||
self[attribute].include? message | ||
if message.is_a? Symbol |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this conditional and always read from the details
hash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would result in breaking changes, right?
The details
hash doesn't contain the message strings (normalised messages) and such a change wouldn't allow to call added?
using the message string anymore.
I would expect, in my example test, that a call to person.errors.added?(:name, 'is wrong')
is still possible and returns true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah, in the case for asserting for the string correspondent to the symbol that would break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you fix the rubocop violations?
fix rubocop issues
b17d8d5
to
15cb4ef
Compare
Rubocop fixed and commits squashed 👍 |
@coorasse @rafaelfranca Bumped into an issue with this today. A minor one but it took me a solid chunk of time to figure out what was going wrong. I have the following test: %w[alpha beta charlie].each do |attr|
test "should be invalid without #{attr}" do
package = Package.new
package[attr] = nil
assert_invalid package, attr => :blank
end
end and the following test helper: def assert_invalid(record, options = {})
assert_predicate record, :invalid?
options.each do |attribute, message|
assert record.errors.added?(attribute, message), "Expected #{attribute} to have the following error: #{message}"
end
end These started failing when upgrading to 5.2.0. I finally tracked it down to this change. The issue is that when This causes The fix was pretty simple: %w[name brand_name theme_color].each do |attr|
test "should be invalid without #{attr}" do
package = BrandingPackage.new
package[attr] = nil
assert_invalid package, attr.to_sym => :blank
end
end But, it raised the question of what the expected behavior was for handling string and symbol differences in the details hash? Should Is this a bug? If so, I'm happy to submit a PR to fix it. If it's not a bug, it may be worth noting that it's a breaking change. [edit] Fwiw, it looks like details is always a symbol, so it's a very simple fix. |
Yeah, it is a bug, we look forward to coming a PR to fix it from you. |
Summary
This Pull Requests fixes an issue with the
added?
method ofActiveModel::Errors
.When looking for an error by symbol, the method is returning true even if the error is not present. This happens because another error, with a different symbol, but same message, is present.