module FmRest::Spyke::Model

Public Instance Methods

uri() click to toggle source

Spyke override – Uses `__record_id` for building the record URI.

# File lib/fmrest/spyke/model/http.rb, line 38
def uri
  ::Spyke::Path.new(@uri_template, fmrest_uri_attributes) if @uri_template
end

Private Instance Methods

fmrest_uri_attributes() click to toggle source
# File lib/fmrest/spyke/model/http.rb, line 54
def fmrest_uri_attributes
  if persisted?
    { __record_id: __record_id }
  else
    # NOTE: it seems silly to be calling attributes.slice(:__record_id)
    # when the record is supposed to not have a record_id set (since
    # persisted? is false here), but it makes sense in the context of how
    # Spyke works:
    #
    # When calling Model.find(id), Spyke will internally create a scope
    # with .where(primary_key => id) and call .find_one on it. Then,
    # somewhere down the line Spyke creates a new empty instance of the
    # current model class to get its .uri property (the one we're
    # partially building through this method and which contains these URI
    # attributes). When initializing a record Spyke first forcefully
    # assigns the .where()-set attributes from the current scope onto
    # that instance's attributes hash, which then leads us right here,
    # where we might have __record_id assigned as a scope attribute:
    attributes.slice(:__record_id)
  end
end
resolve_path_from_action(action) click to toggle source

Spyke override (private) – Use `__record_id` instead of `id`

# File lib/fmrest/spyke/model/http.rb, line 46
def resolve_path_from_action(action)
  case action
  when Symbol then uri.join(action)
  when String then ::Spyke::Path.new(action, fmrest_uri_attributes)
  else uri
  end
end