Class: EagerGroup::Preloader

Inherits:
Object
  • Object
show all
Defined in:
lib/eager_group/preloader.rb,
lib/eager_group/preloader/has_many.rb,
lib/eager_group/preloader/many_to_many.rb,
lib/eager_group/preloader/aggregation_finder.rb,
lib/eager_group/preloader/has_many_through_many.rb,
lib/eager_group/preloader/has_many_through_belongs_to.rb

Defined Under Namespace

Classes: AggregationFinder, HasMany, HasManyThroughBelongsTo, HasManyThroughMany, ManyToMany

Instance Method Summary collapse

Constructor Details

#initialize(klass, records, eager_group_values) ⇒ Preloader

Returns a new instance of Preloader.



11
12
13
14
15
# File 'lib/eager_group/preloader.rb', line 11

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

#find_aggregate_values_per_definition!(definition_key, arguments) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/eager_group/preloader.rb', line 39

def find_aggregate_values_per_definition!(definition_key, arguments)
  unless definition = @klass.eager_group_definitions[definition_key]
    return
  end

  reflection = @klass.reflect_on_association(definition.association)
  return if reflection.blank?

  aggregation_finder_class = if reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
    ManyToMany
  elsif reflection.through_reflection
    if reflection.through_reflection.is_a?(ActiveRecord::Reflection::BelongsToReflection)
      HasManyThroughBelongsTo
    else
      HasManyThroughMany
    end
  else
    HasMany
  end

  aggregation_finder = aggregation_finder_class.new(@klass, definition, arguments, @records)
  aggregate_hash = aggregation_finder.aggregate_hash

  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(aggregation_finder.group_by_key)
    record.send("#{definition_key}=", aggregate_hash[id] || definition.default_value)
  end
end

#runObject

Preload aggregate functions



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eager_group/preloader.rb', line 18

def run
  @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
      next if @records.empty?
      @klass = @records.first.class.reflect_on_association(association_name).klass

      @records = @records.flat_map { |record| record.send(association_name) }
      next if @records.empty?


      self.class.new(@klass, @records, Array.wrap(definition_key)).run
    end

    find_aggregate_values_per_definition!(definition_key, arguments)
  end
end