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
Simplify and alias ActiveModel::Errors methods where possible #19021
Simplify and alias ActiveModel::Errors methods where possible #19021
Conversation
# Returns +true+ if no errors are found, +false+ otherwise. | ||
# If the error message is a string it can be empty. | ||
# | ||
# person.errors.full_messages # => ["name cannot be nil"] | ||
# person.errors.empty? # => false | ||
def empty? | ||
all? { |k, v| v && v.empty? && !v.is_a?(String) } | ||
size.zero? |
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 changes the behavior of the method when given a nil value.
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.
Can you provide an example?
I think that size must return an integer as it's a simple call on hash like: {}.values.size
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.
{ foo: nil }
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.
Not sure if I follow:
irb(main):014:0> {foo: nil}.all? { |k, v| v && v.empty? && !v.is_a?(String) }
=> false
irb(main):015:0> {foo: nil}.values.size.zero?
=> false
It returns the same result
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.
Sorry {foo: []}
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.
Our test coverage is not 100%. I'd find the commit that introduced it
before assuming it's safe to remove
On Fri, Feb 20, 2015, 2:56 PM Wojciech Wnętrzak notifications@github.com
wrote:
In activemodel/lib/active_model/errors.rb
#19021 (comment):# Returns +true+ if no errors are found, +false+ otherwise. # If the error message is a string it can be empty. # # person.errors.full_messages # => ["name cannot be nil"] # person.errors.empty? # => false def empty?
all? { |k, v| v && v.empty? && !v.is_a?(String) }
size.zero?
Actually there is also a flatten call so it still returns the same value
as before.irb(main):043:0> {foo: []}.all? { |k, v| v && v.empty? && !v.is_a?(String) }
=> true
irb(main):044:0> {foo: []}.values.flatten.size.zero?
=> trueThere is a difference when error is a {}
irb(main):053:0> {foo: {}}.all? { |k, v| v && v.empty? && !v.is_a?(String) }
=> true
irb(main):054:0> {foo: {}}.values.flatten.size.zero?
=> falsePreviously size and count returned different results for [] case - size
#=> 0, count #=> 1.
There is no test for adding [], {} as error value and no documentation of
such usage and I don't know why someone would want to add such error.
Should we support it?
If yes, what should be the proper output, because now it's totally
inconsistent.—
Reply to this email directly or view it on GitHub
https://github.com/rails/rails/pull/19021/files#r25106561.
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.
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 just found similar old PR where @rafaelfranca addresses issue with error objects other than proc, symbol and string as invalid. #11183 (comment)
Do you think we should check error type on add
method and raise ArgumentError
when it's not one of the three?
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.
Are there any more doubts about this PR?
There is no breakage of promised API and 5.0 is a good time to make it clean.
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.
@rafaelfranca can we at least set 5.0.0 milestone to not forget about this PR?
21580e9
to
9ebb778
Compare
Simplify and alias ActiveModel::Errors methods where possible
No description provided.