Sha256: 032bafa5d4a065953ef033fb1be3a9cfb36e62ff5216845b1d5f205374347494

Contents?: true

Size: 1.75 KB

Versions: 40

Compression:

Stored size: 1.75 KB

Contents

module ForestLiana
  class LeaderboardStatGetter < StatGetter
    def initialize(parent_model, params, forest_user)
      @scoped_parent_model = get_scoped_model(parent_model, forest_user, params[:timezone])
      child_model = @scoped_parent_model.reflect_on_association(params[:relationshipFieldName]).klass
      @scoped_child_model = get_scoped_model(child_model, forest_user, params[:timezone])
      @label_field = params[:labelFieldName]
      @aggregate = params[:aggregator].downcase
      @aggregate_field = params[:aggregateFieldName]
      @limit = params[:limit]
      @group_by = "#{@scoped_parent_model.table_name}.#{@label_field}"
    end

    def perform
      includes = ForestLiana::QueryHelper.get_one_association_names_symbol(@scoped_child_model)

      result = @scoped_child_model
        .joins(includes)
        .where({ @scoped_parent_model.name.downcase.to_sym => @scoped_parent_model })
        .group(@group_by)
        .order(order)
        .limit(@limit)
        .send(@aggregate, @aggregate_field)
        .map { |key, value| { key: key, value: value } }

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

    def get_scoped_model(model, forest_user, timezone)
      scope_filters = ForestLiana::ScopeManager.get_scope_for_user(forest_user, model.name, as_string: true)

      return model.unscoped if scope_filters.blank?

      FiltersParser.new(scope_filters, model, timezone, @params).apply_filters
    end

    def order
      order = 'DESC'

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

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
forest_liana-8.3.3 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-8.3.1 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-8.3.0 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.10 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.9 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.8 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.7 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.6 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.5 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.4 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.3 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.2 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.1 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.1.0 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-8.2.0 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.0.0 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-9.0.0.beta.1 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-8.1.0 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-8.0.17 app/services/forest_liana/leaderboard_stat_getter.rb
forest_liana-8.0.16 app/services/forest_liana/leaderboard_stat_getter.rb