| In: |
lib/active_record/errors.rb
|
| Parent: | Object |
# File lib/active_record/errors.rb, line 3 3: def initialize(base) 4: @base = base 5: @errors = {} 6: end
# 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
# 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.
# 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>.
# 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
# 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
# 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