Module: ApiModel::InstanceMethods
Instance Method Summary collapse
- #initialize(attributes = nil) ⇒ Object
-
#properties_hash ⇒ Object
Returns all the defined attributes in a hash without the :persisted attribute which was added automatically.
-
#save(path, body = nil, options = {}) ⇒ Object
Sends a request to the api to update a resource.
-
#set_error_on_self_or_child(field, messages, obj = self) ⇒ Object
If the field to apply errors to is another ApiModel instance, call
set_errors_from_hashon it. -
#set_errors_from_hash(errors_hash, obj = self) ⇒ Object
Convenience method to handle error hashes and set them as ActiveModel errors on instances.
Instance Method Details
#initialize(attributes = nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/api_model/instance_methods.rb', line 18 def initialize(attributes = nil) run_callbacks :initialize do super attributes end end |
#properties_hash ⇒ Object
Returns all the defined attributes in a hash without the :persisted attribute which was added automatically.
This is useful for when you need to pass the entire object back to an API, or if you want to serialize the object.
89 90 91 92 |
# File 'lib/api_model/instance_methods.rb', line 89 def properties_hash ActiveSupport::Deprecation.warn "properties_hash() is deprecated and may be removed from future releases, use as_json() instead.", caller self.as_json.with_indifferent_access end |
#save(path, body = nil, options = {}) ⇒ Object
Sends a request to the api to update a resource. If the response was successful, then it will update the instance with any changes which the API has returned. If not, it will set ActiveModel errors.
The default request type is PUT, but you can override this by setting :request_method in the options hash.
It also includes 3 callbacks which you can hook onto; save, which is the entire method, whether the API request was successful or not, and successful_save and unsuccessful_save which are triggered on successful or unsuccessful responses.
By default it uses the ApiModel::Builder::Hash builder rather than using the normal method of using the class, or api config builders. This is to avoid building new objects from the response, but can be easily overriden by passing in :builder in the options hash.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/api_model/instance_methods.rb', line 64 def save(path, body=nil, ={}) request_method = .delete(:request_method) || :put errors_root = .delete(:json_errors_root) || self.class.api_model_configuration.json_errors_root run_callbacks :save do response = self.class.call_api_with_json request_method, path, body, .reverse_merge(builder: ApiModel::Builder::Hash.new) response_success = response.http_response.api_call.success? if response_success run_callbacks :successful_save do update_attributes response.response_build_hash end else run_callbacks :unsuccessful_save do set_errors_from_hash response.fetch_from_body(errors_root) end end response_success end end |
#set_error_on_self_or_child(field, messages, obj = self) ⇒ Object
If the field to apply errors to is another ApiModel instance, call set_errors_from_hash on it. Otherwise, go ahead and treat it as a normal ActiveModel error on the current obj instance.
42 43 44 45 46 47 48 |
# File 'lib/api_model/instance_methods.rb', line 42 def set_error_on_self_or_child(field, , obj = self) if obj.respond_to?(field.to_sym) && obj.send(field.to_sym).respond_to?(:set_error_on_self_or_child) obj.send(field.to_sym).set_errors_from_hash else obj.errors.add field.to_sym, end end |
#set_errors_from_hash(errors_hash, obj = self) ⇒ Object
Convenience method to handle error hashes and set them as ActiveModel errors on instances. Using the ‘obj`, you can move the errors on to child classes if needed.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/api_model/instance_methods.rb', line 26 def set_errors_from_hash(errors_hash, obj = self) return false unless errors_hash.is_a? Hash errors_hash.each do |field,| if .is_a?(Array) .each do || set_error_on_self_or_child field, , obj end else set_error_on_self_or_child field, , obj end end end |