Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting on "virtual" column #2468

Open
wengzilla opened this issue Dec 8, 2023 · 0 comments
Open

Sorting on "virtual" column #2468

wengzilla opened this issue Dec 8, 2023 · 0 comments

Comments

@wengzilla
Copy link

wengzilla commented Dec 8, 2023

  • 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant