Class: Cybertrain::Errors
- Inherits:
-
Object
- Object
- Cybertrain::Errors
- Defined in:
- cybertrain/errors.rb
Overview
Validation messages of one record, keyed by attribute (ActiveModel::Errors). Every model has one, Model#errors, cleared and refilled by Model#valid?.
Keys are the Symbols the validations use (:title); a String key is a
different key. In a template: article.errors.any?,
article.errors.full_messages, article.errors[:title].
Instance Method Summary collapse
-
#[](attr) ⇒ Array<String>
The messages for that attribute,
[]if none. -
#add(attr, message) ⇒ nil
Adds a message for an attribute;
:basefor the record as a whole. -
#any? ⇒ Boolean
True when there is at least one message.
-
#clear ⇒ nil
Removes every message.
-
#count ⇒ Integer
The number of messages, over every attribute.
-
#each {|attr, message| ... } ⇒ Object
Yields every message with its attribute.
-
#empty? ⇒ Boolean
True when there are no messages.
-
#full_messages ⇒ Array<String>
“Title can’t be blank”: the attribute humanized, then the message.
-
#key?(attr) ⇒ Boolean
True when the attribute has a message.
-
#keys ⇒ Array<Symbol>
The attributes that have messages, in the order they were first added.
Instance Method Details
#[](attr) ⇒ Array<String>
Returns the messages for that attribute, [] if none.
38 39 40 41 |
# File 'cybertrain/errors.rb', line 38 def [](attr) list = @messages[attr] list.nil? ? Array.new(0) { "" } : list end |
#add(attr, message) ⇒ nil
Adds a message for an attribute; :base for the record as a whole.
25 26 27 28 29 30 31 32 33 |
# File 'cybertrain/errors.rb', line 25 def add(attr, ) list = @messages[attr] if list.nil? @messages[attr] = [] else list << end nil end |
#any? ⇒ Boolean
Returns true when there is at least one message.
45 46 47 |
# File 'cybertrain/errors.rb', line 45 def any? !@messages.empty? end |
#clear ⇒ nil
Removes every message.
66 67 68 69 |
# File 'cybertrain/errors.rb', line 66 def clear @messages.clear nil end |
#count ⇒ Integer
Returns the number of messages, over every attribute.
57 58 59 60 61 |
# File 'cybertrain/errors.rb', line 57 def count n = 0 @messages.each_value { |list| n += list.size } n end |
#each {|attr, message| ... } ⇒ Object
Yields every message with its attribute.
105 106 107 108 109 |
# File 'cybertrain/errors.rb', line 105 def each @messages.each do |attr, list| list.each { || yield attr, } end end |
#empty? ⇒ Boolean
Returns true when there are no messages.
51 52 53 |
# File 'cybertrain/errors.rb', line 51 def empty? @messages.empty? end |
#full_messages ⇒ Array<String>
“Title can’t be blank”: the attribute humanized, then the message.
Humanizing replaces _ with a space and capitalizes, so :article_id
gives "Article id ..." and :base gives "Base ...".
92 93 94 95 96 97 98 99 |
# File 'cybertrain/errors.rb', line 92 def out = Array.new(0) { "" } @messages.each do |attr, list| label = attr.to_s.tr("_", " ").capitalize list.each { || out << "#{label} #{}" } end out end |
#key?(attr) ⇒ Boolean
Returns true when the attribute has a message.
74 75 76 |
# File 'cybertrain/errors.rb', line 74 def key?(attr) @messages.key?(attr) end |
#keys ⇒ Array<Symbol>
Returns the attributes that have messages, in the order they were first added.
81 82 83 |
# File 'cybertrain/errors.rb', line 81 def keys @messages.keys end |