From 26835c0a78fbe94287754bb62388d900a12d5510 Mon Sep 17 00:00:00 2001 From: Thiago Youssef <43591948+thiagoyoussef@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:10:53 -0300 Subject: [PATCH 1/2] feature: summarizable associations --- app/controllers/avo/charts_controller.rb | 22 ++++++++++- app/views/avo/partials/_table_header.html.erb | 10 +---- spec/features/avo/field_summarizing_spec.rb | 37 +++++++++++++++++++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/app/controllers/avo/charts_controller.rb b/app/controllers/avo/charts_controller.rb index 5e872c8742..e450af5084 100644 --- a/app/controllers/avo/charts_controller.rb +++ b/app/controllers/avo/charts_controller.rb @@ -3,10 +3,30 @@ module Avo class ChartsController < BaseController def distribution_chart - @values_summary = resource.model_class.group(params[:field_id]).reorder("count_all desc").count + @values_summary = records.group(params[:field_id]).reorder("count_all desc").count + @field_id = params[:field_id] render "avo/partials/distribution_chart", layout: "avo/blank" end + + private + + def records + if is_associated_record? + related_records + else + resource.model_class + end + end + + def related_records + relation_class = BaseResource.get_model_by_name(params[:via_relation_class]) + parent = relation_class.find(params[:via_record_id]) + + association_name = BaseResource.valid_association_name(parent, resource.model_key) + + parent.send(association_name) + end end end diff --git a/app/views/avo/partials/_table_header.html.erb b/app/views/avo/partials/_table_header.html.erb index bafb43bc09..a29151f1bb 100644 --- a/app/views/avo/partials/_table_header.html.erb +++ b/app/views/avo/partials/_table_header.html.erb @@ -1,9 +1,3 @@ -<% - # Currently there isn't a way to summarize records on associations. - # We'd love to support this feature so please send in a PR. - should_summarize = @parent_record.blank? -%> - @@ -83,14 +77,14 @@ <%= field.table_header_label %> <% end %> <% end %> - <% if field.summarizable && should_summarize %> + <% if field.summarizable %>
<%= svg 'heroicons/solid/chart-bar', class: 'h-3 ml-1' %>