diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-03-30 13:47:30 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-03-30 13:47:30 -0300 |
commit | 7131f6ba2249c7828f1eb86e2b490674b8ddfaeb (patch) | |
tree | db75571574071ed9459a36923ff010564597c9d8 /activemodel/lib/active_model | |
parent | b5c35023bd422c62973d003378b7185dfe221919 (diff) | |
parent | 9ebb778ca0f891efcbf2b8f73c01860c6a6e1108 (diff) | |
download | rails-7131f6ba2249c7828f1eb86e2b490674b8ddfaeb.tar.gz rails-7131f6ba2249c7828f1eb86e2b490674b8ddfaeb.tar.bz2 rails-7131f6ba2249c7828f1eb86e2b490674b8ddfaeb.zip |
Merge pull request #19021 from morgoth/activemodel-errors-refactoring
Simplify and alias ActiveModel::Errors methods where possible
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 28 |
1 files changed, 4 insertions, 24 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index e35ed03e74..f843b279ce 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -102,9 +102,7 @@ module ActiveModel def include?(attribute) messages[attribute].present? end - # aliases include? alias :has_key? :include? - # aliases include? alias :key? :include? # Get messages for +key+. @@ -199,6 +197,7 @@ module ActiveModel def size values.flatten.size end + alias :count :size # Returns all message values. # @@ -216,35 +215,15 @@ module ActiveModel messages.keys end - # Returns an array of error messages, with the attribute name included. - # - # person.errors.add(:name, :blank, message: "can't be blank") - # person.errors.add(:name, :not_specified, message: "must be specified") - # person.errors.to_a # => ["name can't be blank", "name must be specified"] - def to_a - full_messages - end - - # Returns the number of error messages. - # - # person.errors.add(:name, :blank, message: "can't be blank") - # person.errors.count # => 1 - # person.errors.add(:name, :not_specified, message: "must be specified") - # person.errors.count # => 2 - def count - to_a.size - end - # Returns +true+ if no errors are found, +false+ otherwise. # If the error message is a string it can be empty. # # person.errors.full_messages # => ["name cannot be nil"] # person.errors.empty? # => false def empty? - all? { |k, v| v && v.empty? && !v.is_a?(String) } + size.zero? end - # aliases empty? - alias_method :blank?, :empty? + alias :blank? :empty? # Returns an xml formatted representation of the Errors hash. # @@ -407,6 +386,7 @@ module ActiveModel def full_messages map { |attribute, message| full_message(attribute, message) } end + alias :to_a :full_messages # Returns all the full error messages for a given attribute in an array. # |