module LogicalModel::RESTActions::InstanceMethods
Public Instance Methods
_create(params = {})
click to toggle source
creates model.
returns: @return false if model invalid @return nil if there was a connection problem @return created model ID if successfull
@example Usage:
@person = Person.new(params[:person]) @person.create( non_attribute_param: "value" )
# File lib/logical_model/rest_actions.rb, line 54 def _create(params = {}) unless params[:ignore_validation] return false unless valid? end params = { self.json_root => self.attributes }.merge(params) params = self.class.merge_key(params) response = Typhoeus::Request.post( self.class.resource_uri, body: params, timeout: self.class.timeout ) self.last_response_code = response.code if response.code == 201 || response.code == 202 log_ok(response) if self.respond_to?('id=') self.id = ActiveSupport::JSON.decode(response.body)["id"] else true end elsif response.code == 400 log_failed(response) ws_errors = ActiveSupport::JSON.decode(response.body)["errors"] ws_errors.each_key do |k| self.errors.add k, ws_errors[k] end return false else log_failed(response) return nil end end
_destroy(params={})
click to toggle source
Destroy object
Usage:
@person.destroy
# File lib/logical_model/rest_actions.rb, line 149 def _destroy(params={}) self.class.delete(self.id,params) end
_save()
click to toggle source
Saves Objects attributes
Returns false if Object#valid? is false. Returns updated object if successfull. Returns nil if update failed
Usage:
@person.save
# File lib/logical_model/rest_actions.rb, line 125 def _save self.attributes = attributes return false unless valid? sending_params = self.attributes sending_params.delete(:id) params = { self.json_root => sending_params } params = self.class.merge_key(params) response = Typhoeus::Request.put( self.class.resource_uri(id), params: params, timeout: self.class.timeout ) if response.code == 200 log_ok(response) return self else log_failed(response) return nil end end
_update(params)
click to toggle source
Updates Objects attributes, this will only send attributes passed as arguments
Returns false if Object#valid? is false. Returns updated object if successfull. Returns nil if update failed
Usage:
@person.update(params[:person])
# File lib/logical_model/rest_actions.rb, line 93 def _update(params) self.attributes = params[self.json_root] unless params[:ignore_validation] return false unless valid? end params = self.class.merge_key(params) response = Typhoeus::Request.put( self.class.resource_uri(id), params: params, timeout: self.class.timeout ) if response.code == 200 log_ok(response) return self else log_failed(response) return nil end end
create(params={})
click to toggle source
# File lib/logical_model/rest_actions.rb, line 11 def create(params={}) run_callbacks :save do run_callbacks :create do _create(params) end end end
destroy(params={})
click to toggle source
# File lib/logical_model/rest_actions.rb, line 27 def destroy(params={}) run_callbacks :destroy do _destroy(params) end end
save()
click to toggle source
# File lib/logical_model/rest_actions.rb, line 19 def save run_callbacks :save do run_callbacks new_record?? :create : :update do _save end end end
update(params)
click to toggle source
@param params [Hash] parameters to be sent to service
# File lib/logical_model/rest_actions.rb, line 34 def update(params) run_callbacks :save do run_callbacks :update do _update(params) end end end