administrate_ransack 0.1.2 → 0.1.12

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: 88c77d51c97628de19019594f60a310000340c079d4adaf442005c1037da1748
4
- data.tar.gz: 41055594ee72a4e78c2d201ff04d758839238c122d917362382a8842b860b2a2
3
+ metadata.gz: 1be017567c90bc3b625f7db95063c8f79ec3176191d0ac8c97b2a720574d32da
4
+ data.tar.gz: c8ca5c07a2ce1e560d394736173db107a9b8a1177854bac626eb6c296e901f95
5
5
  SHA512:
6
- metadata.gz: cc773c880ca05607c1af0d11bf15b5bd9a1e6c7bf5a90060f7e49977cdb5f099d379f1617e9b91079c98321847e404c8e3e5cad94d2156ac986a425b95eaf4d4
7
- data.tar.gz: 253988df0bb9cb37804da6486b22867dfba2660a8065f38e13a353b549a9c655b300296e38651c1b293cace9bfd6685ed7c6e81f99d1b669b110b3591fb9cdb2
6
+ metadata.gz: 97465a3a36becda93904588d3505376b76481d35cb5f710788568f578a476c43c7dc2ff49a7f7d3b733e1f42c6988c5182a69bd1eb0660f193b013a6d1b9dbc9
7
+ data.tar.gz: dc13be2b62e76b562169e1646b627d7d2c5e410b3625c37aa1982470ed33d052e382098de2fbca904207caf69a94ed144c4e7d2c7bfcf4605c9e57b0ee11d08b
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # Administrate Ransack [![Gem Version](https://badge.fury.io/rb/administrate_ransack.svg)](https://badge.fury.io/rb/administrate_ransack)
1
+ # Administrate Ransack [![Gem Version](https://badge.fury.io/rb/administrate_ransack.svg)](https://badge.fury.io/rb/administrate_ransack) [![CircleCI](https://circleci.com/gh/blocknotes/administrate_ransack.svg?style=svg)](https://circleci.com/gh/blocknotes/administrate_ransack)
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 method via module prepend in controller;
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', attribute_types: page.attribute_types) %>
17
+ <%= render('administrate_ransack/filters') %>
18
18
  ```
19
+ - See the Customizations section to change the attributes list
19
20
 
20
21
  ## Usage
21
- 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`.
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,26 +36,52 @@ end
32
36
  ```
33
37
 
34
38
  ## Notes
35
- - Administrate Search input works independently from Ransack searches, I suggest to disable it eventually
39
+ - Administrate Search logic works independently from Ransack searches, I suggest to disable it eventually (ex. overriding `show_search_bar?` in the controller)
36
40
  - 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
41
  ```rb
38
42
  sort_link(@ransack_results, attr_name) do
39
- # ...
43
+ # ...
44
+ end
45
+ ```
46
+ - 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.
47
+ + This gem checks if `flatpickr` function is available in the global scope and applies it to the `datetime-local` filter inputs;
48
+ + you can include the library using your application assets or via CDN, ex. adding to **app/views/layouts/admin/application.html.erb**:
49
+ ```html
50
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr@4.5.7/dist/flatpickr.min.css">
51
+ <script src="https://cdn.jsdelivr.net/npm/flatpickr@4.5.7/dist/flatpickr.min.js"></script>
52
+
53
+ <script>
54
+ // optionally change the flatpikr options:
55
+ window.flatpickr_filters_options = { dateFormat: "Y-m-d" };
56
+ </script>
40
57
  ```
41
58
 
42
59
  ## Customizations
43
- - Allow only some fields for the filters in the index view:
60
+ - Sample with different options provided:
44
61
  ```erb
