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

Instance Method Summary collapse

Class Method Details

.allObject

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

Parameters:

  • attributes (Hash) (defaults to: {})

    the attributes to initialize the record with



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.

Examples:

Person.query(first_name: "Stefan").fetch # -> POST .../_find

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :raise_on_no_matching_records (Boolean)

    whether to raise APIError::NoMatchingRecordsError when no records match (FM error 401). If not given it returns an empty resultset.



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(options = {})
  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 options[: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.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :script (String)

    the name of a FileMaker script to execute upon deletion



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fmrest/spyke/model/orm.rb', line 167

def destroy(options = {})
  # 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 options.has_key?(:script)
      "?" + Faraday::Utils.build_query(FmRest::V1.convert_script_params(options[: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.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :script (String)

    the name of a FileMaker script to execute upon deletion

  • :script (String)

    the name of a FileMaker script to execute upon deletion



185
186
187
188
189
190
191
# File 'lib/fmrest/spyke/model/orm.rb', line 185

def reload(options = {})
  scope = self.class
  scope = scope.script(options[:script]) if options.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

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :script (String)

    the name of a FileMaker script to execute upon saving

  • :raise_validation_errors (Boolean)

    whether to raise an exception if validations fail

Returns:

  • (true)

    if saved successfully

  • (false)

    if validations or persistence failed



129
130
131
132
133
134
135
136
# File 'lib/fmrest/spyke/model/orm.rb', line 129

def save(options = {})
  callback = persisted? ? :update : :create

  return false unless perform_save_validations(callback, options)
  return false unless perform_save_persistence(callback, options)

  true
end

#save!(options = {}) ⇒ true

Exception-raising version of #save.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :script (String)

    the name of a FileMaker script to execute upon saving

  • :raise_validation_errors (Boolean)

    whether to raise an exception if validations fail

Returns:

  • (true)

    if saved successfully

Raises:

  • if validations or presistence failed



146
147
148
# File 'lib/fmrest/spyke/model/orm.rb', line 146

def save!(options = {})
  save(options.merge(raise_validation_errors: true))
end

#update!(new_attributes, options = {}) ⇒ Object

Exception-raising version of #update.

Parameters:

  • new_attributes (Hash)

    a hash of record attributes to update the record with

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :script (String)

    the name of a FileMaker script to execute upon saving

  • :raise_validation_errors (Boolean)

    whether to raise an exception if validations fail



157
158
159
160
# File 'lib/fmrest/spyke/model/orm.rb', line 157

def update!(new_attributes, options = {})
  self.attributes = new_attributes
  save!(options)
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