Sha256: 706467da13fd17e30d3d143249472a9b62bde11a7ec006bb7d6db86c27567d20

Contents?: true

Size: 1.75 KB

Versions: 29

Compression:

Stored size: 1.75 KB

Contents

module ForestLiana
  class LineStatGetter
    attr_accessor :record

    def initialize(resource, params)
      @resource = resource
      @params = params
      @populates = {}
    end

    def perform
      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)
      end

      value = value.send(time_range, @params[:group_by_date_field])
      value = value.group(group_by_field || :id) if group_by_field

      value = value.send(@params[:aggregate].downcase, @params[:aggregate_field])
        .map do |k, v|
          if k.kind_of?(Array)
            {
              label: k[0],
              values: {
                key: populate(k[1]),
                value: v
              }
            }
          else
            {
              label: k,
              values: {
                value: v
              }
            }
          end
        end

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

    private

    def group_by_field
      field_name = @params[:group_by_field]
      association = @resource.reflect_on_association(field_name) if field_name

      if association
        association.foreign_key
      else
        field_name
      end
    end

    def populate(id)
      @populates[id] ||= begin
        field_name = @params[:group_by_field]
        association = @resource.reflect_on_association(field_name)

        if association
          association.klass.find(id)
        else
          id
        end
      end
    end

    def time_range
      "group_by_#{@params[:time_range].try(:downcase) || 'month'}"
    end

  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
forest_liana-1.3.10 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.9 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.8 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.7 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.6 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.4 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.3.0 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.2.6 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.2.5 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.2.3 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.2.2 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.2.1 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.1.35 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.1.34 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.1.33 app/services/forest_liana/line_stat_getter.rb
forest_liana-1.1.32 app/services/forest_liana/line_stat_getter.rb