administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Polymorphic field seems to only support one rule for ordering

Open DavidGeismarLtd opened this issue 4 years ago • 1 comments

Accorging to the doc. Field::Polymorphic seems to support an order parameter, which is useful to display the associated models in a specific order. However, if you have a polymorphic field it means you have different associated models, that can have different schema and different ordering logic. For each one of those associated models, you should have the flexibility to define a different ordering rule. here's an example :

    item: Field::Polymorphic.with_options(
      classes: [Program, Session],
      order: 'startdate'
    ),

this works at the moment if both Program and Session have a startdate, but what if program doesn't have one ? Or you want to order programs by endDate instead ?

DavidGeismarLtd avatar May 20 '21 12:05 DavidGeismarLtd

Good point. I had a look at the code and I don't see a reason why we couldn't add this feature. I think it would involve changes to the following section of Administrate's code:

https://github.com/thoughtbot/administrate/blob/b3bb0a3b7833deceff71cfe1687ac36cec12a6e8/lib/administrate/field/polymorphic.rb#L38-L44

There's the question of what the shape of the order option should be in order to express this preference. Perhaps a hash?

  ATTRIBUTE_TYPES = {
    id: Field::Number,
    action: Field::String,
    logeable: Field::Polymorphic.with_options(
      classes: [Customer, ::Order],
      order: {
        "Customer" => "updated_at",
        "Order" => ["shipped_at", "updated_at"],
      }
    ),
  }.freeze

For the example above I have used the LogEntryDashboard in the example app. This would be a good place to base some specs on.

Finally, the docs would need to be updated.

Would you be able to volunteer a PR?

pablobm avatar Jun 10 '21 14:06 pablobm

I am less sure about this one now, and I'm thinking it's out of scope. Closing, but happy to discuss further.

pablobm avatar Apr 06 '23 21:04 pablobm