Class: Restforce::DB::Associations::BelongsTo

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

Overview

Restforce::DB::Associations::BelongsTo defines a relationship in which a Salesforce ID on the named database association exists on this Mapping’s Salesforce record.

Instance Attribute Summary

Attributes inherited from Base

#cache, #lookup, #name

Instance Method Summary collapse

Methods inherited from Base

#fields, #initialize, #synced_for?

Constructor Details

This class inherits a constructor from Restforce::DB::Associations::Base

Instance Method Details

#build(database_record, salesforce_record, cache = AssociationCache.new(database_record)) ⇒ Object

Public: Construct a database record from the Salesforce records associated with the supplied parent Salesforce record.

database_record - An instance of an ActiveRecord::Base subclass. salesforce_record - A Hashie::Mash representing a Salesforce object. cache - A Restforce::DB::AssociationCache (optional).

Returns an Array of constructed association records.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/restforce/db/associations/belongs_to.rb', line 20

def build(database_record, salesforce_record, cache = AssociationCache.new(database_record))
  return [] unless build?

  @cache = cache

  lookups = {}
  attributes = {}
  instances = []

  for_mappings(database_record) do |mapping, lookup|
    lookup_id = deep_lookup(salesforce_record, lookup)
    lookups[mapping.lookup_column] = lookup_id

    # If the referenced database record already exists, we can short-
    # circuit the accumulation of attributes here.
    next if cache.find(mapping.database_model, mapping.lookup_column => lookup_id)

    instance = mapping.salesforce_record_type.find(lookup_id)

    # If any of the mappings are invalid, short-circuit the creation of
    # the associated record.
    return [] unless instance

    instances << instance
    attrs = mapping.convert(mapping.database_model, instance.attributes)
    attributes = attributes.merge(attrs)
  end

  constructed_records(database_record, lookups, attributes) do |associated|
    instances.flat_map { |i| nested_records(database_record, associated, i) }
  end
ensure
  @cache = nil
end

#lookups(database_record) ⇒ Object

Public: Get a Hash of Lookup IDs for a specified database record, based on the current record for this association.

database_record - An instance of an ActiveRecord::Base subclass.

Returns a Hash.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/restforce/db/associations/belongs_to.rb', line 61

def lookups(database_record)
  ids = {}

  for_mappings(database_record) do |mapping, lookup|
    # It's possible to define a belongs_to association in a Mapping
    # for what is actually a one-to-many association on the
    # ActiveRecord object, so we always treat the result as an Array.
    associated = Array(database_record.association(name).reader)
    ids[lookup] = associated.first.send(mapping.lookup_column) unless associated.empty?
  end

  ids
end