Class: ActiveRecord::Errors
- Inherits:
-
Object
- Object
- ActiveRecord::Errors
- 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
-
#add(attribute, msg = "invalid") ⇒ Object
Adds an error message (
msg
) to theattribute
, which will be returned on a call toon(attribute)
for the same attribute and ensure that this error object returns false when asked ifempty?
. -
#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: #add_on_boundry_breaking)
Will add an error message to each of the attributes in
attributes
that has a length outside of the passed boundaryrange
. -
#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 byattribute_present?
). -
#add_to_base(msg) ⇒ Object
Adds an error to the base object instead of any particular attribute.
-
#clear ⇒ Object
Removes all the errors that have been added.
-
#count ⇒ Object
Returns the total number of errors added.
-
#each ⇒ Object
Yields each attribute and associated message per error added.
-
#each_full ⇒ Object
Yields each full error message added.
-
#empty? ⇒ Boolean
Returns true if no errors have been added.
-
#full_messages ⇒ Object
Returns all the full error messages in an array.
-
#initialize(base) ⇒ Errors
constructor
:nodoc:.
-
#invalid?(attribute) ⇒ Boolean
Returns true if the specified
attribute
has errors associated with it. -
#on(attribute) ⇒ Object
(also: #[])
-
Returns nil, if no errors are associated with the specified
attribute
.
-
-
#on_base ⇒ Object
Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).
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 |
#clear ⇒ Object
Removes all the errors that have been added.
193 194 195 |
# File 'lib/active_record/validations.rb', line 193 def clear @errors = {} end |
#count ⇒ Object
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 |
#each ⇒ Object
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_full ⇒ Object
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 .each { |msg| yield msg } end |
#empty? ⇒ Boolean
Returns true if no errors have been added.
188 189 190 |
# File 'lib/active_record/validations.rb', line 188 def empty? return @errors.empty? end |
#full_messages ⇒ Object
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 = [] @errors.each_key do |attr| @errors[attr].each do |msg| if attr == :base << msg else << @base.class.human_attribute_name(attr) + " " + msg end end end return end |
#invalid?(attribute) ⇒ Boolean
Returns true if the specified attribute
has errors associated with it.
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_base ⇒ Object
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 |