45
- <% attribute_types = {
46
- author: Administrate::Field::BelongsTo,
62
+ <%
63
+ # In alternative prepare an hash in the dashboard like RANSACK_TYPES = {}
64
+ attribute_types = {
47
65
  title: Administrate::Field::String,
66
+ author: Administrate::Field::BelongsTo,
48
67
  published: Administrate::Field::Boolean
49
- } %>
68
+ }
69
+ attribute_labels = {
70
+ author: 'Written by',
71
+ title: nil
72
+ }
73
+ %>
50
74
  <%= render(
51
75
  'administrate_ransack/filters',
52
- attribute_types: attribute_types
76
+ attribute_types: attribute_types,
77
+ attribute_labels: attribute_labels,
78
+ search_path: admin_root_path
53
79
  ) %>
54
80
  ```
81
+ - An alternative is to prepare some hashes constants in the dashboard (ex. `RANSACK_TYPES`) and then:
82
+ ```erb
83
+ <%= render('administrate_ransack/filters', attribute_types: @dashboard.class::RANSACK_TYPES) %>
84
+ ```
55
85
  - Optional basic style to setup the filters as a sidebar:
56
86
  ```css
57
87
  .main-content__body {
@@ -79,9 +109,30 @@ sort_link(@ransack_results, attr_name) do
79
109
  Screenshot:
80
110
  ![screenshot](screenshot.png)
81
111
 
112
+ ## Extra notes
113
+ - 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:
114
+ ```ruby
115
+ def scoped_resource
116
+ @ransack_results = super.ransack(params[:q])
117
+ @ransack_results.result(distinct: true)
118
+ end
119
+ ```
120
+ - 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:
121
+ ```ruby
122
+ ransacker :keywords do
123
+ Arel.sql("posts.metadata ->> 'keywords'")
124
+ end
125
+ ```
126
+ - 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:
127
+ ```erb
128
+ <%= link_to("Tag's posts", admin_posts_path('q[tags_id_in][]': page.resource.id), class: "button") %>
129
+ ```
130
+
82
131
  ## Do you like it? Star it!
83
132
  If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
84
133
 
134
+ Or consider offering me a coffee, it's a small thing but it is greatly appreciated: [about me](https://www.blocknot.es/about-me).
135
+
85
136
  ## Contributors
86
137
  - [Mattia Roccoberton](https://blocknot.es/): author
87
138
 
data/Rakefile CHANGED
@@ -1,7 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'bundler/gem_tasks'
4
+
3
5
  begin
4
- require 'bundler/setup'
6
+ require 'rspec/core/rake_task'
7
+
8
+ RSpec::Core::RakeTask.new(:spec) do |t|
9
+ # t.ruby_opts = %w[-w]
10
+ t.rspec_opts = ['--color', '--format documentation']
11
+ end
12
+
13
+ task default: :spec
5
14
  rescue LoadError
6
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
15
+ puts '! LoadError: no RSpec available'
7
16
  end
@@ -1,4 +1,27 @@
1
- <%= search_form_for [:admin, @ransack_results], html: { 'data-administrate-ransack-filters': '1' } do |f| %>
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
+ <%
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
+ else
21
+ form_path = [:admin, @ransack_results]
22
+ end
23
+ %>
24
+ <%= search_form_for form_path, form_options do |f| %>
2
25
  <div class="filters">
3
26
  <% attribute_types.each do |field, type| %>
4
27
  <% next if field == :id %>
@@ -11,32 +34,32 @@
11
34
  <% if association %>
12
35
  <% label = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
13
36
  <% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
14
- <%= f.label field %>
37
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
15
38
  <%= f.collection_select "#{field}_id_eq", collection, :id, label, include_blank: true %>
16
39
  <% end %>
17
40
  <% when 'Administrate::Field::Boolean' %>
18
- <%= f.label field %>
41
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
19
42
  <%= f.select "#{field}_eq", [[t('administrate_ransack.filters.no'), false], [t('administrate_ransack.filters.yes'), true]], include_blank: true %>
20
43
  <% when 'Administrate::Field::Date' %>
21
- <%= f.label field %>
44
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
22
45
  <%= f.date_field "#{field}_gteq" %>
23
46
  <%= f.date_field "#{field}_lteq" %>
24
47
  <% when 'Administrate::Field::DateTime' %>
25
- <%= f.label field %>
48
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
26
49
  <%= f.datetime_field "#{field}_gteq" %>
27
50
  <%= f.datetime_field "#{field}_lteq" %>
28
51
  <% when 'Administrate::Field::Email', 'Administrate::Field::String', 'Administrate::Field::Text' %>
29
- <%= f.label field %>
52
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
30
53
  <%= f.search_field "#{field}_cont" %>
31
54
  <% when 'Administrate::Field::Number' %>
32
- <%= f.label field %>
55
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
33
56
  <%= f.number_field "#{field}_eq" %>
34
57
  <% when 'Administrate::Field::HasMany' %>
35
58
  <% association = @ransack_results.klass.reflections[field.to_s] %>
36
59
  <% if association %>
37
60
  <% label = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
38
61
  <% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
39
- <%= f.label field %>
62
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
40
63
  <%= f.collection_check_boxes "#{field}_id_in", collection, :id, label do |b| %>
41
64
  <%= b.label do %>
42
65
  <%= b.check_box %>
@@ -45,12 +68,13 @@
45
68
  <% end %>
46
69
  <% end %>
47
70
  <% when 'Administrate::Field::Select' %>
48
- <%= f.label field %>
71
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
49
72
  <%= f.select "#{field}_eq", type.options[:collection], include_blank: true %>
73
+ <% else %>
74
+ <!-- unsupported Field::HasOne -->
75
+ <!-- unsupported Field::Polymorphic -->
76
+ <!-- unsupported Field::Password -->
50
77
  <% end %>
51
- <%# unsupported Field::HasOne %>
52
- <%# unsupported Field::Polymorphic %>
53
- <%# unsupported Field::Password %>
54
78
  </div>
55
79
  <% end %>
56
80
  </div>
@@ -4,11 +4,6 @@ require 'ransack'
4
4
 
5
5
  module AdministrateRansack
6
6
  module Searchable
7
- def index
8
- @dashboard = dashboard
9
- super
10
- end
11
-
12
7
  def scoped_resource
13
8
  @ransack_results = super.ransack(params[:q])
14
9
  @ransack_results.result(distinct: true)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdministrateRansack
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.12'
5
5
  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.1.2
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Roccoberton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-02 00:00:00.000000000 Z
11
+ date: 2021-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: administrate
@@ -16,28 +16,112 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.14.0
19
+ version: '0.14'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.14.0
26
+ version: '0.14'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ransack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 2.3.2
33
+ version: '2.3'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 2.3.2
40
+ version: '2.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activestorage
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '6.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '6.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: capybara
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.33'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.33'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: puma
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec_junit_formatter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.0'
41
125
  - !ruby/object:Gem::Dependency
42
126
  name: rubocop
43
127
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +136,62 @@ dependencies:
52
136
  - - "~>"
53
137
  - !ruby/object:Gem::Version
54
138
  version: '0.90'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sassc
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '2.4'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '2.4'
153
+ - !ruby/object:Gem::Dependency
154
+ name: selenium-webdriver
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.142'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.142'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sprockets-rails
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '3.2'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '3.2'
181
+ - !ruby/object:Gem::Dependency
182
+ name: sqlite3
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '1.4'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '1.4'
55
195
  description: A plugin for Administrate to use Ransack for filtering resources
56
196
  email:
57
197
  - mat@blocknot.es
@@ -87,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
227
  - !ruby/object:Gem::Version
88
228
  version: '0'
89
229
  requirements: []
90
- rubygems_version: 3.0.3
230
+ rubygems_version: 3.1.4
91
231
  signing_key:
92
232
  specification_version: 4
93
233
  summary: Administrate Ransack plugin