Skip to content

Commit 21c5288

Browse files
authored
hasMany collection columns (#2415)
1 parent 84badf2 commit 21c5288

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

docs/customizing_dashboards.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ association `belongs_to :country`, from your model.
113113

114114
**Field::HasMany**
115115

116+
`:collection_attributes` - Set the columns to display in the show view.
117+
Default is COLLECTION_ATTRIBUTES in dashboard.
118+
116119
`:limit` - The number of resources (paginated) to display in the show view. To disable pagination,
117120
set this to `0` or `false`. Default is `5`.
118121

lib/administrate/field/has_many.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ def self.permitted_attribute(attr, _options = {})
2222
end
2323

2424
def associated_collection(order = self.order)
25-
Administrate::Page::Collection.new(associated_dashboard, order: order)
25+
Administrate::Page::Collection.new(
26+
associated_dashboard,
27+
order: order,
28+
collection_attributes: options[:collection_attributes],
29+
)
2630
end
2731

2832
def attribute_key

lib/administrate/page/collection.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Administrate
44
module Page
55
class Collection < Page::Base
66
def attribute_names
7+
options.fetch(:collection_attributes, nil) ||
78
dashboard.collection_attributes
89
end
910

spec/example_app/app/dashboards/order_dashboard.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class OrderDashboard < Administrate::BaseDashboard
1111
address_state: Field::String,
1212
address_zip: Field::String,
1313
customer: Field::BelongsTo.with_options(order: "name"),
14-
line_items: Field::HasMany,
14+
line_items: Field::HasMany.with_options(
15+
collection_attributes: %i[product quantity unit_price total_price],
16+
),
1517
total_price: Field::Number.with_options(prefix: "$", decimals: 2),
1618
shipped_at: Field::DateTime,
1719
payments: Field::HasMany,

spec/features/show_page_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,21 @@
265265
end
266266
end
267267

268+
it "displays specified collection_attributes for the has_many association" do
269+
line_item = create(:line_item)
270+
271+
visit admin_order_path(line_item.order)
272+
273+
within(table_for_attribute(:line_items)) do
274+
columns = all("tr th").map do |e|
275+
e[:class]&.split&.last&.split("--line_item_")&.last
276+
end
277+
expect(%w[product quantity unit_price total_price]).to(
278+
eq(columns.first(4)),
279+
)
280+
end
281+
end
282+
268283
def ids_in_table
269284
all("tr td:first-child").map(&:text).map(&:to_i)
270285
end

0 commit comments

Comments
 (0)