Class: Cybertrain::Errors

Inherits:
Object
  • Object
show all
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

Instance Method Details

#[](attr) ⇒ Array<String>

Returns the messages for that attribute, [] if none.

Parameters:

  • attr (Symbol)

Returns:

  • (Array<String>) —

    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.

Examples:

before_validation { |r| r.errors.add(:base, "Pick a date") if r.starts_on.nil? }

Parameters:

  • attr (Symbol)
  • message (String) —

    e.g. "is taken", shown after the humanized attribute by #full_messages

Returns:

  • (nil)


31
32
33
34
35
36
37
38
39
# File 'cybertrain/errors.rb', line 31

def add(attr, message)
  list = @messages[attr]
  if list.nil?
    @messages[attr] = [message]
  else
    list << message
  end
  nil
end

#any? ⇒ Boolean

Returns true when there is at least one message.

Returns:

  • (Boolean) —

    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.

Returns:

  • (nil)


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.

Returns:

  • (Integer) —

    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.

Yield Parameters:

  • attr (Symbol)
  • message (String)


119
120
121
122
123
# File 'cybertrain/errors.rb', line 119

def each
  @messages.each do |attr, list|
    list.each { |message| yield attr, message }
  end
end

#empty? ⇒ Boolean

Returns true when there are no messages.

Returns:

  • (Boolean) —

    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.

Examples:

@article.errors.full_messages.join(", ")

Returns:

  • (Array<String>)


106
107
108
109
110
111
112
113
# File 'cybertrain/errors.rb', line 106

def full_messages
  out = Array.new(0) { "" }
  key = @model_key
  @messages.each do |attr, list|
    list.each { |message| out << I18n.full_message(key, attr, message) }
  end
  out
end

#key?(attr) ⇒ Boolean

Returns true when the attribute has a message.

Parameters:

  • attr (Symbol)

Returns:

  • (Boolean) —

    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.

Returns:

  • (Array<Symbol>) —

    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.

Returns:

  • (String)


95
# File 'cybertrain/errors.rb', line 95

def model_key = @model_key