de3339935f5c14b0a66b5a645a30afe7f925c535
2 class ResourceInvalid
< ClientError
#:nodoc:
5 # Active Resource validation is reported to and from this object, which is used by Base#save
6 # to determine whether the object in a valid state to be saved. See usage example in Validations.
11 delegate
:empty?, :to => :errors
13 def initialize(base
) # :nodoc:
14 @base, @errors = base
, {}
17 # Add an error to the base Active Resource object rather than an attribute.
20 # my_folder = Folder.find(1)
21 # my_folder.errors.add_to_base("You can't edit an existing folder")
22 # my_folder.errors.on_base
23 # # => "You can't edit an existing folder"
25 # my_folder.errors.add_to_base("This folder has been tagged as frozen")
28 # my_folder.errors.on_base
29 # # => ["You can't edit an existing folder", "This folder has been tagged as frozen"]
35 # Adds an error to an Active Resource object's attribute (named for the +attribute+ parameter)
36 # with the error message in +msg+.
39 # my_resource = Node.find(1)
40 # my_resource.errors.add('name', 'can not be "base"') if my_resource.name == 'base'
41 # my_resource.errors.on('name')
42 # # => 'can not be "base"!'
44 # my_resource.errors.add('desc', 'can not be blank') if my_resource.desc == ''
47 # my_resource.errors.on('desc')
48 # # => 'can not be blank!'
50 def add(attribute
, msg
)
51 @errors[attribute
.to_s
] = [] if @errors[attribute
.to_s
].nil?
52 @errors[attribute
.to_s
] << msg
55 # Returns true if the specified +attribute+ has errors associated with it.
58 # my_resource = Disk.find(1)
59 # my_resource.errors.add('location', 'must be Main') unless my_resource.location == 'Main'
60 # my_resource.errors.on('location')
61 # # => 'must be Main!'
63 # my_resource.errors.invalid?('location')
65 # my_resource.errors.invalid?('name')
67 def invalid
?(attribute
)
68 !@errors[attribute
.to_s
].nil?
71 # A method to return the errors associated with +attribute+, which returns nil, if no errors are
72 # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+,
73 # or an array of error messages if more than one error is associated with the specified +attribute+.
76 # my_person = Person.new(params[:person])
77 # my_person.errors.on('login')
80 # my_person.errors.add('login', 'can not be empty') if my_person.login == ''
81 # my_person.errors.on('login')
82 # # => 'can not be empty'
84 # my_person.errors.add('login', 'can not be longer than 10 characters') if my_person.login.length > 10
85 # my_person.errors.on('login')
86 # # => ['can not be empty', 'can not be longer than 10 characters']
88 errors
= @errors[attribute
.to_s
]
89 return nil if errors
.nil?
90 errors
.size
== 1 ? errors
.first
: errors
95 # A method to return errors assigned to +base+ object through add_to_base, which returns nil, if no errors are
96 # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+,
97 # or an array of error messages if more than one error is associated with the specified +attribute+.
100 # my_account = Account.find(1)
101 # my_account.errors.on_base
104 # my_account.errors.add_to_base("This account is frozen")
105 # my_account.errors.on_base
106 # # => "This account is frozen"
108 # my_account.errors.add_to_base("This account has been closed")
109 # my_account.errors.on_base
110 # # => ["This account is frozen", "This account has been closed"]
116 # Yields each attribute and associated message per error added.
119 # my_person = Person.new(params[:person])
121 # my_person.errors.add('login', 'can not be empty') if my_person.login == ''
122 # my_person.errors.add('password', 'can not be empty') if my_person.password == ''
124 # my_person.errors.each {|attr, msg| messages += attr.humanize + " " + msg + "<br />"}
126 # # => "Login can not be empty<br />Password can not be empty<br />"
129 @errors.each_key
{ |attr
| @errors[attr
].each
{ |msg
| yield attr
, msg
} }
132 # Yields each full error message added. So Person.errors.add("first_name", "can't be empty") will be returned
133 # through iteration as "First name can't be empty".
136 # my_person = Person.new(params[:person])
138 # my_person.errors.add('login', 'can not be empty') if my_person.login == ''
139 # my_person.errors.add('password', 'can not be empty') if my_person.password == ''
141 # my_person.errors.each_full {|msg| messages += msg + "<br/>"}
143 # # => "Login can not be empty<br />Password can not be empty<br />"
146 full_messages
.each
{ |msg
| yield msg
}
149 # Returns all the full error messages in an array.
152 # my_person = Person.new(params[:person])
154 # my_person.errors.add('login', 'can not be empty') if my_person.login == ''
155 # my_person.errors.add('password', 'can not be empty') if my_person.password == ''
157 # my_person.errors.full_messages.each {|msg| messages += msg + "<br/>"}
159 # # => "Login can not be empty<br />Password can not be empty<br />"
164 @errors.each_key
do |attr
|
165 @errors[attr
].each
do |msg
|
171 full_messages
<< [attr
.humanize
, msg
].join(' ')
182 # Returns the total number of errors added. Two errors added to the same attribute will be counted as such
186 # my_person = Person.new(params[:person])
187 # my_person.errors.size
190 # my_person.errors.add('login', 'can not be empty') if my_person.login == ''
191 # my_person.errors.add('password', 'can not be empty') if my_person.password == ''
192 # my_person.error.size
196 @errors.values
.inject(0) { |error_count
, attribute
| error_count
+ attribute
.size
}
199 alias_method
:count, :size
200 alias_method
:length, :size
202 # Grabs errors from the XML response.
205 humanized_attributes
= @base.attributes
.keys
.inject({}) { |h
, attr_name
| h
.update(attr_name
.humanize
=> attr_name
) }
206 messages
= Array
.wrap(Hash
.from_xml(xml
)['errors']['error']) rescue []
207 messages
.each
do |message
|
208 attr_message
= humanized_attributes
.keys
.detect
do |attr_name
|
209 if message
[0, attr_name
.size
+ 1] == "#{attr_name} "
210 add humanized_attributes
[attr_name
], message
[(attr_name
.size
+ 1)..-1]
214 add_to_base message
if attr_message
.nil?
219 # Module to support validation and errors with Active Resource objects. The module overrides
220 # Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned
221 # in the web service response. The module also adds an +errors+ collection that mimics the interface
222 # of the errors provided by ActiveRecord::Errors.
226 # Consider a Person resource on the server requiring both a +first_name+ and a +last_name+ with a
227 # <tt>validates_presence_of :first_name, :last_name</tt> declaration in the model:
229 # person = Person.new(:first_name => "Jim", :last_name => "")
230 # person.save # => false (server returns an HTTP 422 status code and errors)
231 # person.valid? # => false
232 # person.errors.empty? # => false
233 # person.errors.count # => 1
234 # person.errors.full_messages # => ["Last name can't be empty"]
235 # person.errors.on(:last_name) # => "can't be empty"
236 # person.last_name = "Halpert"
237 # person.save # => true (and person is now saved to the remote service)
240 def self.included(base
) # :nodoc:
242 alias_method_chain
:save, :validation
246 # Validate a resource and save (POST) it to the remote web service.
247 def save_with_validation
248 save_without_validation
250 rescue ResourceInvalid
=> error
251 errors
.from_xml(error
.response
.body
)
255 # Checks for errors on an object (i.e., is resource.errors empty?).
258 # my_person = Person.create(params[:person])
262 # my_person.errors.add('login', 'can not be empty') if my_person.login == ''
269 # Returns the Errors object that holds all information about attribute error messages.
271 @errors ||= Errors
.new(self)