diff --git a/app/controllers/avo/charts_controller.rb b/app/controllers/avo/charts_controller.rb index 5e872c874..09f96811f 100644 --- a/app/controllers/avo/charts_controller.rb +++ b/app/controllers/avo/charts_controller.rb @@ -3,10 +3,32 @@ module Avo class ChartsController < BaseController def distribution_chart - @values_summary = resource.model_class.group(params[:field_id]).reorder("count_all desc").count + @values_summary = scoped_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 scoped_query + if is_associated_record? + query = resource.authorization&.apply_policy related_records + + Avo::ExecutionContext.new(target: resource.search_query, query: query).handle + else + resource.query_scope + 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 bafb43bc0..a29151f1b 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' %>