Gem generates :filtered and :sorted scopes, can be easily used for building admin namespace or other filterable sortable architecture.
Add this line to your application's Gemfile:
gem 'filterable_sortable'
And then execute:
bundle
Or install it yourself as:
gem install filterable_sortable
First of all include module into ActiveRecord::Base inherited class
include FilterableSortable
In controllers add scopes to index action. I use 'will_paginate' gem, so here is method *.paginate(...)
@posts = Post.filtered(params[:filter]).ordered(params[:order]).paginate(page: params[:page], per_page: params[:per_page])
In views you can use links with params for order and filter. Example in Haml.
= link_to 'Name', posts_path(filter: {custom: :name}, order: {field: :name, direction: :ASC})
Simple search is also acts as filter, works as scope, already defined in gem, so enjoy it.
= form_tag posts_path do
= text_field_tag :'filter[search]'
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request