Module: ActiveResource::Validations
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::Validations
- Defined in:
- activeresource/lib/active_resource/validations.rb
Overview
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. # => ["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)
Instance Method Summary (collapse)
-
- (Object) errors
Returns the Errors object that holds all information about attribute error messages.
-
- (Object) load_remote_errors(remote_errors, save_cache = false)
Loads the set of remote errors into the object's Errors based on the content-type of the error-block received.
-
- (Object) save_with_validation(options = nil)
Validate a resource and save (POST) it to the remote web service.
-
- (Boolean) valid?
Checks for errors on an object (i.e., is resource.errors empty?).
Methods included from ActiveSupport::Concern
append_features, extended, included
Methods included from ActiveModel::Validations
Methods included from ActiveSupport::Callbacks
Instance Method Details
- (Object) errors
Returns the Errors object that holds all information about attribute error messages.
138 139 140 |
# File 'activeresource/lib/active_resource/validations.rb', line 138 def errors @errors ||= Errors.new(self) end |
- (Object) load_remote_errors(remote_errors, save_cache = false)
Loads the set of remote errors into the object's Errors based on the content-type of the error-block received
104 105 106 107 108 109 110 111 |
# File 'activeresource/lib/active_resource/validations.rb', line 104 def load_remote_errors(remote_errors, save_cache = false ) #:nodoc: case self.class.format when ActiveResource::Formats[:xml] errors.from_xml(remote_errors.response.body, save_cache) when ActiveResource::Formats[:json] errors.from_json(remote_errors.response.body, save_cache) end end |
- (Object) save_with_validation(options = nil)
Validate a resource and save (POST) it to the remote web service. If any local validations fail - the save (POST) will not be attempted.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'activeresource/lib/active_resource/validations.rb', line 71 def save_with_validation(=nil) perform_validation = case when Hash [:validate] != false when NilClass true else ActiveSupport::Deprecation.warn "save(#{}) is deprecated, please give save(:validate => #{}) instead", caller end # clear the remote validations so they don't interfere with the local # ones. Otherwise we get an endless loop and can never change the # fields so as to make the resource valid @remote_errors = nil if perform_validation && valid? || !perform_validation save_without_validation true else false end rescue ResourceInvalid => error # cache the remote errors because every call to <tt>valid?</tt> clears # all errors. We must keep a copy to add these back after local # validations @remote_errors = error load_remote_errors(@remote_errors, true) false end |
- (Boolean) valid?
Checks for errors on an object (i.e., is resource.errors empty?).
Runs all the specified local validations and returns true if no errors were added, otherwise false. Runs local validations (eg those on your Active Resource model), and also any errors returned from the remote system the last time we saved. Remote errors can only be cleared by trying to re-save the resource.
Examples
my_person = Person.create(params[:person])
my_person.valid?
# => true
my_person.errors.add('login', 'can not be empty') if my_person.login == ''
my_person.valid?
# => false
131 132 133 134 135 |
# File 'activeresource/lib/active_resource/validations.rb', line 131 def valid? super load_remote_errors(@remote_errors, true) if defined?(@remote_errors) && @remote_errors.present? errors.empty? end |