Class: EagerGroup::Preloader

Inherits:
Object
  • Object
show all
Defined in:
lib/eager_group/preloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, records, eager_group_values) ⇒ Preloader

Returns a new instance of Preloader.



5
6
7
8
9
# File 'lib/eager_group/preloader.rb', line 5

def initialize(klass, records, eager_group_values)
  @klass = klass
  @records = Array.wrap(records).compact.uniq
  @eager_group_values = eager_group_values
end

Instance Method Details

#runObject

Preload aggregate functions



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
# File 'lib/eager_group/preloader.rb', line 12

def run
  primary_key = @klass.primary_key
  @eager_group_values.each do |eager_group_value|
    definition_key, arguments =
      eager_group_value.is_a?(Array) ? [eager_group_value.shift, eager_group_value] : [eager_group_value, nil]

    if definition_key.is_a?(Hash)
      association_name, definition_key = *definition_key.first
      @records = @records.flat_map { |record| record.send(association_name) }
      next if @records.empty?

      @klass = @records.first.class
    end
    record_ids = @records.map { |record| record.send(primary_key) }
    unless definition = @klass.eager_group_definitions[definition_key]
      next
    end

    reflection = @klass.reflect_on_association(definition.association)
    association_class = reflection.klass
    association_class = association_class.instance_exec(*arguments, &definition.scope) if definition.scope

    foreign_key, aggregate_hash =
      if reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
        ["#{reflection.join_table}.#{reflection.foreign_key}", @klass.joins(reflection.name)]
      elsif reflection.through_reflection
        ["#{reflection.through_reflection.name}.#{reflection.through_reflection.foreign_key}", @klass.joins(reflection.name)]
      else
        [reflection.foreign_key, association_class]
      end
    aggregate_hash = aggregate_hash.where(foreign_key => record_ids)
                                   .where(polymophic_as_condition(reflection))
                                   .group(foreign_key)
                                   .send(definition.aggregation_function, definition.column_name)
    if definition.need_load_object
      aggregate_objects = reflection.klass.find(aggregate_hash.values).each_with_object({}) { |o, h| h[o.id] = o }
      aggregate_hash.keys.each { |key| aggregate_hash[key] = aggregate_objects[aggregate_hash[key]] }
    end
    @records.each do |record|
      id = record.send(primary_key)
      record.send("#{definition_key}=", aggregate_hash[id] || definition.default_value)
    end
  end
end