Skip to content

Commit bce2e64

Browse files
authored
Merge pull request rails#23675 from kachick/activemodel-errors-indifferent
Adjust to indifferent access around some ActiveModel::Errors methods
2 parents 43e0aee + 9f566ab commit bce2e64

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

activemodel/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Allow indifferent access in `ActiveModel::Errors`.
2+
3+
`#include?`, `#has_key?`, `#key?`, `#delete` and `#full_messages_for`.
4+
5+
*Kenichi Kamiya*
6+
17
* Removed deprecated `:tokenizer` in the length validator.
28

39
*Rafael Mendonça França*

activemodel/lib/active_model/errors.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def clear
110110
# person.errors.include?(:name) # => true
111111
# person.errors.include?(:age) # => false
112112
def include?(attribute)
113+
attribute = attribute.to_sym
113114
messages.key?(attribute) && messages[attribute].present?
114115
end
115116
alias :has_key? :include?
@@ -121,8 +122,9 @@ def include?(attribute)
121122
# person.errors.delete(:name) # => ["cannot be nil"]
122123
# person.errors[:name] # => []
123124
def delete(key)
124-
details.delete(key)
125-
messages.delete(key)
125+
attribute = key.to_sym
126+
details.delete(attribute)
127+
messages.delete(attribute)
126128
end
127129

128130
# When passed a symbol or a name of a method, returns an array of errors
@@ -342,6 +344,7 @@ def full_messages
342344
# person.errors.full_messages_for(:name)
343345
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
344346
def full_messages_for(attribute)
347+
attribute = attribute.to_sym
345348
messages[attribute].map { |message| full_message(attribute, message) }
346349
end
347350

activemodel/test/cases/errors_test.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ def self.lookup_ancestors
3030
def test_delete
3131
errors = ActiveModel::Errors.new(self)
3232
errors[:foo] << "omg"
33-
errors.delete(:foo)
33+
errors.delete("foo")
3434
assert_empty errors[:foo]
3535
end
3636

3737
def test_include?
3838
errors = ActiveModel::Errors.new(self)
3939
errors[:foo] << "omg"
4040
assert_includes errors, :foo, "errors should include :foo"
41+
assert_includes errors, "foo", "errors should include 'foo' as :foo"
4142
end
4243

4344
def test_dup
@@ -52,6 +53,7 @@ def test_has_key?
5253
errors = ActiveModel::Errors.new(self)
5354
errors[:foo] << "omg"
5455
assert_equal true, errors.has_key?(:foo), "errors should have key :foo"
56+
assert_equal true, errors.has_key?("foo"), "errors should have key 'foo' as :foo"
5557
end
5658

5759
def test_has_no_key
@@ -63,6 +65,7 @@ def test_key?
6365
errors = ActiveModel::Errors.new(self)
6466
errors[:foo] << "omg"
6567
assert_equal true, errors.key?(:foo), "errors should have key :foo"
68+
assert_equal true, errors.key?("foo"), "errors should have key 'foo' as :foo"
6669
end
6770

6871
def test_no_key
@@ -150,10 +153,11 @@ def test_no_key
150153
assert_equal ["cannot be blank"], person.errors[:name]
151154
end
152155

153-
test "added? detects if a specific error was added to the object" do
156+
test "added? detects indifferent if a specific error was added to the object" do
154157
person = Person.new
155158
person.errors.add(:name, "cannot be blank")
156159
assert person.errors.added?(:name, "cannot be blank")
160+
assert person.errors.added?("name", "cannot be blank")
157161
end
158162

159163
test "added? handles symbol message" do
@@ -241,7 +245,7 @@ def test_no_key
241245
assert_equal ["name cannot be blank", "name cannot be nil"], person.errors.full_messages
242246
end
243247

244-
test "full_messages_for contains all the error messages for the given attribute" do
248+
test "full_messages_for contains all the error messages for the given attribute indifferent" do
245249
person = Person.new
246250
person.errors.add(:name, "cannot be blank")
247251
person.errors.add(:name, "cannot be nil")
@@ -253,6 +257,7 @@ def test_no_key
253257
person.errors.add(:name, "cannot be blank")
254258
person.errors.add(:email, "cannot be blank")
255259
assert_equal ["name cannot be blank"], person.errors.full_messages_for(:name)
260+
assert_equal ["name cannot be blank"], person.errors.full_messages_for("name")
256261
end
257262

258263
test "full_messages_for returns an empty list in case there are no errors for the given attribute" do

0 commit comments

Comments
 (0)