module FmRest::Spyke::Model::Associations

This module adds portal support to Spyke models.

Public Instance Methods

__new_portal_record_info=(data) click to toggle source

Takes care of updating the new portal record's recordIds and modIds.

Called when saving a record with freshly added portal records, this method is not meant to be called manually.

@param [Hash] data The hash containing newPortalData from the DAPI

response
# File lib/fmrest/spyke/model/associations.rb, line 137
def __new_portal_record_info=(data)
  data.each do |d|
    table_name = d[:tableName]

    portal_new_records =
      portals.detect { |p| p.portal_key == table_name }.select { |r| !r.persisted? }

    # The DAPI provides only one recordId for the entire portal in the
    # newPortalRecordInfo object. This appears to be the recordId of
    # the last portal record created, so we assume all portal records
    # coming before it must have sequential recordIds up to the one we
    # do have.
    portal_new_records.reverse_each.with_index do |record, i|
      record.__record_id = d[:recordId].to_i - i

      # New records get a fresh modId
      record.__mod_id = 0
    end
  end
end
association(name) click to toggle source

Spyke override – Keep a cache of loaded portals. Spyke's default behavior is to reload the association each time.

Calls superclass method
# File lib/fmrest/spyke/model/associations.rb, line 58
def association(name)
  @loaded_portals ||= {}

  if @loaded_portals.has_key?(name.to_sym)
    return @loaded_portals[name.to_sym]
  end

  super.tap do |assoc|
    next unless assoc.kind_of?(FmRest::Spyke::Portal)
    @loaded_portals[name.to_sym] = assoc
  end
end
changed?() click to toggle source

Override ActiveModel::Dirty's method to include awareness of `@embedded_in_portal`

Calls superclass method
# File lib/fmrest/spyke/model/associations.rb, line 126
def changed?
  super || embedded_in_portal?
end
changes_applied() click to toggle source

Override ActiveModel::Dirty's method to include clearing of `@embedded_in_portal` and `@marked_for_destruction`

Calls superclass method
# File lib/fmrest/spyke/model/associations.rb, line 117
def changes_applied
  super
  @embedded_in_portal = nil
  @marked_for_destruction = nil
end
embedded_in_portal() click to toggle source

Signals that this record has been embedded in a portal so we can make sure to include it in the next update request

# File lib/fmrest/spyke/model/associations.rb, line 106
def embedded_in_portal
  @embedded_in_portal = true
end
embedded_in_portal?() click to toggle source
# File lib/fmrest/spyke/model/associations.rb, line 110
def embedded_in_portal?
  !!@embedded_in_portal
end
has_portal(name, options = {}) click to toggle source

Based on Spyke's `has_many`, but creates a special Portal association instead.

@option :portal_key [String] The key used for the portal in the FM

Data JSON portalData

@option :attribute_prefix [String] The prefix used for portal

attributes in the FM Data JSON

@example

class Person < FmRest::Spyke::Base
  has_portal :jobs, portal_key: "JobsTable", attribute_prefix: "Job"
end
# File lib/fmrest/spyke/model/associations.rb, line 42
def has_portal(name, options = {})
  create_association(name, Portal, options)

  # Store options for SpykeFormatter to use if needed
  portal_key = options[:portal_key] || name
  self.portal_options = portal_options.merge(portal_key.to_s => options.dup.merge(name: name.to_s)).freeze

  define_method "#{name.to_s.singularize}_ids" do
    association(name).map(&:id)
  end
end
mark_for_deletion()
mark_for_destruction() click to toggle source

Signals that this record has been marked for being deleted next time its parent record is saved (e.g. in a portal association)

This method is named after ActiveRecord's namesake

# File lib/fmrest/spyke/model/associations.rb, line 93
def mark_for_destruction
  @marked_for_destruction = true
end
Also aliased as: mark_for_deletion
marked_for_deletion?()
marked_for_destruction?() click to toggle source
# File lib/fmrest/spyke/model/associations.rb, line 98
def marked_for_destruction?
  !!@marked_for_destruction
end
Also aliased as: marked_for_deletion?
portals() click to toggle source

@return [Array<FmRest::Spyke::Portal>] A collection of portal

relations for the record
# File lib/fmrest/spyke/model/associations.rb, line 80
def portals
  self.class.associations.each_with_object([]) do |(key, _), portals|
    candidate = association(key)
    next unless candidate.kind_of?(FmRest::Spyke::Portal)
    portals << candidate
  end
end
reload(*_) click to toggle source

Spyke override – Add portals awareness

Calls superclass method
# File lib/fmrest/spyke/model/associations.rb, line 73
def reload(*_)
  super.tap { @loaded_portals = nil }
end

Private Instance Methods

remove_marked_for_destruction() click to toggle source
# File lib/fmrest/spyke/model/associations.rb, line 160
def remove_marked_for_destruction
  portals.each(&:_remove_marked_for_destruction)
end