activeadmin-searchable_select 1.5.0 → 1.6.0

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: eae3cd46ff6e641c50c4f6e02c7aa4ab8105a160740c07fd82efea3eb4af26e8
4
- data.tar.gz: 37274666f643b0df81a37aa0dbbd197cc7b94f45ef804398496a1ecbb3f1094f
3
+ metadata.gz: 86f2b11107d464955292c9d5a51629757ff3fb213aafdeb9302b948f421796ee
4
+ data.tar.gz: 961efac5444a994afac0b8a13437bbd96ec8effedbbc6356b606a9d23bc80849
5
5
  SHA512:
6
- metadata.gz: a0448e24dbb3c48ca7421eca976836d5be764e91db9e89a129e8c9b47dbb5dbbbdb94ac8937d049b5af35d6c68a17933bc6e3712719fd920b125e65d29147b88
7
- data.tar.gz: 4c90fe918890f40de37d138ba3b82b12307bf5331772a0dd8a139afb1698a8cf3a95620a72372415437b103851ee4ae574807d11a888cd8b850a6d360a2af6e9
6
+ metadata.gz: 79b204c1f6af56e2d39c4ee658cfde5af5ca31733ed3d2eb657ab78a8de66511b9e7387dd5fa64a457768095c67858e64f3fd598058ef1458fceba0575880522
7
+ data.tar.gz: 9bcbd53a3504788a0c18ce8f701efefcc2f0495625eedf277a30c738c5083d4b3a4602fdd95c05ca904ad8b0034e551ea1706a46f077805c5b0146f0121ee2fc
@@ -1,5 +1,9 @@
1
1
  name: tests
2
- on: [push, pull_request]
2
+ on:
3
+ push:
4
+ pull_request:
5
+ schedule:
6
+ - cron: '45 2 * * *'
3
7
 
4
8
  jobs:
5
9
  rspec:
@@ -31,4 +35,4 @@ jobs:
31
35
  ruby-version: ${{ matrix.ruby-version }}
32
36
  bundler-cache: true
33
37
  - name: Run tests
34
- run: bundle exec rspec
38
+ run: bundle exec rspec
data/CHANGELOG.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
- ### Version 1.5.0
3
+ ### Version 1.6.0
4
4
 
5
- 2021-02-16
5
+ 2022-04-05
6
6
 
