From 21c52889445d56d409fe30d1081b651252390baa Mon Sep 17 00:00:00 2001 From: jubilee2 Date: Wed, 18 Oct 2023 08:40:10 -0500 Subject: [PATCH] hasMany collection columns (#2415) --- docs/customizing_dashboards.md | 3 +++ lib/administrate/field/has_many.rb | 6 +++++- lib/administrate/page/collection.rb | 1 + .../example_app/app/dashboards/order_dashboard.rb | 4 +++- spec/features/show_page_spec.rb | 15 +++++++++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/customizing_dashboards.md b/docs/customizing_dashboards.md index d634444de1..59e3ad1817 100644 --- a/docs/customizing_dashboards.md +++ b/docs/customizing_dashboards.md @@ -113,6 +113,9 @@ association `belongs_to :country`, from your model. **Field::HasMany** +`:collection_attributes` - Set the columns to display in the show view. +Default is COLLECTION_ATTRIBUTES in dashboard. + `:limit` - The number of resources (paginated) to display in the show view. To disable pagination, set this to `0` or `false`. Default is `5`. diff --git a/lib/administrate/field/has_many.rb b/lib/administrate/field/has_many.rb index 13ddebd9c7..a5b7275603 100644 --- a/lib/administrate/field/has_many.rb +++ b/lib/administrate/field/has_many.rb @@ -22,7 +22,11 @@ def self.permitted_attribute(attr, _options = {}) end def associated_collection(order = self.order) - Administrate::Page::Collection.new(associated_dashboard, order: order) + Administrate::Page::Collection.new( + associated_dashboard, + order: order, + collection_attributes: options[:collection_attributes], + ) end def attribute_key diff --git a/lib/administrate/page/collection.rb b/lib/administrate/page/collection.rb index dfefbb8a8f..60ad26ef79 100644 --- a/lib/administrate/page/collection.rb +++ b/lib/administrate/page/collection.rb @@ -4,6 +4,7 @@ module Administrate module Page class Collection < Page::Base def attribute_names + options.fetch(:collection_attributes, nil) || dashboard.collection_attributes end diff --git a/spec/example_app/app/dashboards/order_dashboard.rb b/spec/example_app/app/dashboards/order_dashboard.rb index c63bce7f48..0d85d91c8b 100644 --- a/spec/example_app/app/dashboards/order_dashboard.rb +++ b/spec/example_app/app/dashboards/order_dashboard.rb @@ -11,7 +11,9 @@ class OrderDashboard < Administrate::BaseDashboard address_state: Field::String, address_zip: Field::String, customer: Field::BelongsTo.with_options(order: "name"), - line_items: Field::HasMany, + line_items: Field::HasMany.with_options( + collection_attributes: %i[product quantity unit_price total_price], + ), total_price: Field::Number.with_options(prefix: "$", decimals: 2), shipped_at: Field::DateTime, payments: Field::HasMany, diff --git a/spec/features/show_page_spec.rb b/spec/features/show_page_spec.rb index 1e1f40e2d7..a069028bf6 100644 --- a/spec/features/show_page_spec.rb +++ b/spec/features/show_page_spec.rb @@ -265,6 +265,21 @@ end end + it "displays specified collection_attributes for the has_many association" do + line_item = create(:line_item) + + visit admin_order_path(line_item.order) + + within(table_for_attribute(:line_items)) do + columns = all("tr th").map do |e| + e[:class]&.split&.last&.split("--line_item_")&.last + end + expect(%w[product quantity unit_price total_price]).to( + eq(columns.first(4)), + ) + end + end + def ids_in_table all("tr td:first-child").map(&:text).map(&:to_i) end