Class: ActiveRecord::Errors

Inherits:
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",
}

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

:nodoc:



405
406
407
# File 'lib/active_record/validations.rb', line 405

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

Instance Method Details

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



435
436
437
438
# File 'lib/active_record/validations.rb', line 435

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.



451
452
453
454
455
456
457
# File 'lib/active_record/validations.rb', line 451

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?).



441
442
443
444
445
446
447
# File 'lib/active_record/validations.rb', line 441

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.



427
428
429
# File 'lib/active_record/validations.rb', line 427

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

#clearObject

Removes all the errors that have been added.



522
523
524
# File 'lib/active_record/validations.rb', line 522

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.



528
529
530
531
532
# File 'lib/active_record/validations.rb', line 528

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.



487
488
489
# File 'lib/active_record/validations.rb', line 487

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



493
494
495
# File 'lib/active_record/validations.rb', line 493

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

#empty?Boolean

Returns true if no errors have been added.

Returns:

  • (Boolean)


517
518
519
# File 'lib/active_record/validations.rb', line 517

def empty?
  return @errors.empty?
end

#full_messagesObject

Returns all the full error messages in an array.



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/active_record/validations.rb', line 498

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)


462
463
464
# File 'lib/active_record/validations.rb', line 462

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.



469
470
471
472
473
474
475
476
477
# File 'lib/active_record/validations.rb', line 469

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



482
483
484
# File 'lib/active_record/validations.rb', line 482

def on_base
  on(:base)
end