administrate_ransack 0.3.2 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53f9b7117bb0a7ddf959ed0d6e6e054e781cbb50904e076ecba27790f221d2c0
4
- data.tar.gz: dbc0c054f730be1b2b6479e8fa4e72a55d0adf0d9f1f46c3143606700289e9ae
3
+ metadata.gz: 277042f10d332b8b9a9e78c7ebb01181dfd433b1149085490e510e85fceaec99
4
+ data.tar.gz: 52b9ff68844ebd4df6824734fff6f1cd92c8972e554c7cb1968ac5974af8673e
5
5
  SHA512:
6
- metadata.gz: bd13d31b54c05bacbc172eb81871044ab80e9fe1f5182fcdb20e7c4ef96245e3f95be05753abd5b7e7f5cd99367545530b992242d1845b6d4e9dea592d40862f
7
- data.tar.gz: 0befebe036ae09c3336bfd214ae904acb6bfc773d4fe16dbdb89ed5248d36d921616e4b526a7e9c1bbfd430106f3830588be79786d67d64b55b19782bb5da9ec
6
+ metadata.gz: a2d812002382843c657fbae431ae9ce4895a65ddd0735444cd8ee5e3588f801e5d755e131049c218c3b1d00493ca5de7f6cec16220fc297f2b902591c2c79951
7
+ data.tar.gz: d134be9599597ecde10f8d1684512f8d2df48dcdea1dfa2d966549f63c45a9494420b029da64e616f2e83b04b086acd49a084fe98f0780e341fd51771a9ffa15
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
- - Add to your resource index view:
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,12 +100,14 @@ 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 = {}
93
107
  attribute_types = {
94
108
  title: Administrate::Field::String,
95
109
  author: Administrate::Field::BelongsTo,
110
+ category: Administrate::Field::Select.with_options(collection: Post.categories.to_a),
96
111
  published: Administrate::Field::Boolean
97
112
  }
