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



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.

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)


25
26
27
28
29
30
31
32
33
# File 'cybertrain/errors.rb', line 25

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



45
46
47
# File 'cybertrain/errors.rb', line 45

def any?
  !@messages.empty?
end

#clear ⇒ nil

Removes every message.

Returns:

  • (nil)


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.

Returns:

  • (Integer) —

    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.

Yield Parameters:

  • attr (Symbol)
  • message (String)


105
106
107
108
109
# File 'cybertrain/errors.rb', line 105

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



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 ...".

Examples:

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

Returns:

  • (Array<String>)


92
93
94
95
96
97
98
99
# File 'cybertrain/errors.rb', line 92

def full_messages
  out = Array.new(0) { "" }
  @messages.each do |attr, list|
    label = attr.to_s.tr("_", " ").capitalize
    list.each { |message| out << "#{label} #{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



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.

Returns:

  • (Array<Symbol>) —

    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