administrate_ransack 0.1.8 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -13
- data/app/views/administrate_ransack/_filters.html.erb +28 -42
- data/app/views/administrate_ransack/components/_field_belongs_to.html.erb +6 -0
- data/app/views/administrate_ransack/components/_field_boolean.html.erb +3 -0
- data/app/views/administrate_ransack/components/_field_date.html.erb +3 -0
- data/app/views/administrate_ransack/components/_field_datetime.html.erb +3 -0
- data/app/views/administrate_ransack/components/_field_has_many.html.erb +11 -0
- data/app/views/administrate_ransack/components/_field_number.html.erb +2 -0
- data/app/views/administrate_ransack/components/_field_other.html.erb +6 -0
- data/app/views/administrate_ransack/components/_field_select.html.erb +2 -0
- data/app/views/administrate_ransack/components/_field_string.html.erb +4 -0
- data/app/views/administrate_ransack/components/_filter_buttons.html.erb +4 -0
- data/config/locales/en.yml +1 -0
- data/lib/administrate_ransack/searchable.rb +15 -0
- data/lib/administrate_ransack/version.rb +1 -1
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d5b6b6df33ceb539a8c0abfead4d35fc568deec8019fbe4357fa4e7c49a7ec4
|
4
|
+
data.tar.gz: cd1434bf426ffe46b9ee4d8ec34c92aa6c12b2f973dcf1088e81d63568af1240
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c04a5d614096bd32d044440a55f6f76738525f5ffa88b020d6745cf89328242823154b6d77e356c981b7f9ce7093d55711a77eac72bc42a6a6bc3f844d2d47e3
|
7
|
+
data.tar.gz: 8de7bda5e911657aeb149c251038078164a4632b8dc2163a4ac08736770bb50b63a5ce1f9f8b37d6f6d60e0ce94000375b0a7baec487a414ae02a9662c2551f2
|
data/README.md
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
A plugin for [Administrate](https://github.com/thoughtbot/administrate) to use [Ransack](https://github.com/activerecord-hackery/ransack) for filtering resources.
|
3
3
|
|
4
4
|
Features:
|
5
|
-
- add Ransack
|
6
|
-
- offer a filters bar based on the resource's attributes;
|
5
|
+
- add Ransack search results using module prepend inside an Administrate controller;
|
6
|
+
- offer a filters side bar based on the resource's attributes;
|
7
7
|
- customize searchable attributes.
|
8
8
|
|
9
9
|
## Installation
|
@@ -14,13 +14,17 @@ prepend AdministrateRansack::Searchable
|
|
14
14
|
```
|
15
15
|
- Add to your resource index view:
|
16
16
|
```erb
|
17
|
-
<%= render('administrate_ransack/filters'
|
17
|
+
<%= render('administrate_ransack/filters') %>
|
18
18
|
```
|
19
|
+
- See the Usage section for extra options
|
19
20
|
|
20
21
|
## Usage
|
21
|
-
|
22
|
+
- The filters partial accepts some optional parameters:
|
23
|
+
+ `attribute_labels`: hash used to override the field labels, ex. `{ title: "The title" }`
|
24
|
+
+ `attribute_types`: hash used to specify the filter fields, ex. `{ title: Administrate::Field::String }`
|
25
|
+
+ `search_path`: the path to use for searching (form URL)
|
26
|
+
- For associations (has many/belongs to) the label used can be customized adding an `admin_label` method to the target model which returns a string while the collection can by filtered with `admin_scope`. Example:
|
22
27
|
|
23
|
-
Example:
|
24
28
|
```rb
|
25
29
|
class Post < ApplicationRecord
|
26
30
|
scope :admin_scope, -> { where(published: true) }
|
@@ -32,12 +36,7 @@ end
|
|
32
36
|
```
|
33
37
|
|
34
38
|
## Notes
|
35
|
-
- Administrate Search
|
36
|
-
- Ordering by clicking on the headers of the table preserving the Ransack searches requires a change to the headers links, replacing the th links of *_collection* partial with:
|
37
|
-
```rb
|
38
|
-
sort_link(@ransack_results, attr_name) do
|
39
|
-
# ...
|
40
|
-
```
|
39
|
+
- Administrate Search logic works independently from Ransack searches, I suggest to disable it eventually (ex. overriding `show_search_bar?` in the controller)
|
41
40
|
- Date/time filters use Rails `datetime_field` method which produces a `datetime-local` input field, at the moment this type of element is not broadly supported, a workaround is to include [flatpickr](https://github.com/flatpickr/flatpickr) datetime library.
|
42
41
|
+ This gem checks if `flatpickr` function is available in the global scope and applies it to the `datetime-local` filter inputs;
|
43
42
|
+ you can include the library using your application assets or via CDN, ex. adding to **app/views/layouts/admin/application.html.erb**:
|
@@ -52,9 +51,10 @@ sort_link(@ransack_results, attr_name) do
|
|
52
51
|
```
|
53
52
|
|
54
53
|
## Customizations
|
55
|
-
-
|
54
|
+
- Sample with different options provided:
|
56
55
|
```erb
|
57
56
|
<%
|
57
|
+
# In alternative prepare an hash in the dashboard like RANSACK_TYPES = {}
|
58
58
|
attribute_types = {
|
59
59
|
title: Administrate::Field::String,
|
60
60
|
author: Administrate::Field::BelongsTo,
|
@@ -68,9 +68,14 @@ attribute_labels = {
|
|
68
68
|
<%= render(
|
69
69
|
'administrate_ransack/filters',
|
70
70
|
attribute_types: attribute_types,
|
71
|
-
attribute_labels: attribute_labels
|
71
|
+
attribute_labels: attribute_labels,
|
72
|
+
search_path: admin_root_path
|
72
73
|
) %>
|
73
74
|
```
|
75
|
+
- An alternative is to prepare some hashes constants in the dashboard (ex. `RANSACK_TYPES`) and then:
|
76
|
+
```erb
|
77
|
+
<%= render('administrate_ransack/filters', attribute_types: @dashboard.class::RANSACK_TYPES) %>
|
78
|
+
```
|
74
79
|
- Optional basic style to setup the filters as a sidebar:
|
75
80
|
```css
|
76
81
|
.main-content__body {
|
@@ -98,6 +103,25 @@ attribute_labels = {
|
|
98
103
|
Screenshot:
|
99
104
|
![screenshot](screenshot.png)
|
100
105
|
|
106
|
+
## Extra notes
|
107
|
+
- If you need to define custom search logics you can skip prepending the module (`AdministrateRansack::Searchable`) and create your own search query in a controller, ex:
|
108
|
+
```ruby
|
109
|
+
def scoped_resource
|
110
|
+
@ransack_results = super.ransack(params[:q])
|
111
|
+
@ransack_results.result(distinct: true)
|
112
|
+
end
|
113
|
+
```
|
114
|
+
- Sometimes it's easier to create a new ransack field than overriding the search logic, example to search in a `jsonb` field adding to a Post model:
|
115
|
+
```ruby
|
116
|
+
ransacker :keywords do
|
117
|
+
Arel.sql("posts.metadata ->> 'keywords'")
|
118
|
+
end
|
119
|
+
```
|
120
|
+
- With this component you can easily link another resource applying some filters, example to add in a tag show page the link to the related posts:
|
121
|
+
```erb
|
122
|
+
<%= link_to("Tag's posts", admin_posts_path('q[tags_id_in][]': page.resource.id), class: "button") %>
|
123
|
+
```
|
124
|
+
|
101
125
|
## Do you like it? Star it!
|
102
126
|
If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
|
103
127
|
|
@@ -10,67 +10,53 @@
|
|
10
10
|
</script>
|
11
11
|
<% end %>
|
12
12
|
|
13
|
-
<%
|
14
|
-
|
13
|
+
<%
|
14
|
+
attribute_labels ||= {}
|
15
|
+
attribute_types ||= @dashboard.attribute_types.select { |key, _value| @dashboard.collection_attributes.include?(key) }
|
16
|
+
form_options = { html: { 'data-administrate-ransack-filters': '1' } }
|
17
|
+
if local_assigns.has_key?(:search_path)
|
18
|
+
form_path = @ransack_results
|
19
|
+
form_options[:url] = search_path
|
20
|
+
clear_filters_path = search_path
|
21
|
+
else
|
22
|
+
form_path = [:admin, @ransack_results]
|
23
|
+
clear_filters_path = url_for(url_for([:admin, @ransack_results.klass]))
|
24
|
+
end
|
25
|
+
%>
|
26
|
+
|
27
|
+
<%= search_form_for form_path, form_options do |f| %>
|
15
28
|
<div class="filters">
|
16
29
|
<% attribute_types.each do |field, type| %>
|
17
30
|
<% next if field == :id %>
|
18
31
|
|
19
|
-
|
20
|
-
|
32
|
+
<% label = attribute_labels.include?(field) ? attribute_labels[field] : field %>
|
33
|
+
<% input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s %>
|
34
|
+
<div class="filter <%= input_type.parameterize %>">
|
21
35
|
<% case input_type %>
|
22
36
|
<% when 'Administrate::Field::BelongsTo' %>
|
23
37
|
<% association = @ransack_results.klass.reflections[field.to_s] %>
|
24
|
-
|
25
|
-
<% label = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
|
26
|
-
<% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
|
27
|
-
<%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
|
28
|
-
<%= f.collection_select "#{field}_id_eq", collection, :id, label, include_blank: true %>
|
29
|
-
<% end %>
|
38
|
+
<%= render 'administrate_ransack/components/field_belongs_to', form: f, field: field, label: label, association: association %>
|
30
39
|
<% when 'Administrate::Field::Boolean' %>
|
31
|
-
<%= f
|
32
|
-
<%= f.select "#{field}_eq", [[t('administrate_ransack.filters.no'), false], [t('administrate_ransack.filters.yes'), true]], include_blank: true %>
|
40
|
+
<%= render 'administrate_ransack/components/field_boolean', form: f, field: field, label: label %>
|
33
41
|
<% when 'Administrate::Field::Date' %>
|
34
|
-
<%= f
|
35
|
-
<%= f.date_field "#{field}_gteq" %>
|
36
|
-
<%= f.date_field "#{field}_lteq" %>
|
42
|
+
<%= render 'administrate_ransack/components/field_date', form: f, field: field, label: label %>
|
37
43
|
<% when 'Administrate::Field::DateTime' %>
|
38
|
-
<%= f
|
39
|
-
<%= f.datetime_field "#{field}_gteq" %>
|
40
|
-
<%= f.datetime_field "#{field}_lteq" %>
|
44
|
+
<%= render 'administrate_ransack/components/field_datetime', form: f, field: field, label: label %>
|
41
45
|
<% when 'Administrate::Field::Email', 'Administrate::Field::String', 'Administrate::Field::Text' %>
|
42
|
-
<%= f
|
43
|
-
<%= f.search_field "#{field}_cont" %>
|
46
|
+
<%= render 'administrate_ransack/components/field_string', form: f, field: field, label: label %>
|
44
47
|
<% when 'Administrate::Field::Number' %>
|
45
|
-
<%= f
|
46
|
-
<%= f.number_field "#{field}_eq" %>
|
48
|
+
<%= render 'administrate_ransack/components/field_number', form: f, field: field, label: label %>
|
47
49
|
<% when 'Administrate::Field::HasMany' %>
|
48
50
|
<% association = @ransack_results.klass.reflections[field.to_s] %>
|
49
|
-
|
50
|
-
<% label = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
|
51
|
-
<% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
|
52
|
-
<%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
|
53
|
-
<%= f.collection_check_boxes "#{field}_id_in", collection, :id, label do |b| %>
|
54
|
-
<%= b.label do %>
|
55
|
-
<%= b.check_box %>
|
56
|
-
<span><%= b.object.send(label) %></span>
|
57
|
-
<% end %>
|
58
|
-
<% end %>
|
59
|
-
<% end %>
|
51
|
+
<%= render 'administrate_ransack/components/field_has_many', form: f, field: field, label: label, association: association %>
|
60
52
|
<% when 'Administrate::Field::Select' %>
|
61
|
-
<%= f
|
62
|
-
<%= f.select "#{field}_eq", type.options[:collection], include_blank: true %>
|
53
|
+
<%= render 'administrate_ransack/components/field_select', form: f, field: field, label: label, type: type %>
|
63
54
|
<% else %>
|
64
|
-
|
65
|
-
<!-- unsupported Field::Polymorphic -->
|
66
|
-
<!-- unsupported Field::Password -->
|
55
|
+
<%= render 'administrate_ransack/components/field_other', form: f, field: field, label: label, type: type %>
|
67
56
|
<% end %>
|
68
57
|
</div>
|
69
58
|
<% end %>
|
70
59
|
</div>
|
71
60
|
|
72
|
-
|
73
|
-
<%= f.submit %>
|
74
|
-
<%= link_to t('administrate_ransack.filters.clear_filters'), url_for([:admin, @ransack_results.klass]), class: 'btn-clear-filters' %>
|
75
|
-
</div>
|
61
|
+
<%= render 'administrate_ransack/components/filter_buttons', form: f, clear_filters_path: clear_filters_path %>
|
76
62
|
<% end %>
|
@@ -0,0 +1,6 @@
|
|
1
|
+
<% if association %>
|
2
|
+
<% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
|
3
|
+
<% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
|
4
|
+
<%= form.label(label) %>
|
5
|
+
<%= form.collection_select "#{field}_id_eq", collection, :id, desc, include_blank: true %>
|
6
|
+
<% end %>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<% if association %>
|
2
|
+
<% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
|
3
|
+
<% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
|
4
|
+
<%= form.label(label) %>
|
5
|
+
<%= form.collection_check_boxes "#{field}_id_in", collection, :id, desc do |b| %>
|
6
|
+
<%= b.label do %>
|
7
|
+
<%= b.check_box %>
|
8
|
+
<span><%= b.object.send(desc) %></span>
|
9
|
+
<% end %>
|
10
|
+
<% end %>
|
11
|
+
<% end %>
|
data/config/locales/en.yml
CHANGED
@@ -8,5 +8,20 @@ module AdministrateRansack
|
|
8
8
|
@ransack_results = super.ransack(params[:q])
|
9
9
|
@ransack_results.result(distinct: true)
|
10
10
|
end
|
11
|
+
|
12
|
+
# ref => https://github.com/thoughtbot/administrate/blob/v0.15.0/app/helpers/administrate/application_helper.rb#L54-L60
|
13
|
+
def sanitized_order_params(page, current_field_name)
|
14
|
+
collection_names = page.item_includes + [current_field_name]
|
15
|
+
association_params = collection_names.map do |assoc_name|
|
16
|
+
{ assoc_name => %i[order direction page per_page] }
|
17
|
+
end
|
18
|
+
params.permit(:search, :id, :page, :per_page, association_params, q: {})
|
19
|
+
end
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def prepended(base)
|
23
|
+
base.helper_method :sanitized_order_params
|
24
|
+
end
|
25
|
+
end
|
11
26
|
end
|
12
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: administrate_ransack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Roccoberton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: administrate
|
@@ -203,6 +203,16 @@ files:
|
|
203
203
|
- README.md
|
204
204
|
- Rakefile
|
205
205
|
- app/views/administrate_ransack/_filters.html.erb
|
206
|
+
- app/views/administrate_ransack/components/_field_belongs_to.html.erb
|
207
|
+
- app/views/administrate_ransack/components/_field_boolean.html.erb
|
208
|
+
- app/views/administrate_ransack/components/_field_date.html.erb
|
209
|
+
- app/views/administrate_ransack/components/_field_datetime.html.erb
|
210
|
+
- app/views/administrate_ransack/components/_field_has_many.html.erb
|
211
|
+
- app/views/administrate_ransack/components/_field_number.html.erb
|
212
|
+
- app/views/administrate_ransack/components/_field_other.html.erb
|
213
|
+
- app/views/administrate_ransack/components/_field_select.html.erb
|
214
|
+
- app/views/administrate_ransack/components/_field_string.html.erb
|
215
|
+
- app/views/administrate_ransack/components/_filter_buttons.html.erb
|
206
216
|
- config/locales/en.yml
|
207
217
|
- lib/administrate_ransack.rb
|
208
218
|
- lib/administrate_ransack/engine.rb
|