Class: ForestLiana::LeaderboardStatGetter
- Inherits:
-
StatGetter
- Object
- BaseGetter
- StatGetter
- ForestLiana::LeaderboardStatGetter
- Includes:
- AggregationHelper
- Defined in:
- app/services/forest_liana/leaderboard_stat_getter.rb
Instance Attribute Summary
Attributes inherited from StatGetter
Instance Method Summary collapse
- #get_scoped_model(model, forest_user, timezone) ⇒ Object
-
#initialize(parent_model, params, forest_user) ⇒ LeaderboardStatGetter
constructor
A new instance of LeaderboardStatGetter.
- #perform ⇒ Object
Methods included from AggregationHelper
#aggregation_alias, #aggregation_sql, #resolve_field_path
Methods inherited from StatGetter
#get_resource, #validate_params
Methods inherited from BaseGetter
#get_collection, #get_resource, #includes_for_serialization
Constructor Details
#initialize(parent_model, params, forest_user) ⇒ LeaderboardStatGetter
Returns a new instance of LeaderboardStatGetter.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'app/services/forest_liana/leaderboard_stat_getter.rb', line 5 def initialize(parent_model, params, forest_user) @resource = parent_model @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 |
Instance Method Details
#get_scoped_model(model, forest_user, timezone) ⇒ Object
34 35 36 37 38 39 40 |
# File 'app/services/forest_liana/leaderboard_stat_getter.rb', line 34 def get_scoped_model(model, forest_user, timezone) scope_filters = ForestLiana::ScopeManager.get_scope(model.name, forest_user) return model.unscoped if scope_filters.blank? FiltersParser.new(scope_filters, model, timezone, @params).apply_filters end |
#perform ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/services/forest_liana/leaderboard_stat_getter.rb', line 17 def perform includes = ForestLiana::QueryHelper.get_one_association_names_symbol(@scoped_child_model) alias_name = aggregation_alias(@aggregate, @aggregate_field) result = @scoped_child_model .joins(includes) .where({ @scoped_parent_model.name.downcase.to_sym => @scoped_parent_model }) .group(@group_by) .order(Arel.sql("#{alias_name} DESC")) .limit(@limit) .pluck(@group_by, Arel.sql("#{aggregation_sql(@aggregate, @aggregate_field)} AS #{alias_name}")) .map { |key, value| { key: key, value: value } } @record = Model::Stat.new(value: result) end |