Introduction¶ ↑
Gee… it sure would be awesome if ActiveResource actually allowed us to do client-side validation of our ActiveResource objects like the ActiveResource::Base documentation promises
Installation¶ ↑
To install, add this to your environment.rb:
config.gem 'veracious', :version => '0.0.2', :lib => 'veracious', :source => 'http://gemcutter.org'
Next, go to the root of your project in a shell and run this command (you may need to use “sudo” depending on your environment):
sh# rake gems:install
Usage¶ ↑
You can now create a protected “validate” method in your ActiveResource::Base-derived class:
class Book < ActiveResource::Base self.site = 'http://myservice.domain' protected def validate errors.add(:author, 'is required') if (author.blank? rescue true) end end
Now, in your code, you can do the following:
console> b = Book.new console> b.valid? ==> false console> b.author = '' console> b.valid? ==> false console> b.author = 'Iain M. Banks' console> b.valid? ==> true
When you call the valid? method on your active resource object, it will do client side validation. It will not send a request to the service provider for validation.