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
  • Merge requests
  • !922

Feature/custom sorting via options

  • Review changes

  • Download
  • Email patches
  • Plain diff
Open Jon Kinney requested to merge github/fork/headwayio/feature/custom_sorting_via_options into main Jul 04, 2017
  • Overview 3
  • Commits 4
  • Pipelines 1
  • Changes 2

I'm sure this code could be cleaned up, and there are likely edge cases, but I wanted to get this out here to start a discussion as to whether or not people would find this useful? Here are my two use cases:

  1. I want to present one of the attributes differently so I create a new method on the model called, for example, display_phone, where I do some formatting of the sanitized stored phone number data. The problem is that once I do that, I can't sort by that field because the db only knows about phone not display_phone. With this change I can specify an order: 'phone' option to the display_phone field on the dashboard and allow that pretty displayed data to be sorted by its underlying db column.

     phone: Field::String,
     display_phone: Field::String.with_options(
       searchable: false,
       order: 'phone',
     ),
  2. I am displaying a list of Clinics that belong to an Organization and I would like to be able to sort the clinic list by this associated model's name field. By allowing order: 'organization.name' to be specified, we can detect that we're sorting across a join table with the presence of a . and join to that table and order by the specified attribute. One trick/bummer here is that with this implementation, we need to leave the plurality of the join table name to be specified by the user. So in this case with a Clinic#index view where a Clinic belongs to an Organization it'd be a singular organization.name specified.

     organization: Field::BelongsTo.with_options(
       order: 'organization.name',
     ),
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/headwayio/feature/custom_sorting_via_options