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
  • !1805

Order by belongs_to field

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/sejinkim1904/order_by_association_attribute into main Oct 21, 2020
  • Overview 12
  • Commits 8
  • Pipelines 0
  • Changes 6

Created by: sejinkim1904

What does this PR do?

  • Updates Order to be able to sort by belongs_to field
    • Updates initialize to take in association_attribute
    • Adds order_by_association_attribute
    • Adds order_by_attribute
    • Updates order_by_association to use order_by_attribute for belongs_to associations
    • Adds association_has_attribute?
  • Adds belongs_to_sorting_field to ApplicationController to be overridden by users to select which field to sort by
  • Adds belongs_to_sorting_attribute to OrdersController for testing
  • Updates/adds tests to specs
  • Adds Customizing belongs_to Sorting Field section in /docs/customizing_controller_actions.md

How should this be manually tested?

  • Start local server
  • Visit /admin/orders
  • Click on 'Customer'

Any background context you want to provide?

My team currently uses administrate in our application and we noticed when trying to sort by a belongs_to association, by default it sorts by the association's id. We found a workaround by sub-classing Administrate::Order in our controller to be able to sort by an association's field. Our implementation was very specific to our data model but I thought this feature could be useful for future or current devs so I decided to create a more dynamic version.

Screenshots (if appropriate)

Before:

class OrdersController < Admin::ApplicationController
end

before-admin-sorting

After:

class OrdersController < Admin::ApplicationController
  def belongs_to_sorting_attribute
    :name
  end
end

after-admin-sorting

Questions:

  • Do Migrations Need to be ran? NO
  • Do Environment Variables need to be set? NO
  • Any other deploy steps? NO
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/sejinkim1904/order_by_association_attribute