Class: ActiveRecord::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/validations.rb

Overview

Active Record validation is reported to and from this object, which is used by Base#save to determine whether the object in a valid state to be saved. See usage example in Validations.

Constant Summary collapse

@@default_error_messages =
{
:inclusion => "is not included in the list",
:invalid => "is invalid",
:confirmation => "doesn't match confirmation",
:accepted  => "must be accepted",
:empty => "can't be empty",
:too_long => "is too long (max is %d characters)", 
:too_short => "is too short (min is %d characters)", 
:wrong_length => "is the wrong length (should be %d characters)", 
:taken => "has already been taken",
:not_a_number => "is not a number",
}

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

:nodoc:



5
6
7
# File 'lib/active_record/validations.rb', line 5

def initialize(base) # :nodoc:
  @base, @errors = base, {}
end

Instance Method Details

#add(attribute, msg = @@default_error_messages[:invalid]) ⇒ Object

Adds an error message (msg) to the attribute, which will be returned on a call to on(attribute) for the same attribute and ensure that this error object returns false when asked if empty?. More than one error can be added to the same attribute in which case an array will be returned on a call to on(attribute). If no msg is supplied, “invalid” is assumed.



38
39
40
41
# File 'lib/active_record/validations.rb', line 38

def add(attribute, msg = @@default_error_messages[:invalid])
  @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil?
  @errors[attribute.to_s] << msg
end

#add_on_boundary_breaking(attributes, range, too_long_msg = , too_short_msg = ) ⇒ Object Also known as: add_on_boundry_breaking

Will add an error message to each of the attributes in attributes that has a length outside of the passed boundary range. If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.



54
55
56
57
58
59
60
# File 'lib/active_record/validations.rb', line 54

def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])
  for attr in [attributes].flatten
    value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
    add(attr, too_short_msg % range.begin) if value && value.length < range.begin
    add(attr, too_long_msg % range.end) if value && value.length > range.end
  end
end

#add_on_empty(attributes, msg = ) ⇒ Object

Will add an error message to each of the attributes in attributes that is empty (defined by attribute_present?).



44
45
46
47
48
49
50
# File 'lib/active_record/validations.rb', line 44

def add_on_empty(attributes, msg = @@default_error_messages[:empty])
  for attr in [attributes].flatten
    value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
    is_empty = value.respond_to?("empty?") ? value.empty? : false
    add(attr, msg) unless !value.nil? && !is_empty
  end
end

#add_to_base(msg) ⇒ Object

Adds an error to the base object instead of any particular attribute. This is used to report errors that doesn’t tie to any specific attribute, but rather to the object as a whole. These error messages doesn’t get prepended with any field name when iterating with each_full, so they should be complete sentences.



30
31
32
# File 'lib/active_record/validations.rb', line 30

def add_to_base(msg)
  add(:base, msg)
end

#clearObject

Removes all the errors that have been added.



125
126
127
# File 'lib/active_record/validations.rb', line 125

def clear
  @errors = {}
end

#countObject

Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.



131
132
133
134
135
# File 'lib/active_record/validations.rb', line 131

def count
  error_count = 0
  @errors.each_value { |attribute| error_count += attribute.length }
  error_count
end

#eachObject

Yields each attribute and associated message per error added.



90
91
92
# File 'lib/active_record/validations.rb', line 90

def each
  @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
end

#each_fullObject

Yields each full error message added. So Person.errors.add(“first_name”, “can’t be empty”) will be returned through iteration as “First name can’t be empty”.



96
97
98
# File 'lib/active_record/validations.rb', line 96

def each_full
  full_messages.each { |msg| yield msg }
end

#empty?Boolean

Returns true if no errors have been added.

Returns:

  • (Boolean)


120
121
122
# File 'lib/active_record/validations.rb', line 120

def empty?
  return @errors.empty?
end

#full_messagesObject

Returns all the full error messages in an array.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_record/validations.rb', line 101

def full_messages
  full_messages = []
  
  @errors.each_key do |attr| 
    @errors[attr].each do |msg|
      next if msg.nil?
      
      if attr == "base"
        full_messages << msg
      else
        full_messages << @base.class.human_attribute_name(attr) + " " + msg
      end
    end
  end
  
  return full_messages
end

#invalid?(attribute) ⇒ Boolean

Returns true if the specified attribute has errors associated with it.

Returns:

  • (Boolean)


65
66
67
# File 'lib/active_record/validations.rb', line 65

def invalid?(attribute)
  !@errors[attribute.to_s].nil?
end

#on(attribute) ⇒ Object Also known as: []

  • Returns nil, if no errors are associated with the specified attribute.

  • Returns the error message, if one error is associated with the specified attribute.

  • Returns an array of error messages, if more than one error is associated with the specified attribute.



72
73
74
75
76
77
78
79
80
# File 'lib/active_record/validations.rb', line 72

def on(attribute)
  if @errors[attribute.to_s].nil?
    nil
  elsif @errors[attribute.to_s].length == 1
    @errors[attribute.to_s].first
  else
    @errors[attribute.to_s]
  end
end

#on_baseObject

Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).



85
86
87
# File 'lib/active_record/validations.rb', line 85

def on_base
  on(:base)
end