Module: FmRest::Spyke::Model::Orm

Extended by:
ActiveSupport::Concern
Included in:
FmRest::Spyke::Model, Attributes
Defined in:
lib/fmrest/spyke/model/orm.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



30
31
32
33
# File 'lib/fmrest/spyke/model/orm.rb', line 30

def all
  # Use FmRest's Relation instead of Spyke's vanilla one
  current_scope || Relation.new(self, uri: uri)
end

.create!(attributes = {}) ⇒ Object

API-error-raising version of #create



67
68
69
# File 'lib/fmrest/spyke/model/orm.rb', line 67

def create!(attributes = {})
  new(attributes).tap(&:save!)
end

.execute_script(script_name, param: nil) ⇒ Object



71
72
73
74
75
# File 'lib/fmrest/spyke/model/orm.rb', line 71

def execute_script(script_name, param: nil)
  params = {}
  params = {"script.param" => param} unless param.nil?
  request(:get, FmRest::V1::script_path(layout, script_name), params)
end

.fetchObject

Extended fetch to allow properly setting 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, e.g.:

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


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fmrest/spyke/model/orm.rb', line 41

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

Overwrite Spyke's destroy to provide Data API script execution



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fmrest/spyke/model/orm.rb', line 132

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



152
153
154
155
156
157
158
# File 'lib/fmrest/spyke/model/orm.rb', line 152

def reload(options = {})
  scope = self.class
  scope = scope.script(options[:script]) if options.has_key?(:script)
  reloaded = scope.find(id)
  self.attributes = reloaded.attributes
  self.mod_id = reloaded.mod_id
end

#save(options = {}) ⇒ Object

Overwrite Spyke's save to provide a number of features:

  • Validations
  • Data API scripts execution
  • Refresh of dirty attributes


117
118
119
120
121
122
123
124
# File 'lib/fmrest/spyke/model/orm.rb', line 117

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 = {}) ⇒ Object



126
127
128
# File 'lib/fmrest/spyke/model/orm.rb', line 126

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

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

API-error-raising version of #update



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

def update!(new_attributes, options = {})
  self.attributes = new_attributes
  save!(options)
end

#validate!(context = nil) ⇒ Object



163
164
165
# File 'lib/fmrest/spyke/model/orm.rb', line 163

def validate!(context = nil)
  valid?(context) || raise_validation_error
end