Class: ActiveResource::Errors
- Inherits:
-
Object
- Object
- ActiveResource::Errors
- Includes:
- Enumerable
- Defined in:
- lib/active_resource/validations.rb
Overview
Active Resource 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 Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
-
#add(attribute, msg) ⇒ Object
Adds an error to an Active Resource object’s attribute (named for the
attribute
parameter) with the error message inmsg
. -
#add_to_base(msg) ⇒ Object
Add an error to the base Active Resource object rather than an attribute.
- #clear ⇒ Object
-
#each ⇒ Object
Yields each attribute and associated message per error added.
-
#each_full ⇒ Object
Yields each full error message added.
-
#from_array(messages) ⇒ Object
Grabs errors from an array of messages (like ActiveRecord::Validations).
-
#from_json(json) ⇒ Object
Grabs errors from the json response.
-
#from_xml(xml) ⇒ Object
Grabs errors from the XML response.
-
#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: #[])
A method to return the errors associated with
attribute
, which returns nil, if no errors are associated with the specifiedattribute
, the error message if one error is associated with the specifiedattribute
, or an array of error messages if more than one error is associated with the specifiedattribute
. -
#on_base ⇒ Object
A method to return errors assigned to
base
object through add_to_base, which returns nil, if no errors are associated with the specifiedattribute
, the error message if one error is associated with the specifiedattribute
, or an array of error messages if more than one error is associated with the specifiedattribute
. -
#size ⇒ Object
(also: #count, #length)
Returns the total number of errors added.
Constructor Details
#initialize(base) ⇒ Errors
:nodoc:
13 14 15 |
# File 'lib/active_resource/validations.rb', line 13 def initialize(base) # :nodoc: @base, @errors = base, {} end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
9 10 11 |
# File 'lib/active_resource/validations.rb', line 9 def errors @errors end |
Instance Method Details
#add(attribute, msg) ⇒ Object
Adds an error to an Active Resource object’s attribute (named for the attribute
parameter) with the error message in msg
.
Examples
my_resource = Node.find(1)
my_resource.errors.add('name', 'can not be "base"') if my_resource.name == 'base'
my_resource.errors.on('name')
# => 'can not be "base"!'
my_resource.errors.add('desc', 'can not be blank') if my_resource.desc == ''
my_resource.valid?
# => false
my_resource.errors.on('desc')
# => 'can not be blank!'
50 51 52 53 |
# File 'lib/active_resource/validations.rb', line 50 def add(attribute, msg) @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? @errors[attribute.to_s] << msg end |
#add_to_base(msg) ⇒ Object
Add an error to the base Active Resource object rather than an attribute.
Examples
my_folder = Folder.find(1)
my_folder.errors.add_to_base("You can't edit an existing folder")
my_folder.errors.on_base
# => "You can't edit an existing folder"
my_folder.errors.add_to_base("This folder has been tagged as frozen")
my_folder.valid?
# => false
my_folder.errors.on_base
# => ["You can't edit an existing folder", "This folder has been tagged as frozen"]
31 32 33 |
# File 'lib/active_resource/validations.rb', line 31 def add_to_base(msg) add(:base, msg) end |
#clear ⇒ Object
178 179 180 |
# File 'lib/active_resource/validations.rb', line 178 def clear @errors = {} end |
#each ⇒ Object
Yields each attribute and associated message per error added.
Examples
my_person = Person.new(params[:person])
my_person.errors.add('login', 'can not be empty') if my_person.login == ''
my_person.errors.add('password', 'can not be empty') if my_person.password == ''
= ''
my_person.errors.each {|attr, msg| += attr.humanize + " " + msg + "<br />"}
# => "Login can not be empty<br />Password can not be empty<br />"
128 129 130 |
# File 'lib/active_resource/validations.rb', line 128 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”.
Examples
my_person = Person.new(params[:person])
my_person.errors.add('login', 'can not be empty') if my_person.login == ''
my_person.errors.add('password', 'can not be empty') if my_person.password == ''
= ''
my_person.errors.each_full {|msg| += msg + "<br/>"}
# => "Login can not be empty<br />Password can not be empty<br />"
145 146 147 |
# File 'lib/active_resource/validations.rb', line 145 def each_full .each { |msg| yield msg } end |
#from_array(messages) ⇒ Object
Grabs errors from an array of messages (like ActiveRecord::Validations)
203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/active_resource/validations.rb', line 203 def from_array() clear humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) } .each do || = humanized_attributes.keys.detect do |attr_name| if [0, attr_name.size + 1] == "#{attr_name} " add humanized_attributes[attr_name], [(attr_name.size + 1)..-1] end end add_to_base if .nil? end end |
#from_json(json) ⇒ Object
Grabs errors from the json response.
218 219 220 221 |
# File 'lib/active_resource/validations.rb', line 218 def from_json(json) array = ActiveSupport::JSON.decode(json)['errors'] rescue [] from_array array end |
#from_xml(xml) ⇒ Object
Grabs errors from the XML response.
224 225 226 227 |
# File 'lib/active_resource/validations.rb', line 224 def from_xml(xml) array = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue [] from_array array end |
#full_messages ⇒ Object
Returns all the full error messages in an array.
Examples
my_person = Person.new(params[:person])
my_person.errors.add('login', 'can not be empty') if my_person.login == ''
my_person.errors.add('password', 'can not be empty') if my_person.password == ''
= ''
my_person.errors..each {|msg| += msg + "<br/>"}
# => "Login can not be empty<br />Password can not be empty<br />"
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/active_resource/validations.rb', line 161 def = [] @errors.each_key do |attr| @errors[attr].each do |msg| next if msg.nil? if attr == "base" << msg else << [attr.humanize, msg].join(' ') end end end end |
#invalid?(attribute) ⇒ Boolean
Returns true if the specified attribute
has errors associated with it.
Examples
my_resource = Disk.find(1)
my_resource.errors.add('location', 'must be Main') unless my_resource.location == 'Main'
my_resource.errors.on('location')
# => 'must be Main!'
my_resource.errors.invalid?('location')
# => true
my_resource.errors.invalid?('name')
# => false
67 68 69 |
# File 'lib/active_resource/validations.rb', line 67 def invalid?(attribute) !@errors[attribute.to_s].nil? end |
#on(attribute) ⇒ Object Also known as: []
A method to return the errors associated with attribute
, which returns nil, if no errors are associated with the specified attribute
, the error message if one error is associated with the specified attribute
, or an array of error messages if more than one error is associated with the specified attribute
.
Examples
my_person = Person.new(params[:person])
my_person.errors.on('login')
# => nil
my_person.errors.add('login', 'can not be empty') if my_person.login == ''
my_person.errors.on('login')
# => 'can not be empty'
my_person.errors.add('login', 'can not be longer than 10 characters') if my_person.login.length > 10
my_person.errors.on('login')
# => ['can not be empty', 'can not be longer than 10 characters']
87 88 89 90 91 |
# File 'lib/active_resource/validations.rb', line 87 def on(attribute) errors = @errors[attribute.to_s] return nil if errors.nil? errors.size == 1 ? errors.first : errors end |
#on_base ⇒ Object
A method to return errors assigned to base
object through add_to_base, which returns nil, if no errors are associated with the specified attribute
, the error message if one error is associated with the specified attribute
, or an array of error messages if more than one error is associated with the specified attribute
.
Examples
my_account = Account.find(1)
my_account.errors.on_base
# => nil
my_account.errors.add_to_base("This account is frozen")
my_account.errors.on_base
# => "This account is frozen"
my_account.errors.add_to_base("This account has been closed")
my_account.errors.on_base
# => ["This account is frozen", "This account has been closed"]
112 113 114 |
# File 'lib/active_resource/validations.rb', line 112 def on_base on(:base) end |
#size ⇒ Object Also known as: count, length
Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.
Examples
my_person = Person.new(params[:person])
my_person.errors.size
# => 0
my_person.errors.add('login', 'can not be empty') if my_person.login == ''
my_person.errors.add('password', 'can not be empty') if my_person.password == ''
my_person.error.size
# => 2
195 196 197 |
# File 'lib/active_resource/validations.rb', line 195 def size @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } end |