administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Sorting on "virtual" column

Open wengzilla opened this issue 2 years ago • 0 comments

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

I use the money-rails gem which basically has me store values in terms of cents. For instance, if I wanted to represent a price of $10.00, I could create a column called price_cents and write 1000 to it. Monetize is great because when you call .price, you get back the price in dollars (in this case, 10).

When I'm using a monetize column in Administrate, I add in the virtual property called price. Everything works great, but unfortunately, the order mechanic doesn't work. Based on a quick read of the code, it seems like it expects the column to actually exist.

ATTRIBUTE_TYPES = {
  price: Field::Number
}

I could switch to just displaying price_cents in the Administrate dashboard, but then I have to manually format everything into dollars and add translations to change the table header names. Seems unideal.

ATTRIBUTE_TYPES = {
  price_cents: Field::Number.with_options(prefix: "$", decimals: 2, multiplier: 0.01)
}

Does anyone else have this problem? Any workarounds?

  • How could we go about implementing that?

One solution I thought of would be to introduce an :order_by property where I could pass it a different column to sort by. Maybe something like this:

ATTRIBUTE_TYPES = {
  price: Field::Number(order_by: "price_cents")
}

Is there an easier way to achieve what I want?

wengzilla avatar Dec 08 '23 00:12 wengzilla