Sha256: 05dde29d588d5b47d409d3741d7f0e70c1e4f1f4db5be62ad658e6f7bbd3f927

Contents?: true

Size: 1.39 KB

Versions: 22

Compression:

Stored size: 1.39 KB

Contents

module ForestLiana
  class ValueStatGetter < StatGetter
    attr_accessor :record

    def perform
      return if @params[:aggregate].blank?
      resource = get_resource().eager_load(@includes)

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

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

        if raw_previous_interval
          previous_value = filter_parser.apply_filters_on_previous_interval(raw_previous_interval)
        end
      end

      @record = Model::Stat.new(value: {
        countCurrent: count(resource),
        countPrevious: previous_value ? count(previous_value) : nil
      })
    end

    private

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

      if Rails::VERSION::MAJOR >= 4
        if uniq
          # NOTICE: uniq is deprecated since Rails 5.0
          value = Rails::VERSION::MAJOR >= 5 ? value.distinct : value.uniq
        end
        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] || @resource.primary_key
    end

  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
forest_liana-7.6.3 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.6.2 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.6.1 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.6.0 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.5.1 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.5.0 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.4.5 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.4.4 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.4.3 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.4.2 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.4.1 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.4.0 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.3.0 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.2.2 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.2.1 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.2.0 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.1.0 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.0.2 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.0.1 app/services/forest_liana/value_stat_getter.rb
forest_liana-7.0.0 app/services/forest_liana/value_stat_getter.rb