7
- [Compare changes](https://github.com/codevise/activeadmin-searchable_select/compare/1-4-stable...v1.5.0)
7
+ [Compare changes](https://github.com/codevise/activeadmin-searchable_select/compare/1-5-stable...v1.6.0)
8
8
 
9
- - Allow adding additional payload to option items in AJAX response
10
- ([#33](https://github.com/codevise/activeadmin-searchable_select/pull/33))
9
+ - Add option to pass path params for nested resource fetching
10
+ ([#39](https://github.com/codevise/activeadmin-searchable_select/pull/39))
11
11
 
12
12
  See
13
- [1-4-stable branch](https://github.com/codevise/activeadmin-searchable_select/blob/1-4-stable/CHANGELOG.md)
13
+ [1-5-stable branch](https://github.com/codevise/activeadmin-searchable_select/blob/1-5-stable/CHANGELOG.md)
14
14
  for previous changes.
data/README.md CHANGED
@@ -242,6 +242,67 @@ argument:
242
242
  end
243
243
  ```
244
244
 
245
+ #### Path options for nested resources
246
+
247
+ Example for the following setup:
248
+
249
+ ```ruby
250
+ # Models
251
+ class OptionType < ActiveRecord::Base; end
252
+
253
+ class OptionValue < ActiveRecord::Base
254
+ belongs_to :option_type
255
+ end
256
+
257
+ class Product < ActiveRecord::Base
258
+ belongs_to :option_type
259
+ has_many :variants
260
+ end
261
+
262
+ class Variant < ActiveRecord::Base
263
+ belongs_to :product
264
+ belongs_to :option_value
265
+ end
266
+
267
+ # ActiveAdmin
268
+ ActiveAdmin.register(OptionType)
269
+
270
+ ActiveAdmin.register(Product)
271
+
272
+ ActiveAdmin.register(OptionValue) do
273
+ belongs_to :option_type
274
+ searchable_select_options(scope: lambda do |params|
275
+ OptionValue.where(
276
+ option_type_id: params[:option_type_id]
277
+ )
278
+ end,
279
+ text_attribute: :value)
280
+ end
281
+ ```
282
+
283
+ It is possible to pass path parameters for correctly generating URLs for nested resources fetching via `path_params`
284
+
285
+ ```ruby
286
+ ActiveAdmin.register(Variant) do
287
+ belongs_to :product
288
+
289
+ form do |f|
290
+ ...
291
+ f.input(:option_value,
292
+ as: :searchable_select,
293
+ ajax: {
294
+ resource: OptionValue,
295
+ path_params: {
296
+ option_type_id: f.object.product.option_type_id
297
+ }
298
+ })
299
+ ...
300
+ end
301
+ end
302
+ ```
303
+
304
+ This will generate the path for fetching as `all_options_admin_option_type_option_values(option_type_id: f.object.product.option_type_id)` (e.g. `/admin/option_types/2/option_values/all_options`)
305
+
245
306
  #### Inlining Ajax Options in Feature Tests
246
307
 
247
308
  When writing UI driven feature specs (i.e. with Capybara),
@@ -265,7 +326,7 @@ for feature specs:
265
326
 
266
327
  ### Passing options to Select2
267
328
 
268
- It is possible to pass and define configuration options to Select2
329
+ It is possible to pass and define configuration options to Select2
269
330
  via `data-attributes` using nested (subkey) options.
270
331
 
271
332
  Attributes need to be added to the `input_html` option in the form input.
@@ -20,6 +20,10 @@ module ActiveAdmin
20
20
  # - `params`: Hash of query parameters that shall be passed to the
21
21
  # options endpoint.
22
22
  #
23
+ # - `path_params`: Hash of parameters, which would be passed to the
24
+ # dynamic collection path generation for the resource.
25
+ # e.g `admin_articles_path(path_params)`
26
+ #
23
27
  # If the `ajax` option is present, the `collection` option is
24
28
  # ignored.
25
29
  module SelectInputExtension
@@ -45,7 +49,7 @@ module ActiveAdmin
45
49
 
46
50
  def ajax_url
47
51
  return unless options[:ajax]
48
- [ajax_resource.route_collection_path,
52
+ [ajax_resource.route_collection_path(path_params),
49
53
  '/',
50
54
  option_collection.collection_action_name,
51
55
  '?',
@@ -124,6 +128,10 @@ module ActiveAdmin
124
128
  ajax_options.fetch(:params, {})
125
129
  end
126
130
 
131
+ def path_params
132
+ ajax_options.fetch(:path_params, {})
133
+ end
134
+
127
135
  def ajax_options
128
136
  options[:ajax] == true ? {} : options[:ajax]
129
137
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveAdmin
2
2
  module SearchableSelect
3
- VERSION = '1.5.0'.freeze
3
+ VERSION = '1.6.0'.freeze
4
4
  end
5
5
  end
@@ -94,6 +94,94 @@ RSpec.describe 'end to end', type: :feature, js: true do
94
94
  end
95
95
  end
96
96
 
97
+ context 'class with nested belongs_to association' do
98
+ before(:all) do
99
+ ActiveAdminHelpers.setup do
100
+ ActiveAdmin.register(OptionType)
101
+
102
+ ActiveAdmin.register(Product)
103
+
104
+ ActiveAdmin.register(OptionValue) do
105
+ belongs_to :option_type
106
+ searchable_select_options(scope: lambda do |params|
107
+ OptionValue.where(
108
+ option_type_id: params[:option_type_id]
109
+ )
110
+ end,
111
+ text_attribute: :value)
112
+ end
113
+
114
+ ActiveAdmin.register(Variant) do
115
+ belongs_to :product
116
+
117
+ form do |f|
118
+ input :price
119
+ input(:option_value,
120
+ as: :searchable_select,
121
+ ajax: {
122
+ resource: OptionValue,
123
+ path_params: {
124
+ option_type_id: f.object.product.option_type_id
125
+ }
126
+ })
127
+ end
128
+ end
129
+
130
+ ActiveAdmin.setup {}
131
+ end
132
+ end
133
+
134
+ describe 'new page with searchable select filter' do
135
+ it 'loads filter input options' do
136
+ option_type = OptionType.create(name: 'Color')
137
+ ot = OptionType.create(name: 'Size')
138
+ OptionValue.create(value: 'Black', option_type: option_type)
139
+ OptionValue.create(value: 'Orange', option_type: option_type)
140
+ OptionValue.create(value: 'M', option_type: ot)
141
+ product = Product.create(name: 'Cap', option_type: option_type)
142
+
143
+ visit "/admin/products/#{product.id}/variants/new"
144
+
145
+ expand_select_box
146
+ wait_for_ajax
147
+
148
+ expect(select_box_items).to eq(%w(Black Orange))
149
+ end
150
+
151
+ it 'allows filtering options by term' do
152
+ option_type = OptionType.create(name: 'Color')
153
+ ot = OptionType.create(name: 'Size')
154
+ OptionValue.create(value: 'Black', option_type: option_type)
155
+ OptionValue.create(value: 'Orange', option_type: option_type)
156
+ OptionValue.create(value: 'M', option_type: ot)
157
+ product = Product.create(name: 'Cap', option_type: option_type)
158
+
159
+ visit "/admin/products/#{product.id}/variants/new"
160
+
161
+ expand_select_box
162
+ enter_search_term('O')
163
+ wait_for_ajax
164
+
165
+ expect(select_box_items).to eq(%w(Orange))
166
+ end
167
+
168
+ it 'loads more items when scrolling down' do
169
+ option_type = OptionType.create(name: 'Color')
170
+ 15.times { |i| OptionValue.create(value: "Black #{i}", option_type: option_type) }
171
+ product = Product.create(name: 'Cap', option_type: option_type)
172
+
173
+ visit "/admin/products/#{product.id}/variants/new"
174
+
175
+ expand_select_box
176
+ wait_for_ajax
177
+ scroll_select_box_list
178
+ wait_for_ajax
179
+
180
+ expect(select_box_items.size).to eq(15)
181
+ end
182
+ end
183
+ end
184
+
97
185
  def expand_select_box
98
186
  find('.select2-container').click
99
187
  end
@@ -31,6 +31,26 @@ ActiveRecord::Schema.define do
31
31
  t.integer :color_id
32
32
  end
33
33
 
34
+ create_table(:option_types, force: true) do |t|
35
+ t.string :name
36
+ end
37
+
38
+ create_table(:option_values, force: true) do |t|
39
+ t.string :value
40
+ t.belongs_to :option_type
41
+ end
42
+
43
+ create_table(:products, force: true) do |t|
44
+ t.string :name
45
+ t.belongs_to :option_type
46
+ end
47
+
48
+ create_table(:variants, force: true) do |t|
49
+ t.integer :price
50
+ t.belongs_to :product
51
+ t.belongs_to :option_value
52
+ end
53
+
34
54
  create_table(:users, force: true) do |t|
35
55
  t.string :name
36
56
  end
@@ -1,9 +1,13 @@
1
1
  module ActiveAdminHelpers
2
2
  module_function
3
3
 
4
+ def reload_routes!
5
+ Rails.application.reload_routes!
6
+ end
7
+
4
8
  def setup
5
9
  ActiveAdmin.application = nil
6
10
  yield
7
- Rails.application.reload_routes!
11
+ reload_routes!
8
12
  end
9
13
  end
@@ -27,6 +27,22 @@ module Internal
27
27
  end
28
28
  end
29
29
 
30
+ class OptionType < ActiveRecord::Base; end
31
+
32
+ class OptionValue < ActiveRecord::Base
33
+ belongs_to :option_type
34
+ end
35
+
36
+ class Product < ActiveRecord::Base
37
+ belongs_to :option_type
38
+ has_many :variants
39
+ end
40
+
41
+ class Variant < ActiveRecord::Base
42
+ belongs_to :product
43
+ belongs_to :option_value
44
+ end
45
+
30
46
  RSpec.configure do |config|
31
47
  config.after do
32
48
  DatabaseCleaner.strategy = :truncation
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin-searchable_select
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Codevise Solutions Ltd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-16 00:00:00.000000000 Z
11
+ date: 2022-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler