administrate_ransack 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07d7f30c351a3488f7eb7f2ba5c21ed52d1a935fc895070026af20209883d48f
4
- data.tar.gz: 07b89a2beba6bdf5d6c59f17c100c70c7a90d73f85b3260715ba4b1641d566b4
3
+ metadata.gz: 8a221cd6e8ff63813b5fbdb9ef6c2a137e1e1953a3ecafcd2db0034e9f958f35
4
+ data.tar.gz: 5c36e74bb7c07df45d0dd31184e1c5afb1974fd29c79b0a718f123e60152f1f6
5
5
  SHA512:
6
- metadata.gz: c87a21c626b2553f8659740e785c5a7f893b35d021dc483d70d75ada047b729f8a04fcafaf95236f8dae55f25f61fc95a558580ae89bd68d68f520892ff303ee
7
- data.tar.gz: a7040f799122eb75d22e1af2d24c86a84c51c5da6001219cf8ba67fa861b59f2a772fae3a13cfeb9cbaa4e1d3517c754e241723993bc5603ee68f23f409ec74c
6
+ metadata.gz: f1dacc6dce1f49a453fe9cf9819688e54a09ad5fd9a61429ab6674a869a18d4f9e45910dd0547a6733db5e52b215fde3ac7f1f959acba2bc445498d9a7b34a36
7
+ data.tar.gz: 1802141cbf17b9b0a3b7b4c03c218549a7a1cadb24b6dc3edd27f2f9cce6a24835fd6892a30ae57a2b38561a800be72c941c106939a8f90ac0a307c51ce22fd2
data/README.md CHANGED
@@ -1,4 +1,9 @@
1
- # Administrate Ransack [![Gem Version](https://badge.fury.io/rb/administrate_ransack.svg)](https://badge.fury.io/rb/administrate_ransack) [![Specs](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs.yml/badge.svg)](https://github.com/blocknotes/administrate_ransack/actions/workflows/specs.yml)
1
+ # Administrate Ransack
2
+ [![gem version](https://badge.fury.io/rb/administrate_ransack.svg)](https://badge.fury.io/rb/administrate_ransack)
3
+ [![gem downloads](https://badgen.net/rubygems/dt/administrate_ransack)](https://rubygems.org/gems/administrate_ransack)
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
+ [![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)
2
7
 
3
8
  A plugin for [Administrate](https://github.com/thoughtbot/administrate) to use [Ransack](https://github.com/activerecord-hackery/ransack) for filtering resources.
4
9
 
@@ -11,13 +16,17 @@ Features:
11
16
 
12
17
  - After installing Administrate, add to *Gemfile*: `gem 'administrate_ransack'` (and execute `bundle`)
13
18
  - Edit your admin resource controller adding inside the class body:
19
+
14
20
  ```rb
15
21
  prepend AdministrateRansack::Searchable
16
22
  ```
17
- - 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
+
18
26
  ```erb
19
27
  <%= render('administrate_ransack/filters') %>
20
28
  ```
29
+
21
30
  - See the Usage section for extra options
22
31
 
23
32
  ## Usage
@@ -26,6 +35,7 @@ prepend AdministrateRansack::Searchable
26
35
  + `attribute_labels`: hash used to override the field labels, ex. `{ title: "The title" }`
27
36
  + `attribute_types`: hash used to specify the filter fields, ex. `{ title: Administrate::Field::String }`
28
37
  + `search_path`: the path to use for searching (form URL)
38
+ + `namespace`: the namespace used by Administrate, ex. `:supervisor`
29
39
  - 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:
30
40
 
31
41
  ```rb
@@ -39,6 +49,13 @@ class Post < ApplicationRecord
39
49
  end
40
50
  ```
41
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
+
42
59
  - To use scopes in filters it's needed to update also the `ransackable_scopes` in the model, example:
43
60
 
44
61
  ```rb
@@ -69,6 +86,7 @@ end
69
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.
70
87
  + This gem checks if `flatpickr` function is available in the global scope and applies it to the `datetime-local` filter inputs;
71
88
  + you can include the library using your application assets or via CDN, ex. adding to **app/views/layouts/admin/application.html.erb**:
89
+
72
90
  ```html
73
91
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr@4.5.7/dist/flatpickr.min.css">
74
92
  <script src="https://cdn.jsdelivr.net/npm/flatpickr@4.5.7/dist/flatpickr.min.js"></script>
@@ -82,6 +100,7 @@ end
82
100
  ## Customizations
83
101
 
84
102
  - Sample call of the filters partial with different options provided:
103
+
85
104
  ```erb
86
105
  <%
87
106
  # In alternative prepare an hash in the dashboard like RANSACK_TYPES = {}
@@ -102,11 +121,17 @@ attribute_labels = {
102
121
  search_path: admin_root_path
103
122
  ) %>
104
123
  ```
124
+
105
125
  - Another option is to prepare some hashes constants in the dashboard (ex. `RANSACK_TYPES`):
126
+
106
127
  ```erb
107
128
  <%= render('administrate_ransack/filters', attribute_types: @dashboard.class::RANSACK_TYPES) %>
108
129
  ```
130
+
131
+ ## Sample styles
132
+
109
133
  - Some basic style to setup the filters as a sidebar (see the screenshot below):
134
+
110
135
  ```css
111
136
  .main-content__body {
112
137
  display: inline-block;
@@ -131,24 +156,60 @@ attribute_labels = {
131
156
  ```
132
157
 
133
158
  Screenshot:
134
- ![screenshot](screenshot.png)
159
+ ![screenshot](extra/screenshot.png)
160
+
161
+ - Alternative styles for an horizontal search bar:
162
+
163
+ ```css
164
+ [data-administrate-ransack-filters] {
165
+ border: 1px solid #ddd;
166
+ padding: 10px;
167
+ text-align: center;
168
+ }
169
+
170
+ [data-administrate-ransack-filters] .filters {
171
+ display: inline-block;
172
+ }
173
+
174
+ [data-administrate-ransack-filters] .filter, [data-administrate-ransack-filters] .filter > label {
175
+ display: inline-block;
176
+ }
177
+
178
+ [data-administrate-ransack-filters] .filter > input {
179
+ display: inline-block;
180
+ width: auto;
181
+ }
182
+
183
+ [data-administrate-ransack-filters] .filters-buttons {
184
+ display: inline-block;
185
+ margin-left: 20px;
186
+ }
187
+ ```
188
+
189
+ Screenshot:
190
+ ![screenshot2](extra/screenshot2.png)
135
191
 
136
192
  ## Extra notes
137
193
 
138
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:
139
- ```ruby
195
+
196
+ ```rb
140
197
  def scoped_resource
141
198
  @ransack_results = super.ransack(params[:q])
142
199
  @ransack_results.result(distinct: true)
143
200
  end
144
201
  ```
202
+
145
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:
146
- ```ruby
204
+
205
+ ```rb
147
206
  ransacker :keywords do
148
207
  Arel.sql("posts.metadata ->> 'keywords'")
149
208
  end
150
209
  ```
210
+
151
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
+
152
213
  ```erb
153
214
  <%= link_to("Tag's posts", admin_posts_path('q[tags_id_in][]': page.resource.id), class: "button") %>
154
215
  ```
@@ -162,6 +223,7 @@ Or consider offering me a coffee, it's a small thing but it is greatly appreciat
162
223
  ## Contributors
163
224
 
164
225
  - [Mattia Roccoberton](https://blocknot.es/): author
226
+ - The good guys that opened issues and pull requests from time to time
165
227
 
166
228
  ## License
167
229
 
@@ -1,29 +1,21 @@
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' } }
6
+ namespace = local_assigns[:namespace] || :admin
17
7
  if local_assigns.has_key?(:search_path)
18
8
  form_path = @ransack_results
19
9
  form_options[:url] = search_path
20
10
  clear_filters_path = search_path
21
11
  else
22
- form_path = [:admin, @ransack_results]
23
- clear_filters_path = url_for(url_for([:admin, @ransack_results.klass]))
12
+ form_path = [namespace, @ransack_results]
13
+ clear_filters_path = url_for(url_for([namespace, @ransack_results.klass]))
24
14
  end
25
15
  %>
26
16
 
17
+ <% render 'administrate_ransack/javascript' %>
18
+
27
19
  <%= search_form_for form_path, form_options do |f| %>
28
20
  <div class="filters">
29
21
  <% attribute_types.each do |field, type| %>
@@ -32,29 +24,10 @@
32
24
  <% label = attribute_labels.include?(field) ? attribute_labels[field] : field %>
33
25
  <% model = @ransack_results.klass %>
34
26
  <% input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s %>
35
- <div class="filter <%= input_type.parameterize %>">
36
- <% case input_type %>
37
- <% when 'Administrate::Field::BelongsTo' %>
38
- <% association = model.reflections[field.to_s] %>
39
- <%= render 'administrate_ransack/components/field_belongs_to', form: f, model: model, field: field, label: label, association: association %>
40
- <% when 'Administrate::Field::Boolean' %>
41
- <%= render 'administrate_ransack/components/field_boolean', form: f, model: model, field: field, label: label %>
42
- <% when 'Administrate::Field::Date' %>
43
- <%= render 'administrate_ransack/components/field_date', form: f, model: model, field: field, label: label %>
44
- <% when 'Administrate::Field::DateTime' %>
45
- <%= render 'administrate_ransack/components/field_datetime', form: f, model: model, field: field, label: label %>
46
- <% when 'Administrate::Field::Email', 'Administrate::Field::String', 'Administrate::Field::Text' %>
47
- <%= render 'administrate_ransack/components/field_string', form: f, model: model, field: field, label: label %>
48
- <% when 'Administrate::Field::Number' %>
49
- <%= render 'administrate_ransack/components/field_number', form: f, model: model, field: field, label: label %>
50
- <% when 'Administrate::Field::HasMany' %>
51
- <% association = model.reflections[field.to_s] %>
52
- <%= render 'administrate_ransack/components/field_has_many', form: f, model: model, field: field, label: label, association: association %>
53
- <% when 'Administrate::Field::Select' %>
54
- <%= render 'administrate_ransack/components/field_select', form: f, model: model, field: field, label: label, type: type %>
55
- <% else %>
56
- <%= render 'administrate_ransack/components/field_other', form: f, model: model, field: field, label: label, type: type %>
57
- <% 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] %>
58
31
  </div>
59
32
  <% end %>
60
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,6 +1,6 @@
1
- <%= form.label(label) %>
1
+ <%= form.label(label, class: 'filter-label') %>
2
2
  <% if model.ransackable_scopes.include?(field) %>
3
- <%= form.date_field(field) %>
3
+ <%= form.date_field(field, value: form.object.send(field)) %>
4
4
  <% else %>
5
5
  <%= form.date_field("#{field}_gteq") %>
6
6
  <%= form.date_field("#{field}_lteq") %>
@@ -1,6 +1,6 @@
1
- <%= form.label(label) %>
1
+ <%= form.label(label, class: 'filter-label') %>
2
2
  <% if model.ransackable_scopes.include?(field) %>
3
- <%= form.datetime_field(field) %>
3
+ <%= form.datetime_field(field, value: form.object.send(field)) %>
4
4
  <% else %>
5
5
  <%= form.datetime_field("#{field}_gteq") %>
6
6
  <%= form.datetime_field("#{field}_lteq") %>
@@ -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,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.select(field_key, type.options[: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.0'
4
+ VERSION = '0.4.0'
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.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: 2021-05-19 00:00:00.000000000 Z
11
+ date: 2022-04-04 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