aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorbogdanvlviv <bogdanvlviv@gmail.com>2019-01-04 20:35:17 +0200
committerbogdanvlviv <bogdanvlviv@gmail.com>2019-01-04 20:47:31 +0200
commitd5c26c43c0fb3311ec56e62a5fdfe9bf8bf69ecf (patch)
tree0358b297dcd66eb4974a352249d59fdb38fc8d92 /activemodel/lib
parentde50002723dfe9324b9dbe4cdfd18c597746f615 (diff)
downloadrails-d5c26c43c0fb3311ec56e62a5fdfe9bf8bf69ecf.tar.gz
rails-d5c26c43c0fb3311ec56e62a5fdfe9bf8bf69ecf.tar.bz2
rails-d5c26c43c0fb3311ec56e62a5fdfe9bf8bf69ecf.zip
Add `ActiveModel::Errors#of_kind?`
Related to https://github.com/rails/rails/pull/34817#issuecomment-451508668
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/errors.rb39
1 files changed, 30 insertions, 9 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 969effdc20..9fd6f2d89c 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -328,15 +328,15 @@ module ActiveModel
# person.errors.added? :name, :blank # => true
# person.errors.added? :name, "can't be blank" # => true
#
- # If the error message requires an option, then it returns +true+ with
- # the correct option, or +false+ with an incorrect or missing option.
- #
- # person.errors.add :name, :too_long, { count: 25 }
- # person.errors.added? :name, :too_long, count: 25 # => true
- # person.errors.added? :name, "is too long (maximum is 25 characters)" # => true
- # person.errors.added? :name, :too_long, count: 24 # => false
- # person.errors.added? :name, :too_long # => false
- # person.errors.added? :name, "is too long" # => false
+ # If the error message requires options, then it returns +true+ with
+ # the correct options, or +false+ with incorrect or missing options.
+ #
+ # person.errors.add :name, :too_long, { count: 25 }
+ # person.errors.added? :name, :too_long, count: 25 # => true
+ # person.errors.added? :name, "is too long (maximum is 25 characters)" # => true
+ # person.errors.added? :name, :too_long, count: 24 # => false
+ # person.errors.added? :name, :too_long # => false
+ # person.errors.added? :name, "is too long" # => false
def added?(attribute, message = :invalid, options = {})
message = message.call if message.respond_to?(:call)
@@ -347,6 +347,27 @@ module ActiveModel
end
end
+ # Returns +true+ if an error on the attribute with the given message is
+ # present, or +false+ otherwise. +message+ is treated the same as for +add+.
+ #
+ # person.errors.add :age
+ # person.errors.add :name, :too_long, { count: 25 }
+ # person.errors.of_kind? :age # => true
+ # person.errors.of_kind? :name # => false
+ # person.errors.of_kind? :name, :too_long # => true
+ # person.errors.of_kind? :name, "is too long (maximum is 25 characters)" # => true
+ # person.errors.of_kind? :name, :not_too_long # => false
+ # person.errors.of_kind? :name, "is too long" # => false
+ def of_kind?(attribute, message = :invalid)
+ message = message.call if message.respond_to?(:call)
+
+ if message.is_a? Symbol
+ details[attribute.to_sym].map { |e| e[:error] }.include? message
+ else
+ self[attribute].include? message
+ end
+ end
+
# Returns all the full error messages in an array.
#
# class Person