Module: FmRest::Spyke::Model::Orm
- Extended by:
- ActiveSupport::Concern
- Included in:
- FmRest::Spyke::Model, Attributes
- Defined in:
- lib/fmrest/spyke/model/orm.rb
Overview
This module adds and extends various ORM features in Spyke models, including custom query methods, remote script execution and exception-raising persistence methods.
Class Method Summary collapse
-
.all ⇒ Object
Spyke override -- Use FmRest's Relation instead of Spyke's vanilla one.
-
.create!(attributes = {}) ⇒ Object
Exception-raising version of
#create
. -
.fetch(options = {}) ⇒ Object
Spyke override -- properly sets limit, offset and other options, as well as using the appropriate HTTP method/URL depending on whether there's a query present in the current scope.
Instance Method Summary collapse
-
#destroy(options = {}) ⇒ Object
Spyke override -- Adds support for Data API script execution.
-
#reload(options = {}) ⇒ Object
Spyke override -- Adds support for Data API script execution.
-
#save(options = {}) ⇒ true, false
Spyke override -- Adds a number of features to original
#save
:. -
#save!(options = {}) ⇒ true
Exception-raising version of
#save
. -
#update!(new_attributes, options = {}) ⇒ Object
Exception-raising version of
#update
. - #validate!(context = nil) ⇒ Object
Class Method Details
.all ⇒ Object
Spyke override -- Use FmRest's Relation instead of Spyke's vanilla one
33 34 35 |
# File 'lib/fmrest/spyke/model/orm.rb', line 33 def all current_scope || Relation.new(self, uri: uri) end |
.create!(attributes = {}) ⇒ Object
Exception-raising version of #create
77 78 79 |
# File 'lib/fmrest/spyke/model/orm.rb', line 77 def create!(attributes = {}) new(attributes).tap(&:save!) end |
.fetch(options = {}) ⇒ Object
Spyke override -- properly sets limit, offset and other options, as well as using the appropriate HTTP method/URL depending on whether there's a query present in the current scope.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fmrest/spyke/model/orm.rb', line 48 def fetch( = {}) if current_scope.has_query? scope = extend_scope_with_fm_params(current_scope, prefixed: false) scope = scope.where(query: scope.query_params) scope = scope.with(FmRest::V1::find_path(layout)) else scope = extend_scope_with_fm_params(current_scope, prefixed: true) end previous, self.current_scope = current_scope, scope # The DAPI returns a 401 "No records match the request" error when # nothing matches a _find request, so we need to catch it in order # to provide sane behavior (i.e. return an empty resultset) begin current_scope.has_query? ? scoped_request(:post) : super() rescue FmRest::APIError::NoMatchingRecordsError => e raise e if [:raise_on_no_matching_records] ::Spyke::Result.new({}) end ensure self.current_scope = previous end |
Instance Method Details
#destroy(options = {}) ⇒ Object
Spyke override -- Adds support for Data API script execution.
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/fmrest/spyke/model/orm.rb', line 167 def destroy( = {}) # For whatever reason the Data API wants the script params as query # string params for DELETE requests, making this more complicated # than it should be script_query_string = if .has_key?(:script) "?" + Faraday::Utils.build_query(FmRest::V1.convert_script_params([:script])) else "" end self.attributes = delete(uri.to_s + script_query_string) end |
#reload(options = {}) ⇒ Object
Spyke override -- Adds support for Data API script execution.
185 186 187 188 189 190 191 |
# File 'lib/fmrest/spyke/model/orm.rb', line 185 def reload( = {}) scope = self.class scope = scope.script([:script]) if .has_key?(:script) reloaded = scope.find(__record_id) self.attributes = reloaded.attributes self.__mod_id = reloaded.mod_id end |
#save(options = {}) ⇒ true, false
Spyke override -- Adds a number of features to original #save
:
- Validations
- Data API scripts execution
- Refresh of dirty attributes
129 130 131 132 133 134 135 136 |
# File 'lib/fmrest/spyke/model/orm.rb', line 129 def save( = {}) callback = persisted? ? :update : :create return false unless perform_save_validations(callback, ) return false unless perform_save_persistence(callback, ) true end |
#save!(options = {}) ⇒ true
Exception-raising version of #save
.
146 147 148 |
# File 'lib/fmrest/spyke/model/orm.rb', line 146 def save!( = {}) save(.merge(raise_validation_errors: true)) end |
#update!(new_attributes, options = {}) ⇒ Object
Exception-raising version of #update
.
157 158 159 160 |
# File 'lib/fmrest/spyke/model/orm.rb', line 157 def update!(new_attributes, = {}) self.attributes = new_attributes save!() end |
#validate!(context = nil) ⇒ Object
196 197 198 |
# File 'lib/fmrest/spyke/model/orm.rb', line 196 def validate!(context = nil) valid?(context) || raise_validation_error end |