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_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
13 14 15 16 17 |
# File 'lib/api_model/instance_methods.rb', line 13 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.
74 75 76 |
# File 'lib/api_model/instance_methods.rb', line 74 def properties_hash self.to_hash.only(*self.class.attribute_set.collect(&:name)).except(:persisted).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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/api_model/instance_methods.rb', line 49 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_body end else run_callbacks :unsuccessful_save do set_errors_from_hash response.fetch_from_body(errors_root) end end response_success 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.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/api_model/instance_methods.rb', line 21 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 || obj.errors.add field.to_sym, end else obj.errors.add field.to_sym, end end end |