Sortofil
Sortofil - sort and filter helper for Rails app
Features:
- sortable table headers
- restore url query params during session
Usage
Sort/filter
Add this to controller:
include Sortofilterable
before_action :permit_params
before_action :set_default_sort_opts
Customize options (override methods inside controller):
- Simple case:
def set_default_sort_field
@default_sort_field = { nil => :created_at }
end
def set_default_sort_direction
@default_sort_direction = { nil => :desc }
end
def permit_params
@permitted_params = params.permit(sort_params, filter_params,
pagination_params, :your_additional_param)
end
- Multiple models per page case:
def set_default_sort_field
@default_sort_field = { artists: :first_name, authors: :last_name }
end
def set_default_sort_direction
@default_sort_direction = { artists: :asc, authors: :desc }
end
def permit_params
@permitted_params = params.permit({ artists: sort_params + filter_params,
authors: sort_params + filter_params },
pagination_params)
end
View
- Simple case:
<table>
<thead>
<th>
<%= render 'layouts/sortable_theader', model: 'artists' %>
</th>
</thead>
<tbody>
...
</tbody>
</table>
- Multiple models per page case:
<table>
<thead>
<th>
<%= render 'layouts/sortable_theader', model: 'artists', scoped: true,
url: 'root' %>
</th>
</thead>
<tbody>
...
</tbody>
</table>
<table>
<thead>
<th>
<%= render 'layouts/sortable_theader', model: 'authors', scoped: true,
url: 'root' %>
</th>
</thead>
<tbody>
...
</tbody>
</table>
Url restore
Add this to controller:
include UrlRestorable
before_action do
restore_url_params(:your_key)
end
View
<%= render 'layouts/restorable_url_params', key: :your_key %>
Installation
Add this line to your application's Gemfile:
gem 'sortofil'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sortofil
License
The gem is available as open source under the terms of the MIT License.