Included Modules

Files

ActiveResource::Validations

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)

Public Instance Methods

errors() click to toggle source

Returns the Errors object that holds all information about attribute error messages.

     # File lib/active_resource/validations.rb, line 138
138:     def errors
139:       @errors ||= Errors.new(self)
140:     end
save_with_validation(options=nil) click to toggle source

Validate a resource and save (POST) it to the remote web service. If any local validations fail - the save (POST) will not be attempted.

    # File lib/active_resource/validations.rb, line 71
71:     def save_with_validation(options=nil)
72:       perform_validation = case options
73:         when Hash
74:           options[:validate] != false
75:         when NilClass
76:           true
77:         else
78:           ActiveSupport::Deprecation.warn "save(#{options}) is deprecated, please give save(:validate => #{options}) instead", caller
79:           options
80:       end
81: 
82:       # clear the remote validations so they don't interfere with the local
83:       # ones. Otherwise we get an endless loop and can never change the
84:       # fields so as to make the resource valid
85:       @remote_errors = nil
86:       if perform_validation && valid? || !perform_validation
87:         save_without_validation
88:         true
89:       else
90:         false
91:       end
92:     rescue ResourceInvalid => error
93:       # cache the remote errors because every call to <tt>valid?</tt> clears
94:       # all errors. We must keep a copy to add these back after local
95:       # validations
96:       @remote_errors = error
97:       load_remote_errors(@remote_errors, true)
98:       false
99:     end
valid?() click to toggle source

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
     # File lib/active_resource/validations.rb, line 131
131:     def valid?
132:       super
133:       load_remote_errors(@remote_errors, true) if defined?(@remote_errors) && @remote_errors.present?
134:       errors.empty?
135:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.