administrate_ransack 0.3.4 → 0.4.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 +24 -3
- data/app/views/administrate_ransack/_filters.html.erb +6 -40
- data/app/views/administrate_ransack/_javascript.html.erb +17 -0
- data/app/views/administrate_ransack/components/_field_belongs_to.html.erb +3 -1
- data/app/views/administrate_ransack/components/_field_boolean.html.erb +2 -1
- data/app/views/administrate_ransack/components/_field_date.html.erb +1 -1
- data/app/views/administrate_ransack/components/_field_datetime.html.erb +1 -1
- data/app/views/administrate_ransack/components/_field_has_many.html.erb +13 -5
- data/app/views/administrate_ransack/components/_field_number.html.erb +2 -1
- data/app/views/administrate_ransack/components/_field_other.html.erb +4 -4
- data/app/views/administrate_ransack/components/_field_select.html.erb +2 -1
- data/app/views/administrate_ransack/components/_field_string.html.erb +2 -1
- data/lib/administrate_ransack/filters.rb +16 -0
- data/lib/administrate_ransack/version.rb +1 -1
- data/lib/administrate_ransack.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a221cd6e8ff63813b5fbdb9ef6c2a137e1e1953a3ecafcd2db0034e9f958f35
|
4
|
+
data.tar.gz: 5c36e74bb7c07df45d0dd31184e1c5afb1974fd29c79b0a718f123e60152f1f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1dacc6dce1f49a453fe9cf9819688e54a09ad5fd9a61429ab6674a869a18d4f9e45910dd0547a6733db5e52b215fde3ac7f1f959acba2bc445498d9a7b34a36
|
7
|
+
data.tar.gz: 1802141cbf17b9b0a3b7b4c03c218549a7a1cadb24b6dc3edd27f2f9cce6a24835fd6892a30ae57a2b38561a800be72c941c106939a8f90ac0a307c51ce22fd2
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[![gem downloads](https://badgen.net/rubygems/dt/administrate_ransack)](https://rubygems.org/gems/administrate_ransack)
|
4
4
|
[![linters](https://github.com/blocknotes/administrate_ransack/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/administrate_ransack/actions/workflows/linters.yml)
|
5
5
|
[![specs](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs.yml/badge.svg)](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs.yml)
|
6
|
+
[![specs Rails7](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs2.yml/badge.svg)](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs2.yml)
|
6
7
|
|
7
8
|
A plugin for [Administrate](https://github.com/thoughtbot/administrate) to use [Ransack](https://github.com/activerecord-hackery/ransack) for filtering resources.
|
8
9
|
|
@@ -15,13 +16,17 @@ Features:
|
|
15
16
|
|
16
17
|
- After installing Administrate, add to *Gemfile*: `gem 'administrate_ransack'` (and execute `bundle`)
|
17
18
|
- Edit your admin resource controller adding inside the class body:
|
19
|
+
|
18
20
|
```rb
|
19
21
|
prepend AdministrateRansack::Searchable
|
20
22
|
```
|
21
|
-
|
23
|
+
|
24
|
+
- Add to your resource index view (ex. to generate the index for a Post model: `bin/rails generate administrate:views:index Post`):
|
25
|
+
|
22
26
|
```erb
|
23
27
|
<%= render('administrate_ransack/filters') %>
|
24
28
|
```
|
29
|
+
|
25
30
|
- See the Usage section for extra options
|
26
31
|
|
27
32
|
## Usage
|
@@ -44,6 +49,13 @@ class Post < ApplicationRecord
|
|
44
49
|
end
|
45
50
|
```
|
46
51
|
|
52
|
+
- For _has_many_ associations it is possible to use Selectize in place of the checkboxes using:
|
53
|
+
|
54
|
+
```erb
|
55
|
+
<!-- Set options for an association named: tags -->
|
56
|
+
<%= render('administrate_ransack/filters', options: { tags: 'select' } ) %>
|
57
|
+
```
|
58
|
+
|
47
59
|
- To use scopes in filters it's needed to update also the `ransackable_scopes` in the model, example:
|
48
60
|
|
49
61
|
```rb
|
@@ -74,6 +86,7 @@ end
|
|
74
86
|
- 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.
|
75
87
|
+ This gem checks if `flatpickr` function is available in the global scope and applies it to the `datetime-local` filter inputs;
|
76
88
|
+ you can include the library using your application assets or via CDN, ex. adding to **app/views/layouts/admin/application.html.erb**:
|
89
|
+
|
77
90
|
```html
|
78
91
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr@4.5.7/dist/flatpickr.min.css">
|
79
92
|
<script src="https://cdn.jsdelivr.net/npm/flatpickr@4.5.7/dist/flatpickr.min.js"></script>
|
@@ -87,6 +100,7 @@ end
|
|
87
100
|
## Customizations
|
88
101
|
|
89
102
|
- Sample call of the filters partial with different options provided:
|
103
|
+
|
90
104
|
```erb
|
91
105
|
<%
|
92
106
|
# In alternative prepare an hash in the dashboard like RANSACK_TYPES = {}
|
@@ -107,7 +121,9 @@ attribute_labels = {
|
|
107
121
|
search_path: admin_root_path
|
108
122
|
) %>
|
109
123
|
```
|
124
|
+
|
110
125
|
- Another option is to prepare some hashes constants in the dashboard (ex. `RANSACK_TYPES`):
|
126
|
+
|
111
127
|
```erb
|
112
128
|
<%= render('administrate_ransack/filters', attribute_types: @dashboard.class::RANSACK_TYPES) %>
|
113
129
|
```
|
@@ -176,19 +192,24 @@ Screenshot:
|
|
176
192
|
## Extra notes
|
177
193
|
|
178
194
|
- 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 (but you need to assign the Ransack search object to `@ransack_results` for the filters partial), for example:
|
179
|
-
|
195
|
+
|
196
|
+
```rb
|
180
197
|
def scoped_resource
|
181
198
|
@ransack_results = super.ransack(params[:q])
|
182
199
|
@ransack_results.result(distinct: true)
|
183
200
|
end
|
184
201
|
```
|
202
|
+
|
185
203
|
- Sometimes it's easier to create a new Ransack field than overriding the search logic (there are a lot of good examples in the [Ransack Wiki](https://github.com/activerecord-hackery/ransack/wiki/Using-Ransackers)), example to search in a `jsonb` field adding to a Post model:
|
186
|
-
|
204
|
+
|
205
|
+
```rb
|
187
206
|
ransacker :keywords do
|
188
207
|
Arel.sql("posts.metadata ->> 'keywords'")
|
189
208
|
end
|
190
209
|
```
|
210
|
+
|
191
211
|
- With Administrate Ransack you can easily create links to other resources applying some filters, example to add in a tag show page the link to the related posts:
|
212
|
+
|
192
213
|
```erb
|
193
214
|
<%= link_to("Tag's posts", admin_posts_path('q[tags_id_in][]': page.resource.id), class: "button") %>
|
194
215
|
```
|
@@ -1,24 +1,7 @@
|
|
1
|
-
<% content_for :javascript do %>
|
2
|
-
<script>
|
3
|
-
document.addEventListener('DOMContentLoaded', (_event) => {
|
4
|
-
// flatpickr setup
|
5
|
-
if(typeof window.flatpickr === 'function') {
|
6
|
-
var options = window.flatpickr_filters_options;
|
7
|
-
if(typeof options !== 'object') options = { enableTime: true };
|
8
|
-
window.flatpickr('.filter [type="datetime-local"]', options);
|
9
|
-
}
|
10
|
-
|
11
|
-
// selectize setup
|
12
|
-
$(function () {
|
13
|
-
$('.filters select').selectize();
|
14
|
-
});
|
15
|
-
});
|
16
|
-
</script>
|
17
|
-
<% end %>
|
18
|
-
|
19
1
|
<%
|
20
2
|
attribute_labels ||= {}
|
21
3
|
attribute_types ||= @dashboard.attribute_types.select { |key, _value| @dashboard.collection_attributes.include?(key) }
|
4
|
+
options ||= {}
|
22
5
|
form_options = { html: { 'data-administrate-ransack-filters': '1' } }
|
23
6
|
namespace = local_assigns[:namespace] || :admin
|
24
7
|
if local_assigns.has_key?(:search_path)
|
@@ -31,6 +14,8 @@
|
|
31
14
|
end
|
32
15
|
%>
|
33
16
|
|
17
|
+
<% render 'administrate_ransack/javascript' %>
|
18
|
+
|
34
19
|
<%= search_form_for form_path, form_options do |f| %>
|
35
20
|
<div class="filters">
|
36
21
|
<% attribute_types.each do |field, type| %>
|
@@ -39,29 +24,10 @@
|
|
39
24
|
<% label = attribute_labels.include?(field) ? attribute_labels[field] : field %>
|
40
25
|
<% model = @ransack_results.klass %>
|
41
26
|
<% input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s %>
|
27
|
+
<% component = AdministrateRansack::FILTERS[input_type] || 'field_other' %>
|
28
|
+
|
42
29
|
<div class="filter filter-<%= field.to_s.parameterize %> <%= input_type.parameterize %>">
|
43
|
-
|
44
|
-
<% when 'Administrate::Field::BelongsTo' %>
|
45
|
-
<% association = model.reflections[field.to_s] %>
|
46
|
-
<%= render 'administrate_ransack/components/field_belongs_to', form: f, model: model, field: field, label: label, association: association %>
|
47
|
-
<% when 'Administrate::Field::Boolean' %>
|
48
|
-
<%= render 'administrate_ransack/components/field_boolean', form: f, model: model, field: field, label: label %>
|
49
|
-
<% when 'Administrate::Field::Date' %>
|
50
|
-
<%= render 'administrate_ransack/components/field_date', form: f, model: model, field: field, label: label %>
|
51
|
-
<% when 'Administrate::Field::DateTime' %>
|
52
|
-
<%= render 'administrate_ransack/components/field_datetime', form: f, model: model, field: field, label: label %>
|
53
|
-
<% when 'Administrate::Field::Email', 'Administrate::Field::String', 'Administrate::Field::Text' %>
|
54
|
-
<%= render 'administrate_ransack/components/field_string', form: f, model: model, field: field, label: label %>
|
55
|
-
<% when 'Administrate::Field::Number' %>
|
56
|
-
<%= render 'administrate_ransack/components/field_number', form: f, model: model, field: field, label: label %>
|
57
|
-
<% when 'Administrate::Field::HasMany' %>
|
58
|
-
<% association = model.reflections[field.to_s] %>
|
59
|
-
<%= render 'administrate_ransack/components/field_has_many', form: f, model: model, field: field, label: label, association: association %>
|
60
|
-
<% when 'Administrate::Field::Select' %>
|
61
|
-
<%= render 'administrate_ransack/components/field_select', form: f, model: model, field: field, label: label, type: type %>
|
62
|
-
<% else %>
|
63
|
-
<%= render 'administrate_ransack/components/field_other', form: f, model: model, field: field, label: label, type: type %>
|
64
|
-
<% end %>
|
30
|
+
<%= render "administrate_ransack/components/#{component}", form: f, model: model, field: field, label: label, type: type, options: options[field] %>
|
65
31
|
</div>
|
66
32
|
<% end %>
|
67
33
|
</div>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<% content_for :javascript do %>
|
2
|
+
<script>
|
3
|
+
document.addEventListener('DOMContentLoaded', (_event) => {
|
4
|
+
// flatpickr setup
|
5
|
+
if(typeof window.flatpickr === 'function') {
|
6
|
+
var options = window.flatpickr_filters_options;
|
7
|
+
if(typeof options !== 'object') options = { enableTime: true };
|
8
|
+
window.flatpickr('.filter [type="datetime-local"]', options);
|
9
|
+
}
|
10
|
+
|
11
|
+
// selectize setup
|
12
|
+
$(function () {
|
13
|
+
$('.filters select').selectize();
|
14
|
+
});
|
15
|
+
});
|
16
|
+
</script>
|
17
|
+
<% end %>
|
@@ -1,7 +1,9 @@
|
|
1
|
+
<% association = model.reflections[field.to_s] %>
|
1
2
|
<% if association %>
|
2
3
|
<% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_id_eq" %>
|
3
4
|
<% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
|
4
5
|
<% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
|
5
|
-
|
6
|
+
|
7
|
+
<%= form.label(label, class: 'filter-label') %>
|
6
8
|
<%= form.collection_select(field_key, collection, :id, desc, include_blank: true) %>
|
7
9
|
<% end %>
|
@@ -1,4 +1,5 @@
|
|
1
1
|
<% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
|
2
2
|
<% values = [[t('administrate_ransack.filters.no'), false], [t('administrate_ransack.filters.yes'), true]] %>
|
3
|
-
|
3
|
+
|
4
|
+
<%= form.label(label, class: 'filter-label') %>
|
4
5
|
<%= form.select(field_key, values, include_blank: true) %>
|
@@ -1,12 +1,20 @@
|
|
1
|
+
<% association = model.reflections[field.to_s] %>
|
1
2
|
<% if association %>
|
2
3
|
<% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_id_in" %>
|
3
4
|
<% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
|
4
5
|
<% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
|
5
|
-
|
6
|
-
<%= form.
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
|
7
|
+
<%= form.label(label, class: 'filter-label') %>
|
8
|
+
<% if options&.include? 'select' %>
|
9
|
+
<%= form.select(field_key, nil, {}, multiple: true) do %>
|
10
|
+
<%= options_from_collection_for_select(collection, :id, desc) %>
|
11
|
+
<% end %>
|
12
|
+
<% else %>
|
13
|
+
<%= form.collection_check_boxes(field_key, collection, :id, desc) do |b| %>
|
14
|
+
<%= b.label do %>
|
15
|
+
<%= b.check_box %>
|
16
|
+
<span><%= b.object.send(desc) %></span>
|
17
|
+
<% end %>
|
10
18
|
<% end %>
|
11
19
|
<% end %>
|
12
20
|
<% end %>
|
@@ -1,6 +1,6 @@
|
|
1
|
-
<%= form.label(label) %>
|
1
|
+
<%= form.label(label, class: 'filter-label') %>
|
2
2
|
<%= form.search_field(field) %>
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
<%# unsupported Field::HasOne %>
|
5
|
+
<%# unsupported Field::Polymorphic %>
|
6
|
+
<%# unsupported Field::Password %>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdministrateRansack
|
4
|
+
FILTERS = {
|
5
|
+
'Administrate::Field::BelongsTo' => 'field_belongs_to',
|
6
|
+
'Administrate::Field::Boolean' => 'field_boolean',
|
7
|
+
'Administrate::Field::Date' => 'field_date',
|
8
|
+
'Administrate::Field::DateTime' => 'field_date',
|
9
|
+
'Administrate::Field::Email' => 'field_string',
|
10
|
+
'Administrate::Field::HasMany' => 'field_has_many',
|
11
|
+
'Administrate::Field::Number' => 'field_number',
|
12
|
+
'Administrate::Field::Select' => 'field_select',
|
13
|
+
'Administrate::Field::String' => 'field_string',
|
14
|
+
'Administrate::Field::Text' => 'field_string'
|
15
|
+
}.freeze
|
16
|
+
end
|
data/lib/administrate_ransack.rb
CHANGED
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.4.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: 2022-
|
11
|
+
date: 2022-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: administrate
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- README.md
|
64
64
|
- Rakefile
|
65
65
|
- app/views/administrate_ransack/_filters.html.erb
|
66
|
+
- app/views/administrate_ransack/_javascript.html.erb
|
66
67
|
- app/views/administrate_ransack/components/_field_belongs_to.html.erb
|
67
68
|
- app/views/administrate_ransack/components/_field_boolean.html.erb
|
68
69
|
- app/views/administrate_ransack/components/_field_date.html.erb
|
@@ -76,6 +77,7 @@ files:
|
|
76
77
|
- config/locales/en.yml
|
77
78
|
- lib/administrate_ransack.rb
|
78
79
|
- lib/administrate_ransack/engine.rb
|
80
|
+
- lib/administrate_ransack/filters.rb
|
79
81
|
- lib/administrate_ransack/searchable.rb
|
80
82
|
- lib/administrate_ransack/version.rb
|
81
83
|
homepage: https://github.com/blocknotes/administrate_ransack
|