Class: Restforce::DB::Associations::Base
- Inherits:
-
Object
- Object
- Restforce::DB::Associations::Base
- 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
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#lookup ⇒ Object
readonly
Returns the value of attribute lookup.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#build(_database_record, _salesforce_record, _cache) ⇒ Object
Public: Build a record or series of records for the association defined by this class.
-
#fields ⇒ Object
Public: Get a list of fields which should be included in the Salesforce record’s lookups for any mapping including this association.
-
#initialize(name, through: nil, build: true) ⇒ Base
constructor
Public: Initialize a new Restforce::DB::Associations::Base.
-
#synced_for?(instance) ⇒ Boolean
Public: Has a record for this association already been synchronized for the supplied instance?.
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
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
11 12 13 |
# File 'lib/restforce/db/associations/base.rb', line 11 def cache @cache end |
#lookup ⇒ Object (readonly)
Returns the value of attribute lookup.
11 12 13 |
# File 'lib/restforce/db/associations/base.rb', line 11 def lookup @lookup end |
#name ⇒ Object (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.
29 30 31 |
# File 'lib/restforce/db/associations/base.rb', line 29 def build(_database_record, _salesforce_record, _cache) raise NotImplementedError end |
#fields ⇒ Object
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.
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 |