Sha256: ff0f78ba226e0efc719292b33d1075a899ee44d4348ae863c707edda91e6515c

Contents?: true

Size: 1022 Bytes

Versions: 13

Compression:

Stored size: 1022 Bytes

Contents

module ForestLiana
  class ValueStatGetter
    attr_accessor :record

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

    def perform
      return if @params[:aggregate].blank?
      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

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

    private

    def count(value)
      uniq = @params[:aggregate].downcase == 'count'

      if Rails::VERSION::MAJOR == 4
        value = value.uniq if uniq
        value.send(@params[:aggregate].downcase, aggregate_field)
      else
        value.send(@params[:aggregate].downcase, aggregate_field,
                   distinct: uniq)
      end
    end

    def aggregate_field
      @params[:aggregate_field] || :id
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
forest_liana-1.3.10 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.9 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.8 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.7 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.6 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.5 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.4 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.3 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.2 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.1 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.3.0 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.2.6 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.2.5 app/services/forest_liana/value_stat_getter.rb