mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix ActiveModel::Errors #keys, #values
Before: person.errors.keys # => [] person.errors.values # => [] person.errors[:name] # => [] person.errors.keys # => [:name] person.errors.values # => [[]] After: person.errors.keys # => [] person.errors.values # => [] person.errors[:name] # => [] person.errors.keys # => [] person.errors.values # => [] Related to #23468
This commit is contained in:
parent
d28c482435
commit
01269aede3
3 changed files with 52 additions and 11 deletions
|
@ -1,3 +1,33 @@
|
|||
* Fix methods `#keys`, `#values` in `ActiveModel::Errors`.
|
||||
|
||||
Change `#keys` to only return the keys that don't have empty messages.
|
||||
|
||||
Change `#values` to only return the not empty values.
|
||||
|
||||
Example:
|
||||
|
||||
# Before
|
||||
person = Person.new
|
||||
person.errors.keys # => []
|
||||
person.errors.values # => []
|
||||
person.errors.messages # => {}
|
||||
person.errors[:name] # => []
|
||||
person.errors.messages # => {:name => []}
|
||||
person.errors.keys # => [:name]
|
||||
person.errors.values # => [[]]
|
||||
|
||||
# After
|
||||
person = Person.new
|
||||
person.errors.keys # => []
|
||||
person.errors.values # => []
|
||||
person.errors.messages # => {}
|
||||
person.errors[:name] # => []
|
||||
person.errors.messages # => {:name => []}
|
||||
person.errors.keys # => []
|
||||
person.errors.values # => []
|
||||
|
||||
*bogdanvlviv*
|
||||
|
||||
* Avoid converting integer as a string into float.
|
||||
|
||||
*namusyaka*
|
||||
|
|
|
@ -132,15 +132,6 @@ module ActiveModel
|
|||
#
|
||||
# person.errors[:name] # => ["cannot be nil"]
|
||||
# person.errors['name'] # => ["cannot be nil"]
|
||||
#
|
||||
# Note that, if you try to get errors of an attribute which has
|
||||
# no errors associated with it, this method will instantiate
|
||||
# an empty error list for it and +keys+ will return an array
|
||||
# of error keys which includes this attribute.
|
||||
#
|
||||
# person.errors.keys # => []
|
||||
# person.errors[:name] # => []
|
||||
# person.errors.keys # => [:name]
|
||||
def [](attribute)
|
||||
messages[attribute.to_sym]
|
||||
end
|
||||
|
@ -181,7 +172,9 @@ module ActiveModel
|
|||
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
|
||||
# person.errors.values # => [["cannot be nil", "must be specified"]]
|
||||
def values
|
||||
messages.values
|
||||
messages.reject do |key, value|
|
||||
value.empty?
|
||||
end.values
|
||||
end
|
||||
|
||||
# Returns all message keys.
|
||||
|
@ -189,7 +182,9 @@ module ActiveModel
|
|||
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
|
||||
# person.errors.keys # => [:name]
|
||||
def keys
|
||||
messages.keys
|
||||
messages.reject do |key, value|
|
||||
value.empty?
|
||||
end.keys
|
||||
end
|
||||
|
||||
# Returns +true+ if no errors are found, +false+ otherwise.
|
||||
|
|
|
@ -99,6 +99,14 @@ class ErrorsTest < ActiveModel::TestCase
|
|||
assert_equal ["omg", "zomg"], errors.values
|
||||
end
|
||||
|
||||
test "values returns an empty array after try to get a message only" do
|
||||
errors = ActiveModel::Errors.new(self)
|
||||
errors.messages[:foo]
|
||||
errors.messages[:baz]
|
||||
|
||||
assert_equal [], errors.values
|
||||
end
|
||||
|
||||
test "keys returns the error keys" do
|
||||
errors = ActiveModel::Errors.new(self)
|
||||
errors.messages[:foo] << "omg"
|
||||
|
@ -107,6 +115,14 @@ class ErrorsTest < ActiveModel::TestCase
|
|||
assert_equal [:foo, :baz], errors.keys
|
||||
end
|
||||
|
||||
test "keys returns an empty array after try to get a message only" do
|
||||
errors = ActiveModel::Errors.new(self)
|
||||
errors.messages[:foo]
|
||||
errors.messages[:baz]
|
||||
|
||||
assert_equal [], errors.keys
|
||||
end
|
||||
|
||||
test "detecting whether there are errors with empty?, blank?, include?" do
|
||||
person = Person.new
|
||||
person.errors[:foo]
|
||||
|
|
Loading…
Reference in a new issue