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.

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

:nodoc:



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

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

Instance Method Details

#add(attribute, msg = "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.



113
114
115
116
# File 'lib/active_record/validations.rb', line 113

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

#add_on_boundary_breaking(attributes, range, too_long_msg = "is too long (max is %d characters)", too_short_msg = "is too short (min is %d characters)") ⇒ 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.



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

def add_on_boundary_breaking(attributes, range, too_long_msg = "is too long (max is %d characters)", too_short_msg = "is too short (min is %d characters)")
  for attr in [attributes].flatten
    add(attr, too_short_msg % range.begin) if @base.attribute_present?(attr) && @base.send(attr).length < range.begin
    add(attr, too_long_msg % range.end) if @base.attribute_present?(attr) && @base.send(attr).length > range.end
  end
end

#add_on_empty(attributes, msg = "can't be empty") ⇒ Object

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



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

def add_on_empty(attributes, msg = "can't be empty")
  [attributes].flatten.each { |attr| add(attr, msg) unless @base.attribute_present?(attr) }
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.



105
106
107
# File 'lib/active_record/validations.rb', line 105

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

#clearObject

Removes all the errors that have been added.



193
194
195
# File 'lib/active_record/validations.rb', line 193

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.



199
200
201
202
203
# File 'lib/active_record/validations.rb', line 199

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.



160
161
162
# File 'lib/active_record/validations.rb', line 160

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



166
167
168
# File 'lib/active_record/validations.rb', line 166

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

#empty?Boolean

Returns true if no errors have been added.

Returns:

  • (Boolean)


188
189
190
# File 'lib/active_record/validations.rb', line 188

def empty?
  return @errors.empty?
end

#full_messagesObject

Returns all the full error messages in an array.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/active_record/validations.rb', line 171

def full_messages
  full_messages = []
  
  @errors.each_key do |attr| 
    @errors[attr].each do |msg|
      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)


135
136
137
# File 'lib/active_record/validations.rb', line 135

def invalid?(attribute)
  !@errors[attribute].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.



142
143
144
145
146
147
148
149
150
# File 'lib/active_record/validations.rb', line 142

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

#on_baseObject

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



155
156
157
# File 'lib/active_record/validations.rb', line 155

def on_base
  on(:base)
end