diff --git a/app/controllers/avo/charts_controller.rb b/app/controllers/avo/charts_controller.rb index 5e872c874..37bfe0c0a 100644 --- a/app/controllers/avo/charts_controller.rb +++ b/app/controllers/avo/charts_controller.rb @@ -3,10 +3,53 @@ module Avo class ChartsController < BaseController def distribution_chart - @values_summary = resource.model_class.group(params[:field_id]).reorder("count_all desc").count + @values_summary = summary_query.group(params[:field_id].to_sym).reorder("count_all desc").count + @field_id = params[:field_id] render "avo/partials/distribution_chart", layout: "avo/blank" end + + private + + def is_associated_summary + params[:via_record_id].present? && + params[:via_resource_class].present? && + params[:association_name].present? + end + + def summary_query + is_associated_summary ? association_scope : resource.query_scope + end + + def association_scope + parent_resource_class = Avo.resource_manager.get_resource(params[:via_resource_class]) + parent_record = parent_resource_class.find_record(params[:via_record_id], params: params) + + parent_resource = parent_resource_class.new( + record: parent_record, + # Explicitly hardcoding the view to 'show' as association summaries are processed solely within this context + view: Avo::ViewInquirer.new("show") + ) + + parent_resource.detect_fields + + association_name = BaseResource.valid_association_name(parent_record, params[:association_name]) + + parent_field = find_association_field(resource: parent_resource, association: association_name) + + association_query = parent_resource.authorization.apply_policy parent_record.send(association_name) + + if parent_field.scope.present? + association_query = Avo::ExecutionContext.new( + target: parent_field.scope, + query: association_query, + parent: parent_record, + parent_resource: parent_resource + ).handle + end + + association_query + end end end diff --git a/app/views/avo/partials/_table_header.html.erb b/app/views/avo/partials/_table_header.html.erb index bafb43bc0..c02a8e173 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,22 @@ <%= field.table_header_label %> <% end %> <% end %> - <% if field.summarizable && should_summarize %> + <% if field.summarizable %>
<%= svg 'heroicons/solid/chart-bar', class: 'h-3 ml-1' %>