Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document configuration for default ordering #1635

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,43 @@ COLLECTION_FILTERS = {
inactive: ->(resources) { resources.inactive }
}
```

## Default Ordering

A default sorting order and direction can be defined for all index listings, which can then be overridden per controller as needed:

```ruby
# app/controllers/admin/application_controller.rb
module Admin
class ApplicationController < Administrate::ApplicationController
....

def order
@order ||= Administrate::Order.new(
params.fetch(resource_name, {}).fetch(:order, default_sort[:order]),
params.fetch(resource_name, {}).fetch(:direction, default_sort[:direction]),
)
end

# override this in specific controllers as needed
def default_sort
{ order: :updated_at, direction: :desc }
end

...
end
end

# app/controllers/admin/foo_controller.rb
module Admin
class FooController < ApplicationController
....

def default_sort
{ order: :created_at, direction: :asc }
end

...
end
end
```