administrate_ransack 0.1.0 → 0.1.10

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: faad99daa51ceb91223e79770bc0df8aeb7adae21d76044b510cd7628a8fe323
4
- data.tar.gz: 3b1e7a5f12bc54086a182a72715342c52c79e0bdeef77af03a79bf0f659aa590
3
+ metadata.gz: a2dde6b1bd570497e553c4e737f4b48654af8470dfbe733c6e35a3bb74991b6e
4
+ data.tar.gz: e8fcfe208ba7ebc128ccfee07f816da649aa7a8ac0ab35e180e06832af070391
5
5
  SHA512:
6
- metadata.gz: bb14807a4275bdc1d62467bca4d30bc3ec9010df4af46516351ad3ec63b9cd863fce18a87dfac75d20d35a785561fea58a254457eadb645d69d9d0163878dd2c
7
- data.tar.gz: e4f7f6c47e66712a9c574ff0394739189a402f33281612113129ec10af6166d85ef5a8d73b0922a5d47e99f245e18ddd961abdec48cb491e525fd4b44110b9d2
6
+ metadata.gz: 5581bf2209ae91bb8b9b43ffe96be8cecbceadb22d0b0db05755c20443fd6b17ddfcbf70f500442c94c6bf05f4ad31ef3bb9243e3336b9f642b7ac4de4a520b0
7
+ data.tar.gz: cf69dfe636379448c07b703fbf1435f47fdfeb234b15683414215d756132277d8c1b80e7832e8c16fea2fbd1d0ee0e6a904ccde85d43ddcc0409133e6b8da278
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,23 +14,66 @@ 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
+ ```
19
+ - See the Customizations section to change the attributes list
20
+
21
+ ## Usage
22
+ 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`.
23
+
24
+ Example:
25
+ ```rb
26
+ class Post < ApplicationRecord
27
+ scope :admin_scope, -> { where(published: true) }
28
+
29
+ def admin_label
30
+ title.upcase
31
+ end
32
+ end
33
+ ```
34
+
35
+ ## Notes
36
+ - Administrate Search logic works independently from Ransack searches, I suggest to disable it eventually (ex. overriding `show_search_bar?` in the controller)
37
+ - 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:
38
+ ```rb
39
+ sort_link(@ransack_results, attr_name) do
40
+ # ...
41
+ end
42
+ ```
43
+ - 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.
44
+ + This gem checks if `flatpickr` function is available in the global scope and applies it to the `datetime-local` filter inputs;
45
+ + you can include the library using your application assets or via CDN, ex. adding to **app/views/layouts/admin/application.html.erb**:
46
+ ```html
47
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr@4.5.7/dist/flatpickr.min.css">
48
+ <script src="https://cdn.jsdelivr.net/npm/flatpickr@4.5.7/dist/flatpickr.min.js"></script>
49
+
50
+ <script>
51
+ // optionally change the flatpikr options:
52
+ window.flatpickr_filters_options = { dateFormat: "Y-m-d" };
53
+ </script>
18
54
  ```
19
55
 
20
56
  ## Customizations
21
- - Setup the fields for the filters in the index view:
57
+ - Allow only some fields for the filters in the index view:
22
58
  ```erb
23
- <% attribute_types = {
24
- author: Administrate::Field::BelongsTo,
59
+ <%
60
+ attribute_types = {
25
61
  title: Administrate::Field::String,
62
+ author: Administrate::Field::BelongsTo,
26
63
  published: Administrate::Field::Boolean
27
- } %>
64
+ }
65
+ attribute_labels = {
66
+ author: 'Written by',
67
+ title: nil
68
+ }
69
+ %>
28
70
  <%= render(
29
71
  'administrate_ransack/filters',
30
- attribute_types: attribute_types
72
+ attribute_types: attribute_types,
73
+ attribute_labels: attribute_labels
31
74
  ) %>
32
75
  ```
33
- - Optionally setup the layout for filters as a sidebar:
76
+ - Optional basic style to setup the filters as a sidebar:
34
77
  ```css
35
78
  .main-content__body {
36
79
  display: inline-block;
@@ -54,12 +97,33 @@ prepend AdministrateRansack::Searchable
54
97
  }
55
98
  ```
56
99
 
57
- ## Notes
58
- - 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
100
+ Screenshot:
101
+ ![screenshot](screenshot.png)
102
+
103
+ ## Extra notes
104
+ - 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:
105
+ ```ruby
106
+ def scoped_resource
107
+ @ransack_results = super.ransack(params[:q])
108
+ @ransack_results.result(distinct: true)
109
+ end
110
+ ```
111
+ - 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:
112
+ ```ruby
113
+ ransacker :keywords do
114
+ Arel.sql("posts.metadata ->> 'keywords'")
115
+ end
116
+ ```
117
+ - 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:
118
+ ```erb
119
+ <%= link_to("Tag's posts", admin_posts_path('q[tags_id_in][]': page.resource.id), class: "button") %>
120
+ ```
59
121
 
