Class: ActiveResource::Errors

Inherits:
ActiveModel::Errors show all
Defined in:
activeresource/lib/active_resource/validations.rb

Overview

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.

Constant Summary

Constant Summary

Constants inherited from ActiveModel::Errors

ActiveModel::Errors::CALLBACKS_OPTIONS

Instance Method Summary (collapse)

Methods inherited from ActiveModel::Errors

#[], #[]=, #add, #add_on_blank, #add_on_empty, #as_json, #count, #each, #empty?, #full_messages, #generate_message, #initialize, #size, #to_a, #to_hash, #to_xml

Methods inherited from ActiveSupport::OrderedHash

[], #[]=, #clear, #delete, #delete_if, #each, #each_key, #each_value, #encode_with, #initialize, #initialize_copy, #inspect, #invert, #keys, #merge, #merge!, #reject, #reject!, #replace, #shift, #to_a, #to_hash, #to_yaml, #to_yaml_type, #values

Methods inherited from Hash

#as_json, #assert_valid_keys, #deep_dup, #deep_merge, #deep_merge!, #diff, #encode_json, #except, #except!, #extract!, #extractable_options?, from_xml, #reverse_merge, #reverse_merge!, #slice, #slice!, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #to_param, #to_xml, #with_indifferent_access

Constructor Details

This class inherits a constructor from ActiveModel::Errors

Instance Method Details

- (Object) from_array(messages, save_cache = false)

Grabs errors from an array of messages (like ActiveRecord::Validations) The second parameter directs the errors cache to be cleared (default) or not (by passing true)



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'activeresource/lib/active_resource/validations.rb', line 14

def from_array(messages, save_cache = false)
  clear unless save_cache
  humanized_attributes = Hash[@base.attributes.keys.map { |attr_name| [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

- (Object) from_json(json, save_cache = false)

Grabs errors from a json response.



29
30
31
32
# File 'activeresource/lib/active_resource/validations.rb', line 29

def from_json(json, save_cache = false)
  array = Array.wrap(ActiveSupport::JSON.decode(json)['errors']) rescue []
  from_array array, save_cache
end

- (Object) from_xml(xml, save_cache = false)

Grabs errors from an XML response.



35
36
37
38
# File 'activeresource/lib/active_resource/validations.rb', line 35

def from_xml(xml, save_cache = false)
  array = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue []
  from_array array, save_cache
end