activeadmin-searchable_select 1.5.0 → 1.7.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: afca3ffb2bd7868c51afc6e6793fc4b3080a1fdb0f92a248ccdbe264faa58597
4
+ data.tar.gz: defeafa115233119aacc2bde15424810bdd1fd05187c2c48536879586b96b00b
5
5
  SHA512:
6
- metadata.gz: a0448e24dbb3c48ca7421eca976836d5be764e91db9e89a129e8c9b47dbb5dbbbdb94ac8937d049b5af35d6c68a17933bc6e3712719fd920b125e65d29147b88
7
- data.tar.gz: 4c90fe918890f40de37d138ba3b82b12307bf5331772a0dd8a139afb1698a8cf3a95620a72372415437b103851ee4ae574807d11a888cd8b850a6d360a2af6e9
6
+ metadata.gz: 527c21ae72869785419007a81436186684d589e87abc4aeb64213a8fe46af2ca1701e98296f8d2a9c64c756a9331a625aac166994591a57d1c43c70cf277ccb0
7
+ data.tar.gz: 71f29f997879afec190f430697d57362dba21802e322b8045d20b247f3c1ef0df8ed8b88b0a84a4e43910dda1ef9221050e8843fac45133b3c9fd15defe028be
@@ -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,17 @@
1
1
  # CHANGELOG
2
2
 
3
- ### Version 1.5.0
3
+ ### Version 1.7.0
4
4
 
