Sha256: 675082483ef56e7b1383632b6befd7c4c8560fc2687bebf4e95d45ca32d30c60

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module ForestLiana
  class PieStatGetter
    attr_accessor :record

    def initialize(resource, params)
      @resource = resource
      @params = params
    end

    def perform
      if @params[:group_by_field]
        value = @resource

        @params[:filters].try(:each) do |filter|
          operator, filter_value = OperatorValueParser.parse(filter[:value])
          value = OperatorValueParser.add_where(value, filter[:field], operator,
                                                filter_value, @resource)
        end

        # NOTICE: The generated alias for a count is "count_all", for a sum the
        #         alias looks like "sum_#{aggregate_field}"
        field = 'all'
        if @params[:aggregate].downcase == 'sum'
          field = @params[:aggregate_field].downcase
        end

        value = value
          .unscoped
          .group(@params[:group_by_field])
          .order("#{@params[:aggregate].downcase}_#{field} DESC")
          .send(@params[:aggregate].downcase, @params[:aggregate_field])
          .map do |k, v|
            { key: k, value: v }
          end

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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
forest_liana-1.3.40 app/services/forest_liana/pie_stat_getter.rb
forest_liana-1.3.39 app/services/forest_liana/pie_stat_getter.rb
forest_liana-1.3.38 app/services/forest_liana/pie_stat_getter.rb