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”: each message through
errors.formatwith the attribute’s human name (I18n.full_message), in the current locale:activerecord.attributes.<model>.<attr>, else the attribute humanized (:article_idgives"Article ..."). -
#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.
-
#model_key ⇒ String
The model’s dictionary key (
"post"),""for an Errors made without one.
Instance Method Details
#[](attr) ⇒ Array<String>
Returns the messages for that attribute, [] if none.
44 45 46 47 |
# File 'cybertrain/errors.rb', line 44 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.
31 32 33 34 35 36 37 38 39 |
# File 'cybertrain/errors.rb', line 31 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.
51 52 53 |
# File 'cybertrain/errors.rb', line 51 def any? !@messages.empty? end |
#clear ⇒ nil
Removes every message.
72 73 74 75 |
# File 'cybertrain/errors.rb', line 72 def clear @messages.clear nil end |
#count ⇒ Integer
Returns the number of messages, over every attribute.
63 64 65 66 67 |
# File 'cybertrain/errors.rb', line 63 def count n = 0 @messages.each_value { |list| n += list.size } n end |
#each {|attr, message| ... } ⇒ Object
Yields every message with its attribute.
119 120 121 122 123 |
# File 'cybertrain/errors.rb', line 119 def each @messages.each do |attr, list| list.each { || yield attr, } end end |
#empty? ⇒ Boolean
Returns true when there are no messages.
57 58 59 |
# File 'cybertrain/errors.rb', line 57 def empty? @messages.empty? end |
#full_messages ⇒ Array<String>
“Title can’t be blank”: each message through errors.format with the
attribute’s human name (I18n.full_message), in the current locale:
activerecord.attributes.<model>.<attr>, else the attribute
humanized (:article_id gives "Article ..."). A :base message
stands alone, as in Rails.
106 107 108 109 110 111 112 113 |
# File 'cybertrain/errors.rb', line 106 def out = Array.new(0) { "" } key = @model_key @messages.each do |attr, list| list.each { || out << I18n.(key, attr, ) } end out end |
#key?(attr) ⇒ Boolean
Returns true when the attribute has a message.
80 81 82 |
# File 'cybertrain/errors.rb', line 80 def key?(attr) @messages.key?(attr) end |
#keys ⇒ Array<Symbol>
Returns the attributes that have messages, in the order they were first added.
87 88 89 |
# File 'cybertrain/errors.rb', line 87 def keys @messages.keys end |
#model_key ⇒ String
The model’s dictionary key ("post"), "" for an Errors made
without one.
95 |
# File 'cybertrain/errors.rb', line 95 def model_key = @model_key |