Class: ForestLiana::PieStatGetter

Inherits:
StatGetter show all
Defined in:
app/services/forest_liana/pie_stat_getter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from StatGetter

#initialize

Methods inherited from BaseGetter

#get_collection, #get_resource, #includes_for_serialization

Constructor Details

This class inherits a constructor from ForestLiana::StatGetter

Instance Attribute Details

#recordObject

Returns the value of attribute record.


3
4
5
# File 'app/services/forest_liana/pie_stat_getter.rb', line 3

def record
  @record
end

Instance Method Details

#group_by_fieldObject


40
41
42
43
44
45
46
47
48
# File 'app/services/forest_liana/pie_stat_getter.rb', line 40

def group_by_field
  if @params[:group_by_field].include? ':'
    association, field = @params[:group_by_field].split ':'
    resource = @resource.reflect_on_association(association.to_sym)
    "#{resource.table_name}.#{field}"
  else
    "#{@resource.table_name}.#{@params[:group_by_field]}"
  end
end

#orderObject


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/forest_liana/pie_stat_getter.rb', line 50

def order
  order = 'DESC'

  # NOTICE: The generated alias for a count is "count_all", for a sum the
  #         alias looks like "sum_#{aggregate_field}"
  if @params[:aggregate].downcase == 'sum'
    field = @params[:aggregate_field].downcase
  else
    # `count_id` is required only for rails v5
    field = Rails::VERSION::MAJOR == 5 || @includes.size > 0 ? 'id' : 'all'
  end
  "#{@params[:aggregate].downcase}_#{field} #{order}"
end

#performObject


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
# File 'app/services/forest_liana/pie_stat_getter.rb', line 5

def perform
  if @params[:group_by_field]
    timezone_offset = @params[:timezone].to_i
    resource = get_resource().eager_load(@includes)

    filters = ForestLiana::ScopeManager.append_scope_for_user(@params[:filters], @user, @resource.name)

    unless filters.blank?
      resource = FiltersParser.new(filters, resource, @params[:timezone]).apply_filters
    end

    result = resource
      .group(group_by_field)
      .order(order)
      .send(@params[:aggregate].downcase, @params[:aggregate_field])
      .map do |key, value|
        # NOTICE: Display the enum name instead of an integer if it is an
        #         "Enum" field type on old Rails version (before Rails
        #         5.1.3).
        if @resource.respond_to?(:defined_enums) &&
          @resource.defined_enums.has_key?(@params[:group_by_field]) &&
          key.is_a?(Integer)
          key = @resource.defined_enums[@params[:group_by_field]].invert[key]
        elsif @resource.columns_hash[@params[:group_by_field]] &&
          @resource.columns_hash[@params[:group_by_field]].type == :datetime
          key = (key + timezone_offset.hours).strftime('%d/%m/%Y %T')
        end

        { key: key, value: value }
      end

    @record = Model::Stat.new(value: result)
  end
end