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.
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:
registration_status
and employment_status
, and you want to search for "active" registration status only, you will get all the "active" records, including the employment status.Please share your thoughts if you have any other reasons.
Add administrate_filterable
to your Gemfile:
gem 'administrate_filterable'
And then execute:
$ bundle install
Or, simply just run:
$ bundle add administrate_filterable
For each resource you want to add a custom filter, add the following line to their Administrate controller, respectively.
include AdministrateFilterable::Filterer
Example:
class UsersController < Administrate::ApplicationController
include AdministrateFilterable::Filterer
# ...
end
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.
Example (app/dashboards/user_dashboard.rb
):
class UserDashboard < Administrate::BaseDashboard
# ..
FILTER_ATTRIBUTES = [
:first_name,
:last_name,
:email,
:created_at
].freeze
# ..
end
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 app/assets/config/manifest.js
file:
// ... other code here ...
//= link administrate_filterable/application.css
//= link administrate_filterable/application.js
Run rails assets:precompile
if the assets are not loaded.
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' %>
Example (app/views/admin/users/_search.html.erb
):
<form class="search" role="search">
... other code here ...
</form>
<%= render 'index_filter' %>
scoped_resource
methodThis 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 scoped_resource
method:
filtered_resources(your_scoped_resource)
For example:
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
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:
_index_header.html.erb
partial, 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):
belongs_to
association, etc)prompt
option, add include_blank
option, etc)scoped_resource
method)scoped_resource
method)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.
bundle exec rspec
MIT License
Huge thanks for the following resources that helped me a lot in creating this gem: