Module: LogicalModel::RESTActions::InstanceMethods
- Defined in:
- lib/logical_model/rest_actions.rb
Instance Method Summary collapse
-
#_create(params = {}) ⇒ Object
creates model.
-
#_destroy(params = {}) ⇒ Object
Destroy object.
-
#_save ⇒ Object
Saves Objects attributes.
-
#_update(params) ⇒ Object
Updates Objects attributes, this will only send attributes passed as arguments.
- #create(params = {}) ⇒ Object
- #destroy(params = {}) ⇒ Object
- #save ⇒ Object
- #update(params) ⇒ Object
Instance Method Details
#_create(params = {}) ⇒ Object
creates model.
returns:
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 |
# 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 = {}) ⇒ Object
Destroy object
Usage:
@person.destroy
149 150 151 |
# File 'lib/logical_model/rest_actions.rb', line 149 def _destroy(params={}) self.class.delete(self.id,params) end |
#_save ⇒ Object
Saves Objects attributes
Returns false if Object#valid? is false. Returns updated object if successfull. Returns nil if update failed
Usage:
@person.save
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# 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) ⇒ Object
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])
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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 = {}) ⇒ Object
11 12 13 14 15 16 17 |
# 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 = {}) ⇒ Object
27 28 29 30 31 |
# File 'lib/logical_model/rest_actions.rb', line 27 def destroy(params={}) run_callbacks :destroy do _destroy(params) end end |
#save ⇒ Object
19 20 21 22 23 24 25 |
# 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) ⇒ Object
34 35 36 37 38 39 40 41 |
# 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 |