Class: ActiveForce::Association::AbstractProjectionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_force/association/eager_load_projection_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association, parent_association_field = nil, query_fields = nil) ⇒ AbstractProjectionBuilder

Returns a new instance of AbstractProjectionBuilder.



35
36
37
38
39
# File 'lib/active_force/association/eager_load_projection_builder.rb', line 35

def initialize(association, parent_association_field = nil, query_fields = nil)
  @association = association
  @parent_association_field = parent_association_field
  @query_fields = query_fields
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



33
34
35
# File 'lib/active_force/association/eager_load_projection_builder.rb', line 33

def association
  @association
end

#parent_association_fieldObject (readonly)

Returns the value of attribute parent_association_field.



33
34
35
# File 'lib/active_force/association/eager_load_projection_builder.rb', line 33

def parent_association_field
  @parent_association_field
end

#query_fieldsObject (readonly)

Returns the value of attribute query_fields.



33
34
35
# File 'lib/active_force/association/eager_load_projection_builder.rb', line 33

def query_fields
  @query_fields
end

Instance Method Details

#apply_association_scope(query) ⇒ Object



45
46
47
48
49
50
# File 'lib/active_force/association/eager_load_projection_builder.rb', line 45

def apply_association_scope(query)
  return query unless association.scoped?
  raise InvalidEagerLoadAssociation, "Cannot use scopes that expect arguments: #{association.relation_name}" if association.scoped_as.arity.positive?

  query.instance_exec(&association.scoped_as)
end

#projectionsObject



41
42
43
# File 'lib/active_force/association/eager_load_projection_builder.rb', line 41

def projections
  raise "Must define #{self.class.name}#projections"
end

#query_with_association_fieldsObject

Use ActiveForce::Query to build a subquery for the SFDC relationship name. Per SFDC convention, the name needs to be pluralized



56
57
58
59
60
61
# File 'lib/active_force/association/eager_load_projection_builder.rb', line 56

def query_with_association_fields
  relationship_name = association.sfdc_association_field
  selected_fields = query_fields || association.relation_model.fields
  query = ActiveQuery.new(association.relation_model, relationship_name).select(*selected_fields)
  apply_association_scope(query)
end