60
122
  ## Do you like it? Star it!
61
123
  If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
62
124
 
125
+ Or consider offering me a coffee, it's a small thing but it is greatly appreciated: [about me](https://www.blocknot.es/about-me).
126
+
63
127
  ## Contributors
64
128
  - [Mattia Roccoberton](https://blocknot.es/): author
65
129
 
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,3 +1,17 @@
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
+ <% attribute_labels ||= {} %>
14
+ <% attribute_types ||= @dashboard.attribute_types.select { |key, _value| @dashboard.collection_attributes.include?(key) } %>
1
15
  <%= search_form_for [:admin, @ransack_results], html: { 'data-administrate-ransack-filters': '1' } do |f| %>
2
16
  <div class="filters">
3
17
  <% attribute_types.each do |field, type| %>
@@ -10,32 +24,34 @@
10
24
  <% association = @ransack_results.klass.reflections[field.to_s] %>
11
25
  <% if association %>
12
26
  <% label = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
13
- <%= f.label field %>
14
- <%= f.collection_select "#{field}_id_eq", association.klass.all, :id, label, include_blank: true %>
27
+ <% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
28
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
29
+ <%= f.collection_select "#{field}_id_eq", collection, :id, label, include_blank: true %>
15
30
  <% end %>
16
31
  <% when 'Administrate::Field::Boolean' %>
17
- <%= f.label field %>
18
- <%= f.select "#{field}_eq", [['no', false], ['yes', true]], include_blank: true %>
32
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
33
+ <%= f.select "#{field}_eq", [[t('administrate_ransack.filters.no'), false], [t('administrate_ransack.filters.yes'), true]], include_blank: true %>
19
34
  <% when 'Administrate::Field::Date' %>
20
- <%= f.label field %>
35
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
21
36
  <%= f.date_field "#{field}_gteq" %>
22
37
  <%= f.date_field "#{field}_lteq" %>
23
38
  <% when 'Administrate::Field::DateTime' %>
24
- <%= f.label field %>
39
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
25
40
  <%= f.datetime_field "#{field}_gteq" %>
26
41
  <%= f.datetime_field "#{field}_lteq" %>
27
42
  <% when 'Administrate::Field::Email', 'Administrate::Field::String', 'Administrate::Field::Text' %>
28
- <%= f.label field %>
43
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
29
44
  <%= f.search_field "#{field}_cont" %>
30
45
  <% when 'Administrate::Field::Number' %>
31
- <%= f.label field %>
46
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
32
47
  <%= f.number_field "#{field}_eq" %>
33
48
  <% when 'Administrate::Field::HasMany' %>
34
49
  <% association = @ransack_results.klass.reflections[field.to_s] %>
35
50
  <% if association %>
36
51
  <% label = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
37
- <%= f.label field %>
38
- <%= f.collection_check_boxes "#{field}_id_in", association.klass.all, :id, label do |b| %>
52
+ <% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
53
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
54
+ <%= f.collection_check_boxes "#{field}_id_in", collection, :id, label do |b| %>
39
55
  <%= b.label do %>
40
56
  <%= b.check_box %>
41
57
  <span><%= b.object.send(label) %></span>
@@ -43,12 +59,13 @@
43
59
  <% end %>
44
60
  <% end %>
45
61
  <% when 'Administrate::Field::Select' %>
46
- <%= f.label field %>
62
+ <%= f.label(attribute_labels.include?(field) ? attribute_labels[field] : field) %>
47
63
  <%= f.select "#{field}_eq", type.options[:collection], include_blank: true %>
64
+ <% else %>
65
+ <!-- unsupported Field::HasOne -->
66
+ <!-- unsupported Field::Polymorphic -->
67
+ <!-- unsupported Field::Password -->
48
68
  <% end %>
49
- <%# unsupported Field::HasOne %>
50
- <%# unsupported Field::Polymorphic %>
51
- <%# unsupported Field::Password %>
52
69
  </div>
53
70
  <% end %>
54
71
  </div>
@@ -1,4 +1,6 @@
1
1
  en:
2
2
  administrate_ransack:
3
3
  filters:
4
- clear_filters: Clear filters
4
+ 'clear_filters': Clear filters
5
+ 'no': 'No'
6
+ 'yes': 'Yes'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdministrateRansack
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.10'
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.0
4
+ version: 0.1.10
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-09-20 00:00:00.000000000 Z
11
+ date: 2021-04-17 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