Module: ForestLiana::AggregationHelper

Included in:
LeaderboardStatGetter, PieStatGetter
Defined in:
app/services/forest_liana/aggregation_helper.rb

Instance Method Summary collapse

Instance Method Details

#aggregation_alias(type, field) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'app/services/forest_liana/aggregation_helper.rb', line 31

def aggregation_alias(type, field)
  case type
  when 'sum'
    "sum_#{field.downcase}"
  when 'count'
    'count_id'
  else
    raise "Unsupported aggregator : #{type}"
  end
end

#aggregation_sql(type, field) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/forest_liana/aggregation_helper.rb', line 18

def aggregation_sql(type, field)
  field_path = resolve_field_path(field)

  case type
  when 'sum'
    "SUM(#{field_path})"
  when 'count'
    "COUNT(DISTINCT #{field_path})"
  else
    raise "Unsupported aggregator : #{type}"
  end
end

#resolve_field_path(field_param, default_field = 'id') ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/services/forest_liana/aggregation_helper.rb', line 3

def resolve_field_path(field_param, default_field = 'id')
  if field_param.blank?
    default_field ||= @resource.primary_key || 'id'
    return "#{@resource.table_name}.#{default_field}"
  end

  if field_param.include?(':')
    association, field = field_param.split ':'
    associated_resource = @resource.reflect_on_association(association.to_sym)
    "#{associated_resource.table_name}.#{field}"
  else
    "#{@resource.table_name}.#{field_param}"
  end
end