administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Pass option to Field::HasOne to only display specific columns

Open DavidGeismarLtd opened this issue 4 years ago • 3 comments

  • What would you like to be able to do? Can you provide some examples?

Let's say I have a Program has_one Product as: :item where Product handles polymorphic association with item_type and item_id column.

In my Program dashboard, I want to make product editable :

 ATTRIBUTE_TYPES = {
  product: Field::HasOne,
  }
SHOW_PAGE_ATTRIBUTES = %i[
  product
]

FORM_ATTRIBUTES = %i[
 product
]

In the administrate UI you end up with on program#show :

Screenshot 2021-01-29 at 08 33 04

and on program#edit :

Screenshot 2021-01-29 at 08 34 23

On both page the item field from Product is visible or editable, but in that case it doesn't make sense to show or edit the item column because I am already on the page of the resource I want to edit.

  • How could we go about implementing that?

Is there an option I can pass to Field::HasOne to tell it to display only certain fields ? Something like : product: Field::HasOne.with_options(except: %i[ item_type item_id])

  • Can you think of other approaches to the problem?

DavidGeismarLtd avatar Jan 29 '21 07:01 DavidGeismarLtd

Unfortunately, this is not possible at present.

For this to work, the HasMany field would have to link to a different version of the Product dashboard. One under /admin/programs/:id/products, as opposed /admin/products. This way, it would be able to use the URL to identify that a different dashboard (or dashboard configuration) should be used.

This falls under the category of changes to namespacing and dashboard definition that should be handled at some point. For now, I can only thank all of you coming to us with use cases, as these will help us create a better solution in the future.

pablobm avatar Feb 04 '21 15:02 pablobm

I'd like to be able to include a HasOne field on on a Show page but not have the entire object (and all of its own nested HasOne entire objects) displayed.

In some cases I'd rather just have a simple link to the referenced object. Is there anyway to do that right now? I was picturing something like Field::HasOne.with_options(expand: false)

I suppose this is also related to #1559.

richardonrails avatar May 13 '22 20:05 richardonrails

@richardonrails - I recommend creating a new field type; say Field::HasOneLink that behaves as you expect. You can copy (or perhaps inherit) the existing Field::HasOne and build on top of it.

pablobm avatar Jun 23 '22 10:06 pablobm