Suggestion: Allowing ordering to accept more options
Created by: migu0
I have to order the index page by default by an attribute quality
, which can be NULL
or any value from 0 to 10.
module Admin
class OrganizationsController < Admin::ApplicationController
private
def order
@_order ||= if params[:order]
Administrate::Order.new(params[:order], params[:direction])
else
Administrate::Order.new('quality', 'desc NULLS last')
end
end
end
end
If I order with desc
(highest quality first), NULL
's will come first. Postgres has a nice syntax to control whether NULL
's should come first or not: Organization.all.order('quality DESC NULLS last')
.
However, I can't use the code posted above because AR is not aware of this Postgres syntax when attribute and order is passed in as a key / value like so: https://github.com/thoughtbot/administrate/blob/master/lib/administrate/order.rb#L10. I get the following error: Direction "desc NULLS last" is invalid. Valid directions are: [:asc, :desc, :ASC, :DESC, "asc", "desc", "ASC", "DESC"]
However, if we changed this line to accept any SQL ordering (without AR validating it), it would work: relation.order("#{attribute} #{direction}")