Skip to content
This repository
tree: 797588543e
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 99 lines (88 sloc) 3.482 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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
require 'active_support/core_ext/array/wrap'

module ActiveResource
  class ResourceInvalid < ClientError #:nodoc:
  end

  # 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.
  class Errors < ActiveModel::Errors
    # Grabs errors from an array of messages (like ActiveRecord::Validations)
    def from_array(messages)
      clear
      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
        
        self[:base] << message if attr_message.nil?
      end
    end

    # Grabs errors from the json response.
    def from_json(json)
      array = ActiveSupport::JSON.decode(json)['errors'] rescue []
      from_array array
    end

    # Grabs errors from the XML response.
    def from_xml(xml)
      array = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue []
      from_array array
    end
  end
  
  # 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
  # <tt>validates_presence_of :first_name, :last_name</tt> 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)
  #
  module Validations
    extend ActiveSupport::Concern

    included do
      alias_method_chain :save, :validation
    end

    # Validate a resource and save (POST) it to the remote web service.
    def save_with_validation
      save_without_validation
      true
    rescue ResourceInvalid => error
      case error.response['Content-Type']
      when 'application/xml'
        errors.from_xml(error.response.body)
      when 'application/json'
        errors.from_json(error.response.body)
      end
      false
    end

    # Checks for errors on an object (i.e., is resource.errors empty?).
    #
    # ==== 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
    def valid?
      errors.empty?
    end

    # Returns the Errors object that holds all information about attribute error messages.
    def errors
      @errors ||= Errors.new(self)
    end
  end
end
Something went wrong with that request. Please try again.