Class: Restforce::DB::Associations::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/restforce/db/associations/base.rb

Overview

Restforce::DB::Associations::Base defines an association between two mappings in the Registry.

Direct Known Subclasses

BelongsTo, ForeignKey

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, through: nil, build: true) ⇒ Base

Public: Initialize a new Restforce::DB::Associations::Base.

name - The name of the ActiveRecord association to construct. through - The name of the lookup field on the Salesforce record. build - A Boolean indicating whether or not the association chain

should be built out for new records.


19
20
21
22
23
# File 'lib/restforce/db/associations/base.rb', line 19

def initialize(name, through: nil, build: true)
  @name = name.to_sym
  @lookup = through.is_a?(Array) ? through.map(&:to_s) : through.to_s
  @build = build
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



11
12
13
# File 'lib/restforce/db/associations/base.rb', line 11

def cache
  @cache
end

#lookupObject (readonly)

Returns the value of attribute lookup.



11
12
13
# File 'lib/restforce/db/associations/base.rb', line 11

def lookup
  @lookup
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/restforce/db/associations/base.rb', line 11

def name
  @name
end

Instance Method Details

#build(_database_record, _salesforce_record, _cache) ⇒ Object

Public: Build a record or series of records for the association defined by this class. Must be overridden in subclasses.

Raises a NotImplementedError.

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/restforce/db/associations/base.rb', line 29

def build(_database_record, _salesforce_record, _cache)
  raise NotImplementedError
end

#fieldsObject

Public: Get a list of fields which should be included in the Salesforce record’s lookups for any mapping including this association.

Returns a list of Salesforce fields this record should return.



38
39
40
# File 'lib/restforce/db/associations/base.rb', line 38

def fields
  [*lookup]
end

#synced_for?(instance) ⇒ Boolean

Public: Has a record for this association already been synchronized for the supplied instance?

instance - A Restforce::DB::Instances::Base.

Returns a Boolean.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/restforce/db/associations/base.rb', line 48

def synced_for?(instance)
  association_id = associated_salesforce_id(instance)
  return false unless association_id

  base_class = instance.mapping.database_model
  reflection = base_class.reflect_on_association(name)

  mappings_for(reflection).any? do |mapping|
    reflection.klass.exists?(mapping.lookup_column => association_id)
  end
end