2009-05-06 03:14:55 -04:00
|
|
|
require 'active_support/core_ext/array/wrap'
|
|
|
|
|
2006-09-07 20:07:30 -04:00
|
|
|
module ActiveResource
|
2007-01-16 01:34:10 -05:00
|
|
|
class ResourceInvalid < ClientError #:nodoc:
|
2006-09-07 20:07:30 -04:00
|
|
|
end
|
|
|
|
|
2007-01-16 01:34:10 -05:00
|
|
|
# Active Resource validation is reported to and from this object, which is used by Base#save
|
|
|
|
# to determine whether the object in a valid state to be saved. See usage example in Validations.
|
2009-03-20 14:51:01 -04:00
|
|
|
class Errors < ActiveModel::Errors
|
2009-08-09 09:24:06 -04:00
|
|
|
# Grabs errors from an array of messages (like ActiveRecord::Validations)
|
2009-08-19 06:57:50 -04:00
|
|
|
# The second parameter directs the errors cache to be cleared (default)
|
|
|
|
# or not (by passing true)
|
|
|
|
def from_array(messages, save_cache = false)
|
|
|
|
clear unless save_cache
|
2006-09-07 20:07:30 -04:00
|
|
|
humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) }
|
|
|
|
messages.each do |message|
|
|
|
|
attr_message = humanized_attributes.keys.detect do |attr_name|
|
|
|
|
if message[0, attr_name.size + 1] == "#{attr_name} "
|
|
|
|
add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-21 14:29:15 -04:00
|
|
|
self[:base] << message if attr_message.nil?
|
2006-09-07 20:07:30 -04:00
|
|
|
end
|
|
|
|
end
|
2009-08-09 09:24:06 -04:00
|
|
|
|
2009-08-19 06:57:50 -04:00
|
|
|
# Grabs errors from a json response.
|
|
|
|
def from_json(json, save_cache = false)
|
2009-08-09 09:24:06 -04:00
|
|
|
array = ActiveSupport::JSON.decode(json)['errors'] rescue []
|
2009-08-19 06:57:50 -04:00
|
|
|
from_array array, save_cache
|
2009-08-09 09:24:06 -04:00
|
|
|
end
|
|
|
|
|
2009-08-19 06:57:50 -04:00
|
|
|
# Grabs errors from an XML response.
|
|
|
|
def from_xml(xml, save_cache = false)
|
2009-08-09 09:24:06 -04:00
|
|
|
array = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue []
|
2009-08-19 06:57:50 -04:00
|
|
|
from_array array, save_cache
|
2009-08-09 09:24:06 -04:00
|
|
|
end
|
2006-09-07 20:07:30 -04:00
|
|
|
end
|
|
|
|
|
2008-09-03 12:58:47 -04:00
|
|
|
# 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.
|
2007-06-23 13:29:54 -04:00
|
|
|
#
|
|
|
|
# ==== Example
|
2007-01-16 01:34:10 -05:00
|
|
|
#
|
2008-09-03 12:58:47 -04:00
|
|
|
# Consider a Person resource on the server requiring both a +first_name+ and a +last_name+ with a
|
|
|
|
# <tt>validates_presence_of :first_name, :last_name</tt> declaration in the model:
|
2007-01-16 01:34:10 -05:00
|
|
|
#
|
2008-09-03 12:58:47 -04:00
|
|
|
# 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"]
|
2009-03-20 14:51:01 -04:00
|
|
|
# person.errors[:last_name] # => ["can't be empty"]
|
2008-09-03 12:58:47 -04:00
|
|
|
# person.last_name = "Halpert"
|
|
|
|
# person.save # => true (and person is now saved to the remote service)
|
2007-01-16 01:34:10 -05:00
|
|
|
#
|
2006-09-07 20:07:30 -04:00
|
|
|
module Validations
|
2009-06-08 21:52:27 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-08-19 06:57:50 -04:00
|
|
|
include ActiveModel::Validations
|
|
|
|
extend ActiveModel::Validations::ClassMethods
|
2009-06-08 21:52:27 -04:00
|
|
|
|
|
|
|
included do
|
|
|
|
alias_method_chain :save, :validation
|
2006-09-07 20:07:30 -04:00
|
|
|
end
|
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# Validate a resource and save (POST) it to the remote web service.
|
2009-08-19 06:57:50 -04:00
|
|
|
# If any local validations fail - the save (POST) will not be attempted.
|
|
|
|
def save_with_validation(perform_validation = true)
|
|
|
|
# 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
|
2006-10-02 18:14:15 -04:00
|
|
|
rescue ResourceInvalid => error
|
2009-08-19 06:57:50 -04:00
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
|
# Loads the set of remote errors into the object's Errors based on the
|
|
|
|
# content-type of the error-block received
|
|
|
|
def load_remote_errors(remote_errors, save_cache = false ) #:nodoc:
|
|
|
|
case remote_errors.response['Content-Type']
|
2009-09-17 14:34:40 -04:00
|
|
|
when /xml/
|
2009-08-19 06:57:50 -04:00
|
|
|
errors.from_xml(remote_errors.response.body, save_cache)
|
2009-09-17 14:34:40 -04:00
|
|
|
when /json/
|
2009-08-19 06:57:50 -04:00
|
|
|
errors.from_json(remote_errors.response.body, save_cache)
|
2009-08-09 09:24:06 -04:00
|
|
|
end
|
2006-09-07 20:07:30 -04:00
|
|
|
end
|
|
|
|
|
2007-06-23 13:29:54 -04:00
|
|
|
# Checks for errors on an object (i.e., is resource.errors empty?).
|
2009-08-19 06:57:50 -04:00
|
|
|
#
|
|
|
|
# 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.
|
2007-06-23 13:29:54 -04:00
|
|
|
#
|
|
|
|
# ==== 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
|
2009-08-19 06:57:50 -04:00
|
|
|
#
|
2006-09-07 20:07:30 -04:00
|
|
|
def valid?
|
2009-08-19 06:57:50 -04:00
|
|
|
super
|
|
|
|
load_remote_errors(@remote_errors, true) if defined?(@remote_errors) && @remote_errors.present?
|
2006-09-07 20:07:30 -04:00
|
|
|
errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the Errors object that holds all information about attribute error messages.
|
|
|
|
def errors
|
|
|
|
@errors ||= Errors.new(self)
|
|
|
|
end
|
|
|
|
end
|
2006-12-29 17:28:30 -05:00
|
|
|
end
|