Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A administrate
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 96
    • Issues 96
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 32
    • Merge requests 32
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • thoughtbot, inc.
  • administrate
  • Issues
  • #341
Closed
Open
Issue created Dec 15, 2015 by Administrator@rootContributor

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}")

Assignee
Assign to
Time tracking