Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: summarizable associations #3428

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion app/controllers/avo/charts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure

Code scanning / CodeQL

SQL query built from user-controlled sources High

This SQL query depends on a
user-provided value
.

@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
10 changes: 2 additions & 8 deletions app/views/avo/partials/_table_header.html.erb
Original file line number Diff line number Diff line change
@@ -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?
%>

<thead
data-component-name="avo/partials/table_header"
class="bg-white border-b border-gray-200 pb-1">
Expand Down Expand Up @@ -83,14 +77,14 @@
<%= field.table_header_label %>
<% end %>
<% end %>
<% if field.summarizable && should_summarize %>
<% if field.summarizable %>
<div id="summary-header-<%= field.id %>" class="<%= text_classes %> cursor-pointer" data-action="click->toggle#togglePanel">
<%= svg 'heroicons/solid/chart-bar', class: 'h-3 ml-1' %>
</div>
<turbo-frame
id="summary-frame-<%= field.id %>"
loading="lazy"
src="<%= distribution_chart_path(resource_name: field.resource.model_class, field_id: field.id) %>"
src="<%= distribution_chart_path(resource_name: field.resource.model_class, field_id: field.id, via_relation_class: @parent_record.class, via_record_id: @parent_record&.id) %>"
class="absolute inset-auto bg-white w-full sm:w-[400px] top-0 mt-6 p-4 z-40 shadow-modal rounded overflow-hidden hidden border border-gray-50"
data-toggle-target="panel"
>
Expand Down
37 changes: 37 additions & 0 deletions spec/features/avo/field_summarizing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,42 @@
expect(page).not_to have_css 'th[data-table-header-field-id="progress"] div svg'
expect(page).not_to have_css 'th[data-table-header-field-id="description"] div svg'
end

context "when summarizing on association pages" do
let(:user) { create :user }
let!(:project) { create :project, status: :closed, users: [user] }

it "only shows values from associated record" do
visit avo.resources_user_path(user)

scroll_to first_tab_group
click_tab "Projects", within_target: first_tab_group

wait_for_turbo_frame_id("has_and_belongs_to_many_field_show_projects")

expect(page).to have_css "turbo-frame[id='summary-frame-status']", visible: false

find("#summary-header-status").click

expect(page).to have_css "turbo-frame[id='summary-frame-status']", visible: true

# I can't make the lazy loading work, looks like it's not triggered at all
wait_for_turbo_frame_id("summary-frame-status")

expect(page).to have_css "#status-summary", visible: true
expect(page).to have_css "#chart-status", visible: true

within "#status-summary" do
expect(page).to_not have_content "REJECTED\n3"
expect(page).to have_content "CLOSED\n1"
expect(page).to_not have_content "LOADING\n4"
end

find('th[data-table-header-field-id="status"] div svg').click

expect(page).not_to have_css "#status-summary"
expect(page).not_to have_css "#chart-status"
end
end
end
end
Loading