bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 116 forks source link

Order from calculated value for mongoid #216

Closed lcastrooliveira closed 7 years ago

lcastrooliveira commented 7 years ago

Hello, your gem is really great. However I'm stuck in a feature to order columns that are made uppon calculations. I've read these instructions here:

# Order by a calculated value
column(
  :duration_request, 
  order: "(requests.finished_at - requests.accepted_at)"
) do |model|
  Time.at(model.finished_at - model.accepted_at).strftime("%H:%M:%S")
end

But I believe that it only works with active record. Do you have a suggestion on how to do this with mongoid ?

Thanks.

bogdan commented 7 years ago

Datagrid can do whatever mongoid can do. I am not a mongoid expert. If you can show me the code that orders by calculated value in mongoid, I can help you convert it into Datagrid code.

lcastrooliveira commented 7 years ago

Hello Bogdan. Sorry for late reply. The thing is the following: I have a model called service that has a one-to-many link to another model called professional, one service has_many professionals. In order to show in the datagrid how many professionals are linked to that service, i do the following:

column(:professionals, header: 'Profissionais') do |model|
    model.professionals.count
 end

The results appear in the datagrid correctly. However I'm unable to create an order function, thus those little arrows for ordering don't appear in the column. If I specify an order parameter such as:

column(:professionals, header: 'Profissionais', order: 'professionals.count') do |model|
    model.professionals.count
  end

The clickable arrows will appear, however it causes no effect. How can I address this problem ?

Thank you.

bogdan commented 7 years ago

Datagrid can only do things that mongoid can do. Show me how you would do that in plain mongoid..... like:

Service.where(....).order(MAGIC)

If you would be able to figure that out, I will show you how to convert it into datagrid order.