Class: EagerGroup::Preloader::HasManyThroughBelongsTo

Inherits:
AggregationFinder show all
Defined in:
lib/eager_group/preloader/has_many_through_belongs_to.rb

Instance Attribute Summary

Attributes inherited from AggregationFinder

#arguments, #definition, #klass, #record_ids, #reflection

Instance Method Summary collapse

Methods inherited from AggregationFinder

#definition_scope, #initialize

Constructor Details

This class inherits a constructor from EagerGroup::Preloader::AggregationFinder

Instance Method Details

#aggregate_hashObject



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

def aggregate_hash
  scope = reflection.klass.all.tap{|query| query.merge!(definition_scope) if definition_scope }

  scope.where(group_by_foreign_key => record_ids).
    where(polymophic_as_condition).
    group(group_by_foreign_key).
    send(definition.aggregation_function, definition.column_name)
end

#group_by_foreign_keyObject



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

def group_by_foreign_key
  "#{reflection.table_name}.#{reflection.through_reflection.klass.reflect_on_association(reflection.name).foreign_key}"
end

#group_by_keyObject



19
20
21
# File 'lib/eager_group/preloader/has_many_through_belongs_to.rb', line 19

def group_by_key
  reflection.through_reflection.foreign_key
end

#polymophic_as_conditionObject



23
24
25
# File 'lib/eager_group/preloader/has_many_through_belongs_to.rb', line 23

def polymophic_as_condition
  reflection.type ? { reflection.name => { reflection.type => reflection.through_reflection.klass.base_class.name } } : []
end