Class: ActiveForce::Association::EagerLoadBuilderForNestedIncludes

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relations, current_sobject, parent_association_field = nil, query_fields = nil) ⇒ EagerLoadBuilderForNestedIncludes

Returns a new instance of EagerLoadBuilderForNestedIncludes.



16
17
18
19
20
21
22
23
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 16

def initialize(relations, current_sobject, parent_association_field = nil, query_fields = nil)
  @relations = [relations].flatten
  @current_sobject = current_sobject
  @association_mapping = {}
  @parent_association_field = parent_association_field
  @query_fields = query_fields
  @fields = []
end

Instance Attribute Details

#association_mappingObject (readonly)

Returns the value of attribute association_mapping.



14
15
16
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 14

def association_mapping
  @association_mapping
end

#current_sobjectObject (readonly)

Returns the value of attribute current_sobject.



14
15
16
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 14

def current_sobject
  @current_sobject
end

#fieldsObject (readonly)

Returns the value of attribute fields.



14
15
16
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 14

def fields
  @fields
end

#parent_association_fieldObject (readonly)

Returns the value of attribute parent_association_field.



14
15
16
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 14

def parent_association_field
  @parent_association_field
end

#query_fieldsObject (readonly)

Returns the value of attribute query_fields.



14
15
16
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 14

def query_fields
  @query_fields
end

#relationsObject (readonly)

Returns the value of attribute relations.



14
15
16
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 14

def relations
  @relations
end

Class Method Details

.build(relations, current_sobject, parent_association_field = nil, query_fields = nil) ⇒ Object



9
10
11
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 9

def build(relations, current_sobject, parent_association_field = nil, query_fields = nil)
  new(relations, current_sobject, parent_association_field, query_fields).projections
end

Instance Method Details

#build_hash_includes(relation, model = current_sobject, parent_association_field = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 52

def build_hash_includes(relation, model = current_sobject, parent_association_field = nil)
  relation.each do |key, value|
    association = model.associations[key]
    raise InvalidAssociationError, "Association named #{key} was not found on #{model}" if association.nil?
    case association
    when ActiveForce::Association::BelongsToAssociation
      build_includes(association)
      nested_query = build_relation_for_belongs_to(association, value)
      fields.concat(nested_query[:fields])
      association_mapping.merge!(nested_query[:association_mapping])
    else
      nested_query = build_relation(association, value)
      fields.concat(nested_query[:fields])
      association_mapping.merge!(nested_query[:association_mapping])
    end
  end
end

#build_includes(association) ⇒ Object



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

def build_includes(association)
  fields.concat(EagerLoadProjectionBuilder.build(association, parent_association_field, query_fields_for(association)))
  association_mapping[association.sfdc_association_field.downcase] = association.relation_name
end

#projectionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_force/association/eager_load_builder_for_nested_includes.rb', line 26

def projections
  relations.each do |relation|
    case relation
    when Symbol
      association = current_sobject.associations[relation]
      raise InvalidAssociationError, "Association named #{relation} was not found on #{current_sobject}" if association.nil?
      build_includes(association)
    when Hash
      build_hash_includes(relation)
    end
  end
  { fields: fields, association_mapping: association_mapping }
end

#query_fields_for(association) ⇒ Object



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

def query_fields_for(association)
  return nil if query_fields.blank?
  query_fields_with_association = query_fields.find { |nested_field| nested_field[association.relation_name].present? }
  return nil if query_fields_with_association.blank?
  query_fields_with_association[association.relation_name].map { |field| association.relation_model.mappings[field] }
end