Skip to content

Commit

Permalink
Improve sort_link documentation (#1290)
Browse files Browse the repository at this point in the history
The signature of this method is quite convoluted, I tried to clarify it.
  • Loading branch information
deivid-rodriguez authored Mar 25, 2022
1 parent 7ad9544 commit 3a0e520
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Improve `sort_link` documentation.

* Deprecate passing two trailing hashes to `sort_link`, for example:

```ruby
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ The `search_form_for` answer format can be set like this:
```erb
<%= sort_link(@q, :name) %>
```
Additional options can be passed after the column attribute, like a different
column title or a default sort order:
Additional options can be passed after the column parameter, like a different
column title or a default sort order.

If the first option after the column parameter is a String, it's considered a
custom label for the link:

```erb
<%= sort_link(@q, :name, 'Last Name', default_order: :desc) %>
Expand All @@ -169,7 +172,8 @@ explicitly to avoid an `uninitialized constant Model::Xxxable` error (see issue
<%= sort_link(@q, :xxxable_of_Ymodel_type_some_attribute, 'Attribute Name') %>
```

You can also sort on multiple fields by specifying an ordered array:
If the first option after the column parameter and/or the label parameter is an
Array, it will be used for sorting on multiple fields:

```erb
<%= sort_link(@q, :last_name, [:last_name, 'first_name asc'], 'Last Name') %>
Expand All @@ -179,7 +183,8 @@ In the example above, clicking the link will sort by `last_name` and then
`first_name`. Specifying the sort direction on a field in the array tells
Ransack to _always_ sort that particular field in the specified direction.

Multiple `default_order` fields may also be specified with a hash:
Multiple `default_order` fields may also be specified with a trailing options
Hash:

```erb
<%= sort_link(@q, :last_name, %i(last_name first_name),
Expand Down Expand Up @@ -208,6 +213,9 @@ and you can then sort by this virtual field:
<%= sort_link(@q, :reverse_name) %>
```

The trailing options Hash can also be used for passing additional options to the
generated link, like `class:`.

The sort link order indicator arrows may be globally customized by setting a
`custom_arrows` option in an initializer file like
`config/initializers/ransack.rb`.
Expand Down

0 comments on commit 3a0e520

Please sign in to comment.