- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3 (0)
- 2.1.0 (0)
- 2.2.1 (-18)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-1)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveRecord::Errors.
Example
Consider a Person resource on the server requiring both a first_name and a last_name with a validates_presence_of :first_name, :last_name declaration in the model:
person = Person.new(:first_name => "Jim", :last_name => "") person.save # => false (server returns an HTTP 422 status code and errors) person.valid? # => false person.errors.empty? # => false person.errors.count # => 1 person.errors.full_messages # => ["Last name can't be empty"] person.errors[:last_name] # => ["can't be empty"] person.last_name = "Halpert" person.save # => true (and person is now saved to the remote service)
ActiveResource validation is a little different
Given the following model on the remote end:
class Person < ActiveRecord::Base validates_presence_of :first_name, :last_name, :email end
And this ActiveResource on the client end:
class Person < ActiveResource::Base self.site = "http://api.people.com:3000/" end
Validation messages will only be returned after an attempted save call on the client end - eg:
person = Person.new( :first_name => 'Billy', :emails => "william@anotherplanet.co.za" ) person.valid? # => true person.errors.full_messages # => [] person.save # => false person.valid? # => false person.errors.full_messages # => ["Last name can't be empty"]
In ActiveResource::Base it is suggested that you can perform client site validation with something like this:
class Person < ActiveResource::Base self.site = "http://api.people.com:3000/" protected def validate errors.add("last name", "can't be empty") if last_name.blank? end end