aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/errors_test.rb
diff options
context:
space:
mode:
authorEsteban Pastorino <ejpastorino@gmail.com>2016-03-24 10:27:31 -0400
committerEsteban Pastorino <ejpastorino@gmail.com>2016-03-24 16:29:22 -0400
commit9848c4632f0a9be19bf8cb9b8aaa146e28602e30 (patch)
tree55b2e8398aede2eb4e801bc127eb821bf02d5723 /activemodel/test/cases/errors_test.rb
parent00a0388adcb2a09cb811ee9659636a154c36bca5 (diff)
downloadrails-9848c4632f0a9be19bf8cb9b8aaa146e28602e30.tar.gz
rails-9848c4632f0a9be19bf8cb9b8aaa146e28602e30.tar.bz2
rails-9848c4632f0a9be19bf8cb9b8aaa146e28602e30.zip
Do not create a hash key when calling ActiveModel::Errors#include?
From: https://github.com/rails/rails/issues/24279 Problem: By doing `record.errors.include? :foo`, it adds a new key to the @messages hash that defaults to an empty array. This happens because of a combination of these 2 commits: https://github.com/rails/rails/commit/b97035df64f5b2f912425c4a7fcb6e6bb3ddab8d (Added in Rails 4.1) and https://github.com/rails/rails/commit/6ec8ba16d85d5feaccb993c9756c1edcbbf0ba13#diff-fdcf8b65b5fb954372c6fe1ddf284c78R76 (Rails 5.0) By adding the default proc that returns an array for non-existing keys, ruby adds that key to the hash. Solution: Change `#include?` to check with `has_key?` and then check if that value is `present?`. Add test case for ActiveModels::Errors#include?
Diffstat (limited to 'activemodel/test/cases/errors_test.rb')
-rw-r--r--activemodel/test/cases/errors_test.rb7
1 files changed, 7 insertions, 0 deletions
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb
index a5ac055033..26ba6ba4fb 100644
--- a/activemodel/test/cases/errors_test.rb
+++ b/activemodel/test/cases/errors_test.rb
@@ -128,6 +128,13 @@ class ErrorsTest < ActiveModel::TestCase
assert !person.errors.include?(:foo)
end
+ test "include? does not add a key to messages hash" do
+ person = Person.new
+ person.errors.include?(:foo)
+
+ assert_not person.errors.messages.key?(:foo)
+ end
+
test "adding errors using conditionals with Person#validate!" do
person = Person.new
person.validate!