98
113
  attribute_labels = {
@@ -107,7 +122,9 @@ attribute_labels = {
107
122
  search_path: admin_root_path
108
123
  ) %>
109
124
  ```
125
+
110
126
  - Another option is to prepare some hashes constants in the dashboard (ex. `RANSACK_TYPES`):
127
+
111
128
  ```erb
112
129
  <%= render('administrate_ransack/filters', attribute_types: @dashboard.class::RANSACK_TYPES) %>
113
130
  ```
@@ -176,19 +193,24 @@ Screenshot:
176
193
  ## Extra notes
177
194
 
178
195
  - 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
- ```ruby
196
+
197
+ ```rb
180
198
  def scoped_resource
181
199
  @ransack_results = super.ransack(params[:q])
182
200
  @ransack_results.result(distinct: true)
183
201
  end
184
202
  ```
203
+
185
204
  - 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
- ```ruby
205
+
206
+ ```rb
187
207
  ransacker :keywords do
188
208
  Arel.sql("posts.metadata ->> 'keywords'")
189
209
  end
190
210
  ```
211
+
191
212
  - 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:
213
+
192
214
  ```erb
193
215
  <%= link_to("Tag's posts", admin_posts_path('q[tags_id_in][]': page.resource.id), class: "button") %>
194
216
  ```
@@ -1,18 +1,7 @@
1
- <% content_for :javascript do %>
2
- <script>
3
- document.addEventListener('DOMContentLoaded', (_event) => {
4
- if(typeof window.flatpickr === 'function') {
5
- var options = window.flatpickr_filters_options;
6
- if(typeof options !== 'object') options = { enableTime: true };
7
- window.flatpickr('.filter [type="datetime-local"]', options);
8
- }
9
- });
10
- </script>
11
- <% end %>
12
-
13
1
  <%
14
2
  attribute_labels ||= {}
15
3
  attribute_types ||= @dashboard.attribute_types.select { |key, _value| @dashboard.collection_attributes.include?(key) }
4
+ options ||= {}
16
5
  form_options = { html: { 'data-administrate-ransack-filters': '1' } }
17
6
  namespace = local_assigns[:namespace] || :admin
18
7
  if local_assigns.has_key?(:search_path)
@@ -25,6 +14,8 @@
25
14
  end
26
15
  %>
27
16
 
17
+ <% render 'administrate_ransack/javascript' %>
18
+
28
19
  <%= search_form_for form_path, form_options do |f| %>
29
20
  <div class="filters">
30
21
  <% attribute_types.each do |field, type| %>
@@ -33,29 +24,10 @@
33
24
  <% label = attribute_labels.include?(field) ? attribute_labels[field] : field %>
34
25
  <% model = @ransack_results.klass %>
35
26
  <% input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s %>
36
- <div class="filter <%= input_type.parameterize %>">
37
- <% case input_type %>
38
- <% when 'Administrate::Field::BelongsTo' %>
39
- <% association = model.reflections[field.to_s] %>
40
- <%= render 'administrate_ransack/components/field_belongs_to', form: f, model: model, field: field, label: label, association: association %>
41
- <% when 'Administrate::Field::Boolean' %>
42
- <%= render 'administrate_ransack/components/field_boolean', form: f, model: model, field: field, label: label %>
43
- <% when 'Administrate::Field::Date' %>
44
- <%= render 'administrate_ransack/components/field_date', form: f, model: model, field: field, label: label %>
45
- <% when 'Administrate::Field::DateTime' %>
46
- <%= render 'administrate_ransack/components/field_datetime', form: f, model: model, field: field, label: label %>
47
- <% when 'Administrate::Field::Email', 'Administrate::Field::String', 'Administrate::Field::Text' %>
48
- <%= render 'administrate_ransack/components/field_string', form: f, model: model, field: field, label: label %>
49
- <% when 'Administrate::Field::Number' %>
50
- <%= render 'administrate_ransack/components/field_number', form: f, model: model, field: field, label: label %>
51
- <% when 'Administrate::Field::HasMany' %>
52
- <% association = model.reflections[field.to_s] %>
53
- <%= render 'administrate_ransack/components/field_has_many', form: f, model: model, field: field, label: label, association: association %>
54
- <% when 'Administrate::Field::Select' %>
55
- <%= render 'administrate_ransack/components/field_select', form: f, model: model, field: field, label: label, type: type %>
56
- <% else %>
57
- <%= render 'administrate_ransack/components/field_other', form: f, model: model, field: field, label: label, type: type %>
58
- <% end %>
27
+ <% component = AdministrateRansack::FILTERS[input_type] || 'field_other' %>
28
+
29
+ <div class="filter filter-<%= field.to_s.parameterize %> <%= input_type.parameterize %>">
30
+ <%= render "administrate_ransack/components/#{component}", form: f, model: model, field: field, label: label, type: type, options: options[field] %>
59
31
  </div>
60
32
  <% end %>
61
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
- <%= form.label(label) %>
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
- <%= form.label(label) %>
3
+
4
+ <%= form.label(label, class: 'filter-label') %>
4
5
  <%= form.select(field_key, values, include_blank: true) %>
@@ -1,4 +1,4 @@
1
- <%= form.label(label) %>
1
+ <%= form.label(label, class: 'filter-label') %>
2
2
  <% if model.ransackable_scopes.include?(field) %>
3
3
  <%= form.date_field(field, value: form.object.send(field)) %>
4
4
  <% else %>
@@ -1,4 +1,4 @@
1
- <%= form.label(label) %>
1
+ <%= form.label(label, class: 'filter-label') %>
2
2
  <% if model.ransackable_scopes.include?(field) %>
3
3
  <%= form.datetime_field(field, value: form.object.send(field)) %>
4
4
  <% else %>
@@ -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
- <%= form.label(label) %>
6
- <%= form.collection_check_boxes(field_key, collection, :id, desc) do |b| %>
7
- <%= b.label do %>
8
- <%= b.check_box %>
9
- <span><%= b.object.send(desc) %></span>
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,3 +1,4 @@
1
1
  <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
2
- <%= form.label(label) %>
2
+
3
+ <%= form.label(label, class: 'filter-label') %>
3
4
  <%= form.number_field(field_key) %>
@@ -1,6 +1,6 @@
1
- <%= form.label(label) %>
1
+ <%= form.label(label, class: 'filter-label') %>
2
2
  <%= form.search_field(field) %>
3
3
 
4
- <!-- unsupported Field::HasOne -->
5
- <!-- unsupported Field::Polymorphic -->
6
- <!-- unsupported Field::Password -->
4
+ <%# unsupported Field::HasOne %>
5
+ <%# unsupported Field::Polymorphic %>
6
+ <%# unsupported Field::Password %>
@@ -1,3 +1,5 @@
1
1
  <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
2
- <%= form.label(label) %>
3
- <%= form.select(field_key, type.options[:collection] || [], include_blank: true) %>
2
+ <% collection = (type.respond_to?(:options) ? type.options[:collection] : []) || [] %>
3
+
4
+ <%= form.label(label, class: 'filter-label') %>
5
+ <%= form.select(field_key, collection, include_blank: true) %>
@@ -1,3 +1,4 @@
1
1
  <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_cont" %>
2
- <%= form.label(label) %>
2
+
3
+ <%= form.label(label, class: 'filter-label') %>
3
4
  <%= form.search_field(field_key) %>
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdministrateRansack
4
- VERSION = '0.3.2'
4
+ VERSION = '0.4.1'
5
5
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'administrate_ransack/engine'
4
+ require 'administrate_ransack/filters'
4
5
  require 'administrate_ransack/searchable'
6
+ require 'administrate_ransack/version'
5
7
 
6
8
  module AdministrateRansack
7
9
  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.3.2
4
+ version: 0.4.1
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-09-24 00:00:00.000000000 Z
11
+ date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: administrate
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: appraisal
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.4'
41
55
  description: A plugin for Administrate to use Ransack for search filters
42
56
  email:
43
57
  - mat@blocknot.es
@@ -49,6 +63,7 @@ files:
49
63
  - README.md
50
64
  - Rakefile
51
65
  - app/views/administrate_ransack/_filters.html.erb
66
+ - app/views/administrate_ransack/_javascript.html.erb
52
67
  - app/views/administrate_ransack/components/_field_belongs_to.html.erb
53
68
  - app/views/administrate_ransack/components/_field_boolean.html.erb
54
69
  - app/views/administrate_ransack/components/_field_date.html.erb
@@ -62,12 +77,16 @@ files:
62
77
  - config/locales/en.yml
63
78
  - lib/administrate_ransack.rb
64
79
  - lib/administrate_ransack/engine.rb
80
+ - lib/administrate_ransack/filters.rb
65
81
  - lib/administrate_ransack/searchable.rb
66
82
  - lib/administrate_ransack/version.rb
67
83
  homepage: https://github.com/blocknotes/administrate_ransack
68
84
  licenses:
69
85
  - MIT
70
- metadata: {}
86
+ metadata:
87
+ homepage_uri: https://github.com/blocknotes/administrate_ransack
88
+ source_code_uri: https://github.com/blocknotes/administrate_ransack
89
+ rubygems_mfa_required: 'true'
71
90
  post_install_message:
72
91
  rdoc_options: []
73
92
  require_paths:
@@ -83,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
102
  - !ruby/object:Gem::Version
84
103
  version: '0'
85
104
  requirements: []
86
- rubygems_version: 3.1.4
105
+ rubygems_version: 3.1.6
87
106
  signing_key:
88
107
  specification_version: 4
89
108
  summary: Administrate Ransack plugin