Class: ForestLiana::PieStatGetter
- Inherits:
-
StatGetter
- Object
- BaseGetter
- StatGetter
- ForestLiana::PieStatGetter
- Defined in:
- app/services/forest_liana/pie_stat_getter.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
Methods inherited from StatGetter
Methods inherited from BaseGetter
#get_collection, #get_resource, #includes_for_serialization
Constructor Details
This class inherits a constructor from ForestLiana::StatGetter
Instance Attribute Details
#record ⇒ Object
Returns the value of attribute record.
3 4 5 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 3 def record @record end |
Instance Method Details
#group_by_field ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 38 def group_by_field if @params[:group_by_field].include? ':' association, field = @params[:group_by_field].split ':' resource = @resource.reflect_on_association(association.to_sym) "#{resource.table_name}.#{field}" else "#{@resource.table_name}.#{@params[:group_by_field]}" end end |
#order ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 48 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 @params[:aggregate].downcase == 'sum' field = @params[:aggregate_field].downcase else field = Rails::VERSION::MAJOR >= 5 || @includes.size > 0 ? 'id' : 'all' end "#{@params[:aggregate].downcase}_#{field} #{order}" end |
#perform ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/services/forest_liana/pie_stat_getter.rb', line 5 def perform if @params[:group_by_field] timezone_offset = @params[:timezone].to_i resource = get_resource().eager_load(@includes) unless @params[:filters].blank? resource = FiltersParser.new(@params[:filters], resource, @params[:timezone]).apply_filters end result = resource .group(group_by_field) .order(order) .send(@params[:aggregate].downcase, @params[:aggregate_field]) .map do |key, value| # NOTICE: Display the enum name instead of an integer if it is an # "Enum" field type on old Rails version (before Rails # 5.1.3). if @resource.respond_to?(:defined_enums) && @resource.defined_enums.has_key?(@params[:group_by_field]) && key.is_a?(Integer) key = @resource.defined_enums[@params[:group_by_field]].invert[key] elsif @resource.columns_hash[@params[:group_by_field]] && @resource.columns_hash[@params[:group_by_field]].type == :datetime key = (key + timezone_offset.hours).strftime('%d/%m/%Y %T') end { key: key, value: value } end @record = Model::Stat.new(value: result) end end |