administrate_filterable 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +40 -20
- data/app/views/admin/application/_index_filter.html.erb +2 -2
- data/lib/administrate_filterable/filterer.rb +20 -18
- data/lib/administrate_filterable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bcbe156b91c20f14f2353af7c49bc072a9628955dce901cad747bddf89fd43e2
|
4
|
+
data.tar.gz: 120717fc6b4ff1da533b22760146dbbfc0e5c15a7e3602b9cfdd5e67b18b3739
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e3f47f07885b9b88eca9f28f30dd663ca391f802821b6a49ccdb1ac6206eeb0b2ffc896e84b173f09d2c3cff9d7fc04e02127e0a27a26ae003f4a629fbacf6f
|
7
|
+
data.tar.gz: d9dad475889e2ea75a0034e229820218978922fa2db3dfe2d01a9a78faff4b45abb65ed8b22780815edb8e23ea2bb6246579db82e65061a87a1a5db937b0897a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,18 +1,20 @@
|
|
1
|
-
# Administrate Custom Filter
|
2
|
-
|
3
1
|
![Gem](https://img.shields.io/gem/v/administrate_filterable.svg)
|
4
2
|
![CI](https://github.com/IrvanFza/administrate_filterable/workflows/CI/badge.svg)
|
5
3
|
|
6
|
-
|
4
|
+
# Administrate Custom Filter
|
5
|
+
|
6
|
+
An [Administrate](https://github.com/thoughtbot/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](https://github.com/activeadmin/activeadmin)'s filter.
|
7
7
|
|
8
|
-
|
8
|
+
![administrate_filterable-open](https://github.com/IrvanFza/administrate_filterable/assets/4778169/c127f425-ed7b-4ea8-b92f-4644072b1576)
|
9
9
|
|
10
|
-
|
10
|
+
## Why do you need this?
|
11
|
+
|
12
|
+
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).
|
11
13
|
|
12
14
|
But there are some drawbacks that I found:
|
13
|
-
1. Since it uses 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.
|
14
|
-
2. Again, because it
|
15
|
-
3. It's not user
|
15
|
+
1. 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.
|
16
|
+
2. Again, because it searches all the attributes, it will be hard to search for a specific attribute. For example, if you have `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.
|
17
|
+
3. 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.
|
16
18
|
|
17
19
|
Please share your thoughts if you have any other reasons.
|
18
20
|
|
@@ -42,7 +44,7 @@ $ bundle add administrate_filterable
|
|
42
44
|
## Usage
|
43
45
|
|
44
46
|
### Enable the filter
|
45
|
-
For each resource you want to add custom filter, add the following line to their
|
47
|
+
For each resource you want to add a custom filter, add the following line to their Administrate controller, respectively.
|
46
48
|
```ruby
|
47
49
|
include AdministrateFilterable::Filterer
|
48
50
|
```
|
@@ -57,7 +59,7 @@ end
|
|
57
59
|
```
|
58
60
|
|
59
61
|
### Customizing the filter fields
|
60
|
-
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.
|
62
|
+
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.
|
61
63
|
|
62
64
|
Example (`app/dashboards/user_dashboard.rb`):
|
63
65
|
```ruby
|
@@ -76,10 +78,10 @@ end
|
|
76
78
|
### Customizing the filter template
|
77
79
|
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.
|
78
80
|
|
79
|
-
For example, if you want to override the filter template for `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](app/views/admin/application/_index_filter.html.erb) and modify it to suit your needs.
|
81
|
+
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](app/views/admin/application/_index_filter.html.erb) and modify it to suit your needs.
|
80
82
|
|
81
83
|
### Asset Pipeline
|
82
|
-
If you use assets pipeline, you need to include this gem's assets
|
84
|
+
If you use an assets pipeline, you need to include this gem's assets in your `app/assets/config/manifest.js` file:
|
83
85
|
```javascript
|
84
86
|
// ... other code here ...
|
85
87
|
|
@@ -92,7 +94,7 @@ Run `rails assets:precompile` if the assets are not loaded.
|
|
92
94
|
|
93
95
|
## Troubleshooting
|
94
96
|
### Overridden default search template
|
95
|
-
By default this gem will add a filter button to `views/admin/application/_search.html.erb` partial. But if you have
|
97
|
+
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:
|
96
98
|
```erb
|
97
99
|
<%= render 'index_filter' %>
|
98
100
|
```
|
@@ -106,24 +108,42 @@ Example (`app/views/admin/users/_search.html.erb`):
|
|
106
108
|
<%= render 'index_filter' %>
|
107
109
|
```
|
108
110
|
|
111
|
+
### Overridden `scoped_resource` method
|
112
|
+
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.
|
113
|
+
|
114
|
+
To fix this, you need to add the following code in the last line of your overridden `scoped_resource` method:
|
115
|
+
```ruby
|
116
|
+
filtered_resources(your_scoped_resource)
|
117
|
+
```
|
118
|
+
|
119
|
+
For example:
|
120
|
+
```ruby
|
121
|
+
def scoped_resource
|
122
|
+
# Your custom filter logic here
|
123
|
+
resources = super.where(status: 'active')
|
124
|
+
# Add this line to make the filter work
|
125
|
+
filtered_resources(resources)
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
109
129
|
### Filter button not showing
|
110
130
|
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.
|
111
131
|
|
112
132
|
The reason why I use the partial is because:
|
113
133
|
1. It is rarely overridden by the user, so you won't have to add the button manually in most cases.
|
114
|
-
2. Previously, I
|
134
|
+
2. Previously, I used the `_index_header.html.erb` partial, but it conflicts with the export button from [administrate_exportable](https://github.com/SourceLabsLLC/administrate_exportable). Using both gems results in one button missing, due to the partial override.
|
115
135
|
|
116
136
|
## To Do
|
117
137
|
There are still a lot of things to do to make this gem better. Here are some of them (sorted by highest priority first):
|
118
|
-
- [ ] Add support for relational filter (e.g. filter by `belongs_to` association, etc)
|
138
|
+
- [ ] Add support for the relational filter (e.g. filter by `belongs_to` association, etc)
|
119
139
|
- [ ] Add support to customize the dropdown list (e.g. add `prompt` option, add `include_blank` option, etc)
|
120
140
|
- [ ] Exclude checkbox, radio, or select value from the filter params if no action is performed on them
|
121
141
|
- [ ] Figure out a better way to implement the filter functionality (currently I override the `scoped_resource` method)
|
122
|
-
- [ ] Figure out a better way to pass the filter attributes to the form (currently I use instance variable in the overridden `scoped_resource` method)
|
142
|
+
- [ ] Figure out a better way to pass the filter attributes to the form (currently I use an instance variable in the overridden `scoped_resource` method)
|
123
143
|
- [ ] Add capability to customize the filter behavior (e.g. search by exact match, search by partial match, etc just like in the ActiveAdmin filter)
|
124
144
|
- [ ] Improve the toggle button user experience (e.g. add open/close animation, add dynamic open/close title, etc)
|
125
145
|
|
126
|
-
If you have any
|
146
|
+
If you have any ideas or suggestions, please let me know by creating an issue or pull request.
|
127
147
|
|
128
148
|
## Contributing
|
129
149
|
You can help me to improve this gem by contributing to this project. Any help is highly appreciated.
|
@@ -136,6 +156,6 @@ You can help me to improve this gem by contributing to this project. Any help is
|
|
136
156
|
[MIT License](https://github.com/IrvanFza/administrate_filterable/blob/master/LICENSE)
|
137
157
|
|
138
158
|
## Credits
|
139
|
-
Huge thanks for the following resources that
|
140
|
-
- [administrate_exportable](https://github.com/SourceLabsLLC/administrate_exportable):
|
141
|
-
- [Off-Canvas Menu](https://web.archive.org/web/20210304195120/https://codepen.io/11bits/pen/jryEGW): 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.
|
159
|
+
Huge thanks for the following resources that helped me a lot in creating this gem:
|
160
|
+
- [administrate_exportable](https://github.com/SourceLabsLLC/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.
|
161
|
+
- [Off-Canvas Menu](https://web.archive.org/web/20210304195120/https://codepen.io/11bits/pen/jryEGW): 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.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
<% if @
|
1
|
+
<% if @filterable_attributes.present? %>
|
2
2
|
<% resource_title = display_resource_name(resource_name) %>
|
3
3
|
|
4
4
|
<% # TODO: Improve the toggle button user experience (e.g. add open/close animation, add dynamic open/close title, etc) %>
|
@@ -14,7 +14,7 @@
|
|
14
14
|
</header>
|
15
15
|
|
16
16
|
<%= form_with(model: [:admin, new_resource], method: :get, html: { class: "form administrate-filterable__form" }) do |f| %>
|
17
|
-
<% @
|
17
|
+
<% @filterable_attributes.each do |attribute| -%>
|
18
18
|
<% # TODO: Add capability to customize the filter behavior (e.g. search by exact match, search by partial match, etc just like in the ActiveAdmin filter) %>
|
19
19
|
<div class="field-unit field-unit--<%= attribute.html_class %> administrate-filterable__field">
|
20
20
|
<%= render_field attribute, f: f %>
|
@@ -9,30 +9,32 @@ module AdministrateFilterable
|
|
9
9
|
class_methods do
|
10
10
|
def filterable
|
11
11
|
# TODO: Figure out a better way to implement the filter functionality
|
12
|
-
#
|
13
|
-
#
|
12
|
+
# I don't think overriding `scoped_resource` is the ideal solution to implement the filter functionality.
|
13
|
+
# Because when the Administrate controller is generated, it includes suggestion for overriding `scoped_resource`.
|
14
|
+
# I already tried to override Administrate `filter_resources` method, but it doesn't work as expected.
|
15
|
+
# So, let's stick with this solution for now. Suggestions are very welcomed!
|
14
16
|
define_method(:scoped_resource) do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
data = resource_class.all
|
17
|
+
resources = resource_class.default_scoped
|
18
|
+
filtered_resources(resources)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
23
|
+
def filtered_resources(resources)
|
24
|
+
@filterable_attributes = AdministrateFilterable::FiltererService.filter_attributes(dashboard, new_resource)
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
filter_params = params[resource_name]
|
27
|
+
return resources if filter_params.blank?
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
data = data.where(sanitized_query)
|
31
|
-
end
|
29
|
+
filter_params.each do |key, value|
|
30
|
+
next unless resources.column_names.include?(key.to_s) && value.present?
|
32
31
|
|
33
|
-
|
34
|
-
|
32
|
+
# TODO: Add support for relational filter (e.g. filter by `belongs_to` association, etc)
|
33
|
+
sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, ["#{key} LIKE ?", "%#{value}%"])
|
34
|
+
resources = resources.where(sanitized_query)
|
35
35
|
end
|
36
|
+
|
37
|
+
resources
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: administrate_filterable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Irvan Fauziansyah
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|