ActiveRecord::Errors (Class)

In: lib/active_record/errors.rb
Parent: Object

Methods

add   add_on_empty   clear   count   each   each_full   empty?   invalid?   new   on  

Public Class methods

[Source]

   # File lib/active_record/errors.rb, line 3
3:     def initialize(base)
4:       @base = base
5:       @errors = {}
6:     end

Public Instance methods

[Source]

    # File lib/active_record/errors.rb, line 8
 8:     def add(attribute, msg = nil)
 9:       @errors[attribute] = [] if @errors[attribute].nil?
10:       @errors[attribute] << (msg.nil? ? true : msg)
11:     end

[Source]

    # File lib/active_record/errors.rb, line 13
13:     def add_on_empty(attributes, msg = "can't be empty")
14:       if attributes.kind_of?(Array)
15:         attributes.each { |attr| add(attr, msg) unless @base.attribute_present?(attr) }
16:       else
17:         add(attributes, msg) unless @base.attribute_present?(attributes)
18:       end
19:     end

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

[Source]

    # File lib/active_record/errors.rb, line 22
22:     def invalid?(attribute)
23:       !@errors[attribute].nil?
24:     end

Returns nil, if no errors are associated with the specified <tt>attribute<tt>. Returns the error message, if one error is associated with the specified <tt>attribute<tt>. Returns an array of error messages, if more than one error is associated with the specified <tt>attribute<tt>.

[Source]

    # File lib/active_record/errors.rb, line 30
30:     def on(attribute)
31:       if @errors[attribute].nil?
32:         nil
33:       elsif @errors[attribute].length == 1
34:         @errors[attribute].first
35:       else
36:         @errors[attribute]
37:       end
38:     end

Yields each attribute and associated message per error added

[Source]

    # File lib/active_record/errors.rb, line 41
41:     def each
42:       @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
43:     end

Yields each attribute and associated message per error added

[Source]

    # File lib/active_record/errors.rb, line 46
46:     def each_full
47:       @errors.each_key { |attr| @errors[attr].each { |msg| 
48:         yield @base.class.human_attribute_name(attr) + " " + msg
49:       } }
50:     end

[Source]

    # File lib/active_record/errors.rb, line 52
52:     def empty?
53:       return @errors.empty?
54:     end

[Source]

    # File lib/active_record/errors.rb, line 56
56:     def clear
57:       @errors = {}
58:     end

[Source]

    # File lib/active_record/errors.rb, line 60
60:     def count
61:       error_count = 0
62:       @errors.each_value { |attribute| error_count += attribute.length }
63:       error_count
64:     end

[Validate]