1
0
Fork 0
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:
bogdanvlviv 2017-03-28 12:05:14 +03:00
parent d28c482435
commit 01269aede3
3 changed files with 52 additions and 11 deletions

View file

@ -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. * Avoid converting integer as a string into float.
*namusyaka* *namusyaka*

View file

@ -132,15 +132,6 @@ module ActiveModel
# #
# person.errors[:name] # => ["cannot be nil"] # person.errors[:name] # => ["cannot be nil"]
# 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) def [](attribute)
messages[attribute.to_sym] messages[attribute.to_sym]
end end
@ -181,7 +172,9 @@ module ActiveModel
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]} # person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
# person.errors.values # => [["cannot be nil", "must be specified"]] # person.errors.values # => [["cannot be nil", "must be specified"]]
def values def values
messages.values messages.reject do |key, value|
value.empty?
end.values
end end
# Returns all message keys. # Returns all message keys.
@ -189,7 +182,9 @@ module ActiveModel
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]} # person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
# person.errors.keys # => [:name] # person.errors.keys # => [:name]
def keys def keys
messages.keys messages.reject do |key, value|
value.empty?
end.keys
end end
# Returns +true+ if no errors are found, +false+ otherwise. # Returns +true+ if no errors are found, +false+ otherwise.

View file

@ -99,6 +99,14 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal ["omg", "zomg"], errors.values assert_equal ["omg", "zomg"], errors.values
end 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 test "keys returns the error keys" do
errors = ActiveModel::Errors.new(self) errors = ActiveModel::Errors.new(self)
errors.messages[:foo] << "omg" errors.messages[:foo] << "omg"
@ -107,6 +115,14 @@ class ErrorsTest < ActiveModel::TestCase
assert_equal [:foo, :baz], errors.keys assert_equal [:foo, :baz], errors.keys
end 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 test "detecting whether there are errors with empty?, blank?, include?" do
person = Person.new person = Person.new
person.errors[:foo] person.errors[:foo]