5
- 2021-02-16
5
+ 2023-05-03
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-6-stable...v1.7.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
+ - Fix pre-selected item when using belongs_to
10
+ ([#43](https://github.com/codevise/activeadmin-searchable_select/pull/43),
11
+ [#44](https://github.com/codevise/activeadmin-searchable_select/pull/44))
12
+ - Improvement the examples in Readme for import npm assets
13
+ ([#41](https://github.com/codevise/activeadmin-searchable_select/pull/41))
11
14
 
12
15
  See
13
- [1-4-stable branch](https://github.com/codevise/activeadmin-searchable_select/blob/1-4-stable/CHANGELOG.md)
16
+ [1-6-stable branch](https://github.com/codevise/activeadmin-searchable_select/blob/1-6-stable/CHANGELOG.md)
14
17
  for previous changes.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # ActiveAdmin Searchable Select
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/activeadmin-searchable_select.svg)](http://badge.fury.io/rb/activeadmin-searchable_select)
4
+ [![NPM Version](https://badge.fury.io/js/@codevise%2Factiveadmin-searchable_select.svg)](https://badge.fury.io/js/@codevise%2Factiveadmin-searchable_select)
5
+ [![npm](https://img.shields.io/npm/dm/@codevise/activeadmin-searchable_select)](https://www.npmjs.com/package/@codevise/activeadmin-searchable_select)
4
6
  [![Build Status](https://github.com/codevise/activeadmin-searchable_select.svg?branch=master)](https://github.com/codevise/activeadmin-searchable_select/actions)
5
7
 
6
8
  Searchable select boxes (via [Select2](https://select2.org/)) for
@@ -15,6 +17,7 @@ Add `activeadmin-searchable_select` to your Gemfile:
15
17
  gem 'activeadmin-searchable_select'
16
18
  ```
17
19
 
20
+ ##### Using assets via Sprockets
18
21
  Import stylesheets and require javascripts:
19
22
 
20
23
  ```scss
@@ -27,6 +30,39 @@ Import stylesheets and require javascripts:
27
30
  //= require active_admin/searchable_select
28
31
  ```
29
32
 
33
+ ##### Using assets via Webpacker (or any other assets bundler) as a NPM module (Yarn package)
34
+
35
+ Execute:
36
+
37
+ $ npm i @codevise/activeadmin-searchable_select
38
+
39
+ Or
40
+
41
+ $ yarn add @codevise/activeadmin-searchable_select
42
+
43
+ Or add manually to `package.json`:
44
+
45
+ ```json
46
+ "dependencies": {
47
+ "@codevise/activeadmin-searchable_select": "1.6.0"
48
+ }
49
+ ```
50
+ and execute:
51
+
52
+ $ yarn
53
+
54
+ Add the following line into `app/javascript/active_admin.js`:
55
+
56
+ ```javascript
57
+ import '@codevise/activeadmin-searchable_select';
58
+ ```
59
+
60
+ Add the following line into `app/javascript/stylesheets/active_admin.scss`:
61
+
62
+ ```css
63
+ @import '@codevise/activeadmin-searchable_select';
64
+ ```
65
+
30
66
  ## Usage
31
67
 
32
68
  ### Making Select Boxes Searchable
@@ -242,6 +278,67 @@ argument:
242
278
  end
243
279
  ```
244
280
 
281
+ #### Path options for nested resources
282
+
283
+ Example for the following setup:
284
+
285
+ ```ruby
286
+ # Models
287
+ class OptionType < ActiveRecord::Base; end
288
+
289
+ class OptionValue < ActiveRecord::Base
290
+ belongs_to :option_type
291
+ end
292
+
293
+ class Product < ActiveRecord::Base
294
+ belongs_to :option_type
295
+ has_many :variants
296
+ end
297
+
298
+ class Variant < ActiveRecord::Base
299
+ belongs_to :product
300
+ belongs_to :option_value
301
+ end
302
+
303
+ # ActiveAdmin
304
+ ActiveAdmin.register(OptionType)
305
+
306
+ ActiveAdmin.register(Product)
307
+
308
+ ActiveAdmin.register(OptionValue) do
309
+ belongs_to :option_type
310
+ searchable_select_options(scope: lambda do |params|
311
+ OptionValue.where(
312
+ option_type_id: params[:option_type_id]
313
+ )
314
+ end,
315
+ text_attribute: :value)
316
+ end
317
+ ```
318
+
319
+ It is possible to pass path parameters for correctly generating URLs for nested resources fetching via `path_params`
320
+
321
+ ```ruby
322
+ ActiveAdmin.register(Variant) do
323
+ belongs_to :product
324
+
325
+ form do |f|
326
+ ...
327
+ f.input(:option_value,
328
+ as: :searchable_select,
329
+ ajax: {
330
+ resource: OptionValue,
331
+ path_params: {
332
+ option_type_id: f.object.product.option_type_id
333
+ }
334
+ })
335
+ ...
336
+ end
337
+ end
338
+ ```
339
+
340
+ 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`)
341
+
245
342
  #### Inlining Ajax Options in Feature Tests
246
343
 
247
344
  When writing UI driven feature specs (i.e. with Capybara),
@@ -265,7 +362,7 @@ for feature specs:
265
362
 
266
363
  ### Passing options to Select2
267
364
 
268
- It is possible to pass and define configuration options to Select2
365
+ It is possible to pass and define configuration options to Select2
269
366
  via `data-attributes` using nested (subkey) options.
270
367
 
271
368
  Attributes need to be added to the `input_html` option in the form input.
@@ -0,0 +1,2 @@
1
+ import 'select2';
2
+ import './searchable_select/init';
@@ -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
  '?',
@@ -80,7 +84,7 @@ module ActiveAdmin
80
84
  end
81
85
 
82
86
  def option_collection_scope
83
- option_collection.scope(template, ajax_params)
87
+ option_collection.scope(template, path_params.merge(ajax_params))
84
88
  end
85
89
 
86
90
  def option_collection
@@ -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.7.0'.freeze
4
4
  end
5
5
  end
data/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@codevise/activeadmin-searchable_select",
3
+ "version": "1.7.0",
4
+ "description": "Use searchable selects based on Select2 in Active Admin forms and filters.",
5
+ "main": "src/searchable_select.js",
6
+ "style": "src/searchable_select.scss",
7
+ "repository": "git@github.com:codevise/activeadmin-searchable_select.git",
8
+ "author": "Codevise Solutions Ltd <info@codevise.de>",
9
+ "license": "MIT",
10
+ "private": false,
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/codevise/activeadmin-searchable_select.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/codevise/activeadmin-searchable_select/issues"
17
+ },
18
+ "homepage": "https://github.com/codevise/activeadmin-searchable_select#readme",
19
+ "keywords": [
20
+ "select2",
21
+ "active",
22
+ "admin",
23
+ "searchable",
24
+ "select"
25
+ ],
26
+ "dependencies": {
27
+ "jquery": ">= 3.0, < 5",
28
+ "select2": "~> 4.0"
29
+ },
30
+ "files": [
31
+ "src/**/*"
32
+ ],
33
+ "scripts": {
34
+ "prepare_javascripts_src": "rm -rf src && cp -R app/assets/javascripts/active_admin/ src && mv src/searchable_select_pack.js src/searchable_select.js",
35
+ "prepare_stylesheets_src": "cp -R app/assets/stylesheets/active_admin/* src",
36
+ "prepublishOnly": "npm run prepare_javascripts_src && npm run prepare_stylesheets_src"
37
+ }
38
+ }
@@ -94,6 +94,110 @@ 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
+
184
+ describe 'edit page with searchable select filter' do
185
+ it 'preselects item' do
186
+ option_type = OptionType.create(name: 'Color')
187
+ ot = OptionType.create(name: 'Size')
188
+ option_value = OptionValue.create(value: 'Black', option_type: option_type)
189
+ OptionValue.create(value: 'Orange', option_type: option_type)
190
+ OptionValue.create(value: 'M', option_type: ot)
191
+ product = Product.create(name: 'Cap', option_type: option_type)
192
+ variant = Variant.create(product: product, option_value: option_value)
193
+
194
+ visit "/admin/products/#{product.id}/variants/#{variant.id}/edit"
195
+
196
+ expect(select_box_selected_item_text).to eq('Black')
197
+ end
198
+ end
199
+ end
200
+
97
201
  def expand_select_box
98
202
  find('.select2-container').click
99
203
  end
@@ -110,6 +214,10 @@ RSpec.describe 'end to end', type: :feature, js: true do
110
214
  all('.select2-dropdown li').map(&:text)
111
215
  end
112
216
 
217
+ def select_box_selected_item_text
218
+ find('.select2-selection').text
219
+ end
220
+
113
221
  def wait_for_ajax
114
222
  Timeout.timeout(Capybara.default_max_wait_time) do
115
223
  sleep 0.1
@@ -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.7.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: 2023-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -314,6 +314,7 @@ files:
314
314
  - activeadmin-searchable_select.gemspec
315
315
  - app/assets/javascripts/active_admin/searchable_select.js
316
316
  - app/assets/javascripts/active_admin/searchable_select/init.js
317
+ - app/assets/javascripts/active_admin/searchable_select_pack.js
317
318
  - app/assets/stylesheets/active_admin/searchable_select.scss
318
319
  - bin/rspec
319
320
  - gemfiles/rails_5.1_active_admin_1.x.gemfile
@@ -330,6 +331,7 @@ files:
330
331
  - lib/activeadmin/searchable_select/resource_extension.rb
331
332
  - lib/activeadmin/searchable_select/select_input_extension.rb
332
333
  - lib/activeadmin/searchable_select/version.rb
334
+ - package.json
333
335
  - spec/features/ajax_params_spec.rb
334
336
  - spec/features/end_to_end_spec.rb
335
337
  - spec/features/filter_input_spec.rb
@@ -375,7 +377,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
375
377
  - !ruby/object:Gem::Version
376
378
  version: '0'
377
379
  requirements: []
378
- rubygems_version: 3.0.8
380
+ rubygems_version: 3.2.3
379
381
  signing_key:
380
382
  specification_version: 4
381
383
  summary: Use searchable selects based on Select2 in Active Admin forms and filters.