Module: RPCMapper::AssociationPreload::ClassMethods

Defined in:
lib/rpc_mapper/association_preload.rb

Instance Method Summary collapse

Instance Method Details

#eager_load_associations(original_results, relation) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
# File 'lib/rpc_mapper/association_preload.rb', line 4

def eager_load_associations(original_results, relation)
  relation.includes_values.each do |association_id|
    association = self.defined_associations[association_id.to_sym]
    force_fresh = relation.fresh_value

    unless association
      raise(
        RPCMapper::AssociationNotFound,
        "no such association (#{association_id})"
      )
    end

    unless association.eager_loadable?
      raise(
        RPCMapper::AssociationPreloadNotSupported,
        "delayed execution options (block options) cannot be used for eager loaded associations"
      )
    end

    options = association.options

    case association.type
    when :has_many, :has_one
      fks = original_results.collect { |record| record.send(association.primary_key) }.compact

      pre_records = association.target_klass.where(association.foreign_key => fks).all(:fresh => force_fresh)

      original_results.each do |record|
        pk = record.send(association.primary_key)
        relevant_records = pre_records.select { |r| r.send(association.foreign_key) == pk }

        relevant_records = if association.collection?
          scope = association.scope(record).where(association.foreign_key => pk)
          scope.records = relevant_records
          scope
        else
          relevant_records.first
        end

        record.send("#{association.id}=", relevant_records)
      end

    when :belongs_to
      fks = original_results.collect { |record| record.send(association.foreign_key) }.compact

      pre_records = association.target_klass.where(association.primary_key => fks).all(:fresh => force_fresh)

      original_results.each do |record|
        fk = record.send(association.foreign_key)
        relevant_records = pre_records.select { |r| r.send(association.primary_key) == fk }.first
        record.send("#{association.id}=", relevant_records)
      end
    end
  end
end