From 2a06f13099b3344e93198728795209bc69501d4a Mon Sep 17 00:00:00 2001 From: lulalala Date: Tue, 3 Apr 2018 10:52:36 +0800 Subject: Add messages_for --- activemodel/lib/active_model/errors.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 805d042cac..bb64bc6264 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -65,6 +65,8 @@ module ActiveModel extend Forwardable def_delegators :@errors, :size, :clear, :blank?, :empty? + # TODO: forward all enumerable methods after `each` deprecation is removed. + def_delegators :@errors, :count LEGACY_ATTRIBUTES = [:messages, :details].freeze @@ -199,7 +201,7 @@ module ActiveModel # person.errors[:name] # => ["cannot be nil"] # person.errors['name'] # => ["cannot be nil"] def [](attribute) - where(attribute.to_sym).map { |error| error.message } + messages_for(attribute) end # Iterates through each error key, value pair in the error messages hash. @@ -604,6 +606,10 @@ module ActiveModel def deprecation_removal_warning(method_name) ActiveSupport::Deprecation.warn("ActiveModel::Errors##{method_name} is deprecated and will be removed in Rails 6.1") end + + def deprecation_rename_warning(old_method_name, new_method_name) + ActiveSupport::Deprecation.warn("ActiveModel::Errors##{old_method_name} is deprecated. Please call ##{new_method_name} instead.") + end end # Raised when a validation cannot be corrected by end users and are considered -- cgit v1.2.3