administrate_ransack 0.2.0 → 0.3.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: 4d5b6b6df33ceb539a8c0abfead4d35fc568deec8019fbe4357fa4e7c49a7ec4
4
- data.tar.gz: cd1434bf426ffe46b9ee4d8ec34c92aa6c12b2f973dcf1088e81d63568af1240
3
+ metadata.gz: 07d7f30c351a3488f7eb7f2ba5c21ed52d1a935fc895070026af20209883d48f
4
+ data.tar.gz: 07b89a2beba6bdf5d6c59f17c100c70c7a90d73f85b3260715ba4b1641d566b4
5
5
  SHA512:
6
- metadata.gz: c04a5d614096bd32d044440a55f6f76738525f5ffa88b020d6745cf89328242823154b6d77e356c981b7f9ce7093d55711a77eac72bc42a6a6bc3f844d2d47e3
7
- data.tar.gz: 8de7bda5e911657aeb149c251038078164a4632b8dc2163a4ac08736770bb50b63a5ce1f9f8b37d6f6d60e0ce94000375b0a7baec487a414ae02a9662c2551f2
6
+ metadata.gz: c87a21c626b2553f8659740e785c5a7f893b35d021dc483d70d75ada047b729f8a04fcafaf95236f8dae55f25f61fc95a558580ae89bd68d68f520892ff303ee
7
+ data.tar.gz: a7040f799122eb75d22e1af2d24c86a84c51c5da6001219cf8ba67fa861b59f2a772fae3a13cfeb9cbaa4e1d3517c754e241723993bc5603ee68f23f409ec74c
data/README.md CHANGED
@@ -1,12 +1,14 @@
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)
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)
2
+
2
3
  A plugin for [Administrate](https://github.com/thoughtbot/administrate) to use [Ransack](https://github.com/activerecord-hackery/ransack) for filtering resources.
3
4
 
4
5
  Features:
5
6
  - add Ransack search results using module prepend inside an Administrate controller;
6
- - offer a filters side bar based on the resource's attributes;
7
+ - offer a filters partial based on the resource's attributes;
7
8
  - customize searchable attributes.
8
9
 
9
10
  ## Installation
11
+
10
12
  - After installing Administrate, add to *Gemfile*: `gem 'administrate_ransack'` (and execute `bundle`)
11
13
  - Edit your admin resource controller adding inside the class body:
12
14
  ```rb
@@ -19,13 +21,15 @@ prepend AdministrateRansack::Searchable
19
21
  - See the Usage section for extra options
20
22
 
21
23
  ## Usage
24
+
22
25
  - The filters partial accepts some optional parameters:
23
26
  + `attribute_labels`: hash used to override the field labels, ex. `{ title: "The title" }`
24
27
  + `attribute_types`: hash used to specify the filter fields, ex. `{ title: Administrate::Field::String }`
25
28
  + `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:
29
+ - 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:
27
30
 
28
31
  ```rb
32
+ # Sample post model
29
33
  class Post < ApplicationRecord
30
34
  scope :admin_scope, -> { where(published: true) }
31
35
 
@@ -35,8 +39,33 @@ class Post < ApplicationRecord
35
39
  end
36
40
  ```
37
41
 
42
+ - To use scopes in filters it's needed to update also the `ransackable_scopes` in the model, example:
43
+
44
+ ```rb
45
+ # Sample post model
46
+ class Post < ApplicationRecord
47
+ scope :recents, ->(dt = 1.month.ago) { where('dt > ?', dt).order(dt: :desc) }
48
+ scope :by_category, ->(category) { where(category: category) }
49
+
50
+ class << self
51
+ def ransackable_scopes(_auth_object = nil)
52
+ %i[by_category recents]
53
+ end
54
+ end
55
+ end
56
+ ```
57
+
58
+ ```erb
59
+ <!-- Sample index view -->
60
+ <%= render(
61
+ 'administrate_ransack/filters',
62
+ attribute_types: { recents: Administrate::Field::DateTime, by_category: Administrate::Field::String }
63
+ ) %>
64
+ ```
65
+
38
66
  ## Notes
39
- - Administrate Search logic works independently from Ransack searches, I suggest to disable it eventually (ex. overriding `show_search_bar?` in the controller)
67
+
68
+ - Administrate Search logic works independently from Ransack searches, I suggest to disable it eventually (ex. overriding `show_search_bar?` in the controller or removing the bar from the view)
40
69
  - 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.
41
70
  + This gem checks if `flatpickr` function is available in the global scope and applies it to the `datetime-local` filter inputs;
42
71
  + you can include the library using your application assets or via CDN, ex. adding to **app/views/layouts/admin/application.html.erb**:
@@ -51,7 +80,8 @@ end
51
80
  ```
52
81
 
53
82
  ## Customizations
54
- - Sample with different options provided:
83
+
84
+ - Sample call of the filters partial with different options provided:
55
85
  ```erb
56
86
  <%
57
87
  # In alternative prepare an hash in the dashboard like RANSACK_TYPES = {}
@@ -72,11 +102,11 @@ attribute_labels = {
72
102
  search_path: admin_root_path
73
103
  ) %>
74
104
  ```
75
- - An alternative is to prepare some hashes constants in the dashboard (ex. `RANSACK_TYPES`) and then:
105
+ - Another option is to prepare some hashes constants in the dashboard (ex. `RANSACK_TYPES`):
76
106
  ```erb
77
107
  <%= render('administrate_ransack/filters', attribute_types: @dashboard.class::RANSACK_TYPES) %>
78
108
  ```
79
- - Optional basic style to setup the filters as a sidebar:
109
+ - Some basic style to setup the filters as a sidebar (see the screenshot below):
80
110
  ```css
81
111
  .main-content__body {
82
112
  display: inline-block;
@@ -104,31 +134,35 @@ Screenshot:
104
134
  ![screenshot](screenshot.png)
105
135
 
106
136
  ## Extra notes
107
- - 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:
137
+
138
+ - 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:
108
139
  ```ruby
109
140
  def scoped_resource
110
141
  @ransack_results = super.ransack(params[:q])
111
142
  @ransack_results.result(distinct: true)
112
143
  end
113
144
  ```
114
- - 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:
145
+ - 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:
115
146
  ```ruby
116
147
  ransacker :keywords do
117
148
  Arel.sql("posts.metadata ->> 'keywords'")
118
149
  end
119
150
  ```
120
- - 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:
151
+ - 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:
121
152
  ```erb
122
153
  <%= link_to("Tag's posts", admin_posts_path('q[tags_id_in][]': page.resource.id), class: "button") %>
123
154
  ```
124
155
 
125
156
  ## Do you like it? Star it!
157
+
126
158
  If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
127
159
 
128
160
  Or consider offering me a coffee, it's a small thing but it is greatly appreciated: [about me](https://www.blocknot.es/about-me).
129
161
 
130
162
  ## Contributors
163
+
131
164
  - [Mattia Roccoberton](https://blocknot.es/): author
132
165
 
133
166
  ## License
167
+
134
168
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -30,29 +30,30 @@
30
30
  <% next if field == :id %>
31
31
 
32
32
  <% label = attribute_labels.include?(field) ? attribute_labels[field] : field %>
33
+ <% model = @ransack_results.klass %>
33
34
  <% input_type = type.is_a?(Administrate::Field::Deferred) ? type.deferred_class.to_s : type.to_s %>
34
35
  <div class="filter <%= input_type.parameterize %>">
35
36
  <% case input_type %>
36
37
  <% when 'Administrate::Field::BelongsTo' %>
37
- <% association = @ransack_results.klass.reflections[field.to_s] %>
38
- <%= render 'administrate_ransack/components/field_belongs_to', form: f, field: field, label: label, association: association %>
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 %>
39
40
  <% when 'Administrate::Field::Boolean' %>
40
- <%= render 'administrate_ransack/components/field_boolean', form: f, field: field, label: label %>
41
+ <%= render 'administrate_ransack/components/field_boolean', form: f, model: model, field: field, label: label %>
41
42
  <% when 'Administrate::Field::Date' %>
42
- <%= render 'administrate_ransack/components/field_date', form: f, field: field, label: label %>
43
+ <%= render 'administrate_ransack/components/field_date', form: f, model: model, field: field, label: label %>
43
44
  <% when 'Administrate::Field::DateTime' %>
44
- <%= render 'administrate_ransack/components/field_datetime', form: f, field: field, label: label %>
45
+ <%= render 'administrate_ransack/components/field_datetime', form: f, model: model, field: field, label: label %>
45
46
  <% when 'Administrate::Field::Email', 'Administrate::Field::String', 'Administrate::Field::Text' %>
46
- <%= render 'administrate_ransack/components/field_string', form: f, field: field, label: label %>
47
+ <%= render 'administrate_ransack/components/field_string', form: f, model: model, field: field, label: label %>
47
48
  <% when 'Administrate::Field::Number' %>
48
- <%= render 'administrate_ransack/components/field_number', form: f, field: field, label: label %>
49
+ <%= render 'administrate_ransack/components/field_number', form: f, model: model, field: field, label: label %>
49
50
  <% when 'Administrate::Field::HasMany' %>
50
- <% association = @ransack_results.klass.reflections[field.to_s] %>
51
- <%= render 'administrate_ransack/components/field_has_many', form: f, field: field, label: label, association: association %>
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 %>
52
53
  <% when 'Administrate::Field::Select' %>
53
- <%= render 'administrate_ransack/components/field_select', form: f, field: field, label: label, type: type %>
54
+ <%= render 'administrate_ransack/components/field_select', form: f, model: model, field: field, label: label, type: type %>
54
55
  <% else %>
55
- <%= render 'administrate_ransack/components/field_other', form: f, field: field, label: label, type: type %>
56
+ <%= render 'administrate_ransack/components/field_other', form: f, model: model, field: field, label: label, type: type %>
56
57
  <% end %>
57
58
  </div>
58
59
  <% end %>
@@ -1,6 +1,7 @@
1
1
  <% if association %>
2
+ <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_id_eq" %>
2
3
  <% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
3
4
  <% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
4
5
  <%= form.label(label) %>
5
- <%= form.collection_select "#{field}_id_eq", collection, :id, desc, include_blank: true %>
6
+ <%= form.collection_select(field_key, collection, :id, desc, include_blank: true) %>
6
7
  <% end %>
@@ -1,3 +1,4 @@
1
+ <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
1
2
  <% values = [[t('administrate_ransack.filters.no'), false], [t('administrate_ransack.filters.yes'), true]] %>
2
3
  <%= form.label(label) %>
3
- <%= form.select "#{field}_eq", values, include_blank: true %>
4
+ <%= form.select(field_key, values, include_blank: true) %>
@@ -1,3 +1,7 @@
1
1
  <%= form.label(label) %>
2
- <%= form.date_field "#{field}_gteq" %>
3
- <%= form.date_field "#{field}_lteq" %>
2
+ <% if model.ransackable_scopes.include?(field) %>
3
+ <%= form.date_field(field) %>
4
+ <% else %>
5
+ <%= form.date_field("#{field}_gteq") %>
6
+ <%= form.date_field("#{field}_lteq") %>
7
+ <% end %>
@@ -1,3 +1,7 @@
1
1
  <%= form.label(label) %>
2
- <%= form.datetime_field "#{field}_gteq" %>
3
- <%= form.datetime_field "#{field}_lteq" %>
2
+ <% if model.ransackable_scopes.include?(field) %>
3
+ <%= form.datetime_field(field) %>
4
+ <% else %>
5
+ <%= form.datetime_field("#{field}_gteq") %>
6
+ <%= form.datetime_field("#{field}_lteq") %>
7
+ <% end %>
@@ -1,8 +1,9 @@
1
1
  <% if association %>
2
+ <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_id_in" %>
2
3
  <% desc = association.klass.method_defined?(:admin_label) ? :admin_label : :to_s %>
3
4
  <% collection = association.klass.send(association.klass.respond_to?(:admin_scope) ? :admin_scope : :all) %>
4
5
  <%= form.label(label) %>
5
- <%= form.collection_check_boxes "#{field}_id_in", collection, :id, desc do |b| %>
6
+ <%= form.collection_check_boxes(field_key, collection, :id, desc) do |b| %>
6
7
  <%= b.label do %>
7
8
  <%= b.check_box %>
8
9
  <span><%= b.object.send(desc) %></span>
@@ -1,2 +1,3 @@
1
+ <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
1
2
  <%= form.label(label) %>
2
- <%= form.number_field "#{field}_eq" %>
3
+ <%= form.number_field(field_key) %>
@@ -1,2 +1,3 @@
1
+ <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_eq" %>
1
2
  <%= form.label(label) %>
2
- <%= form.select "#{field}_eq", type.options[:collection] || [], include_blank: true %>
3
+ <%= form.select(field_key, type.options[:collection] || [], include_blank: true) %>
@@ -1,4 +1,3 @@
1
+ <% field_key = model.ransackable_scopes.include?(field) ? field : "#{field}_cont" %>
1
2
  <%= form.label(label) %>
2
- <%= form.search_field("#{field}_cont") %>
3
-
4
- <%#= form.search_field(@ransack_results.klass.respond_to?(field) ? field : "#{field}_cont") %>
3
+ <%= form.search_field(field_key) %>
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AdministrateRansack
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
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.2.0
4
+ version: 0.3.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-01 00:00:00.000000000 Z
11
+ date: 2021-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: administrate
@@ -38,161 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
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'
125
- - !ruby/object:Gem::Dependency
126
- name: rubocop
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '0.90'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
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'
195
- description: A plugin for Administrate to use Ransack for filtering resources
41
+ description: A plugin for Administrate to use Ransack for search filters
196
42
  email:
197
43
  - mat@blocknot.es
198
44
  executables: []
@@ -237,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
83
  - !ruby/object:Gem::Version
238
84
  version: '0'
239
85
  requirements: []
240
- rubygems_version: 3.0.3
86
+ rubygems_version: 3.1.4
241
87
  signing_key:
242
88
  specification_version: 4
243
89
  summary: Administrate Ransack plugin