From cccbac6df6de18b98e300fdd973758447446dbee Mon Sep 17 00:00:00 2001 From: lulalala Date: Tue, 3 Apr 2018 00:17:45 +0800 Subject: Add a transitional method `objects`, for accessing the array directly. This is because we try to accommodate old hash behavior, so `first` and `last` now does not return Error object. --- activemodel/lib/active_model/errors.rb | 1 + activemodel/test/cases/errors_test.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 98e16ea455..805d042cac 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -74,6 +74,7 @@ module ActiveModel self.i18n_customize_full_message = false attr_reader :errors + alias :objects :errors # Pass in the instance of the object that is using the errors object. # diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 58aa7ee147..048b8a92fb 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -159,14 +159,14 @@ class ErrorsTest < ActiveModel::TestCase assert_equal :name, error.attribute assert_equal :blank, error.type - assert_equal error, person.errors.first + assert_equal error, person.errors.objects.first end test "add, with type as symbol" do person = Person.new person.errors.add(:name, :blank) - assert_equal :blank, person.errors.first.type + assert_equal :blank, person.errors.objects.first.type assert_equal ["can't be blank"], person.errors[:name] end @@ -183,7 +183,7 @@ class ErrorsTest < ActiveModel::TestCase person = Person.new person.errors.add(:name) - assert_equal :invalid, person.errors.first.type + assert_equal :invalid, person.errors.objects.first.type assert_equal ["is invalid"], person.errors[:name] end @@ -203,7 +203,7 @@ class ErrorsTest < ActiveModel::TestCase person = Person.new person.errors.add(:name, type) - assert_equal :blank, person.errors.first.type + assert_equal :blank, person.errors.objects.first.type assert_equal ["can't be blank"], person.errors[:name] end @@ -214,7 +214,7 @@ class ErrorsTest < ActiveModel::TestCase person = Person.new person.errors.add(:name, :blank, message: type) - assert_equal :blank, person.errors.first.type + assert_equal :blank, person.errors.objects.first.type assert_equal [msg], person.errors[:name] end @@ -225,7 +225,7 @@ class ErrorsTest < ActiveModel::TestCase person = Person.new person.errors.add(:name, message: type) - assert_equal :invalid, person.errors.first.type + assert_equal :invalid, person.errors.objects.first.type assert_equal [msg], person.errors[:name] end -- cgit v1.2.3