Administrate Custom Filter
An Administrate plugin to add a custom filter to your index page. The filter will be rendered as an off-canvas component, so it won't take up too much space on your index page. Highly inspired by ActiveAdmin's filter.
Why do you need this?
Let's agree that the Administrate's team has done a great job with the default search or filter functionality. It already supports multiple search fields and relational table searches. It's easy to customize (like enabling/disabling the search, defining custom search fields, etc).
But there are some drawbacks that I found:
- Since it uses a single search box, the search process behind it is quite heavy. It will search all the attributes of the model, and it will be slower if you have a lot of data.
- Again, because it searches all the attributes, it will be hard to search for a specific attribute. For example, if you have
employment_status, and you want to search for "active" registration status only, you will get all the "active" records, including the employment status.
- It's not user-friendly when it comes to a defined search list (e.g. dropdown list). The user will have to type the value manually, and it's not good for the user experience.
Please share your thoughts if you have any other reasons.
- Ruby on Rails version >= 5.0
- Administrate version >= 0.2.2
administrate_filterable to your Gemfile:
And then execute:
$ bundle install
Or, simply just run:
$ bundle add administrate_filterable
Enable the filter
For each resource you want to add a custom filter, add the following line to their Administrate controller, respectively.
class UsersController < Administrate::ApplicationController include AdministrateFilterable::Filterer # ... end
Customizing the filter fields
By default, all the attributes from
COLLECTION_ATTRIBUTES will be rendered as the filter fields. You can override this by adding
FILTER_ATTRIBUTES to your Administrate's dashboard file.
class UserDashboard < Administrate::BaseDashboard # .. FILTER_ATTRIBUTES = [ :first_name, :last_name, :email, :created_at ].freeze # .. end
Customizing the filter template
It is possible to customize the filter template (e.g. changing the filter button icon, etc). You can do this by overriding the default template in your application, just create a new file called
_index_filter.html.erb in your desired resource folder.
For example, if you want to override the filter template for the
users resource, you need to create the file in
app/views/admin/users/_index_filter.html.erb. Then just copy and paste the content from the default template here and modify it to suit your needs.
If you use an assets pipeline, you need to include this gem's assets in your
// ... other code here ... //= link administrate_filterable/application.css //= link administrate_filterable/application.js
rails assets:precompile if the assets are not loaded.
Overridden default search template
By default, this gem will add a filter button to
views/admin/application/_search.html.erb partial. But if you have overridden that partial in your application you can add the button manually by adding the following line:
<%= render 'index_filter' %>
<form class="search" role="search"> ... other code here ... </form> <%= render 'index_filter' %>
This gem utilizes the
scoped_resource method to filter the resources by overriding its default behavior. If you by any chance also override this method in your application controller, you will also override the filter functionality and it won't work.
To fix this, you need to add the following code in the last line of your overridden
def scoped_resource # Your custom filter logic here resources = super.where(status: 'active') # Add this line to make the filter work filtered_resources(resources) end
Filter button not showing
Since I use the
_search.html.erb partial to add the filter button, it may not be showing if you turn all the searchable attributes to false in the model dashboard. So make sure you have at least one searchable attribute to make the filter button show up.
The reason why I use the partial is because:
- It is rarely overridden by the user, so you won't have to add the button manually in most cases.
- Previously, I used the
_index_header.html.erbpartial, but it conflicts with the export button from administrate_exportable. Using both gems results in one button missing, due to the partial override.
There are still a lot of things to do to make this gem better. Here are some of them (sorted by highest priority first):
- Add support for the relational filter (e.g. filter by
- Add support to customize the dropdown list (e.g. add
- Exclude checkbox, radio, or select value from the filter params if no action is performed on them
- Figure out a better way to implement the filter functionality (currently I override the
- Figure out a better way to pass the filter attributes to the form (currently I use an instance variable in the overridden
- Add capability to customize the filter behavior (e.g. search by exact match, search by partial match, etc just like in the ActiveAdmin filter)
- Improve the toggle button user experience (e.g. add open/close animation, add dynamic open/close title, etc)
If you have any ideas or suggestions, please let me know by creating an issue or pull request.
You can help me to improve this gem by contributing to this project. Any help is highly appreciated.
- Fork this repo and create a pull request
- Please test your code:
bundle exec rspec
- Please document your code if needed
Huge thanks for the following resources that helped me a lot in creating this gem:
- administrate_exportable: I just copy and paste the code from this gem and modify it to suit my needs, highly recommended if you need to export your data from Administrate.
- Off-Canvas Menu: I found this code from Google Images when I was looking for a way to create an off-canvas filter component. I modified it a little bit to suit my needs. But it seems the CodePen is no longer available, so I put the archived version here. If any of you know the original author, please let me know so I can give the proper credit.