Sha256: d0b499ffe4d8f2c3fffb4ace3054a9aa1ed51752dc3c74f6791a7a3d6d603074

Contents?: true

Size: 927 Bytes

Versions: 13

Compression:

Stored size: 927 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 = value.where("#{filter[:field]} #{operator} '#{filter_value}'")
      end

      @record = 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], 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.1.18 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.17 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.16 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.15 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.14 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.13 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.12 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.11 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.10 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.8 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.7 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.6 app/services/forest_liana/value_stat_getter.rb
forest_liana-1.1.5 app/services/forest_liana/value_stat_getter.rb