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 bug on added? method #31117

Merged
merged 1 commit into from Nov 13, 2017
Merged

fix bug on added? method #31117

merged 1 commit into from Nov 13, 2017

Conversation

coorasse
Copy link
Contributor

Summary

This Pull Requests fixes an issue with the added? method of ActiveModel::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.

@rails-bot
Copy link

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
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

@coorasse
Copy link
Contributor Author

r? @rafaelfranca

@rails-bot rails-bot assigned rafaelfranca and unassigned kamipo Nov 13, 2017
Copy link
Member

@rafaelfranca rafaelfranca left a 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
@coorasse
Copy link
Contributor Author

Rubocop fixed and commits squashed 👍

@jhubert
Copy link
Contributor

jhubert commented Jul 13, 2018

@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 attr => :blank is passed to the helper the value of attr is a string.

This causes self.details[attribute] to return nothing, compared to self.details[attribute.to_sym] which returns the expected error.

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 package.added?("name", :blank) not be expected to work? It does in 5.1 seemingly because self[attribute.to_s] and self[attribute.to_sym] are equivalent.

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.

@kamipo
Copy link
Member

kamipo commented Jul 14, 2018

Yeah, it is a bug, we look forward to coming a PR to fix it from you.

@jhubert
Copy link
Contributor

jhubert commented Jul 14, 2018

Thanks @kamipo! PR #33361 submitted.

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.

None yet

5 participants