Skip to content

Commit 18a9ae3

Browse files
authored
Merge branch 'codevise:master' into master
2 parents b8f43de + c359ee4 commit 18a9ae3

25 files changed

+685
-93
lines changed

.github/workflows/tests.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: tests
2+
on:
3+
push:
4+
pull_request:
5+
schedule:
6+
- cron: '45 2 * * *'
7+
8+
jobs:
9+
rspec:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
ruby-version:
16+
- '2.6'
17+
- '2.7'
18+
gemfile:
19+
- gemfiles/rails_5.1_active_admin_1.x.gemfile
20+
- gemfiles/rails_5.x_active_admin_1.x.gemfile
21+
- gemfiles/rails_5.x_active_admin_2.x.gemfile
22+
- gemfiles/rails_6.x_active_admin_2.x.gemfile
23+
include:
24+
- ruby-version: '3.0'
25+
gemfile: gemfiles/rails_6.x_active_admin_2.x.gemfile
26+
- ruby-version: '3.0'
27+
gemfile: gemfiles/rails_6.x_active_admin_3.x.gemfile
28+
- ruby-version: '3.0'
29+
gemfile: gemfiles/rails_7.x_active_admin_3.x.gemfile
30+
31+
env:
32+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
33+
34+
steps:
35+
- uses: actions/checkout@v2
36+
- name: Set up Ruby
37+
uses: ruby/setup-ruby@v1
38+
with:
39+
ruby-version: ${{ matrix.ruby-version }}
40+
bundler-cache: true
41+
- name: Run tests
42+
run: bundle exec rspec

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ Gemfile.lock
66
/pkg/*
77
/spec/internal/tmp
88
/spec/internal/db/*.sqlite
9-
/spec/examples.txt
9+
/spec/internal/db/*.sqlite-shm
10+
/spec/internal/db/*.sqlite-wal
11+
/spec/examples.txt
12+
/src
13+
/node_modules

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
AllCops:
22
TargetRubyVersion: 2.3
3+
Exclude:
4+
- 'bin/**/*'
35

46
# The default of 80 characters is a little to narrow.
57
Metrics/LineLength:

.travis.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

Appraisals

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
appraise 'rails-4.2-active-admin-1.0.0.pre4' do
2-
gem 'rails', '~> 4.2'
3-
gem 'activeadmin', '1.0.0.pre4'
4-
gem 'jquery-ui-rails', '~> 5.0'
5-
gem 'sqlite3', '~> 1.3.6'
6-
end
7-
81
appraise 'rails-5.1-active-admin-1.x' do
92
gem 'rails', '~> 5.1.0'
103
gem 'activeadmin', '~> 1.0'
@@ -18,4 +11,19 @@ end
1811
appraise 'rails-5.x-active-admin-2.x' do
1912
gem 'rails', '~> 5.2'
2013
gem 'activeadmin', '~> 2.0'
21-
end
14+
end
15+
16+
appraise 'rails-6.x-active-admin-2.x' do
17+
gem 'rails', '~> 6.1'
18+
gem 'activeadmin', '~> 2.0'
19+
end
20+
21+
appraise 'rails-6.x-active-admin-3.x' do
22+
gem 'rails', '~> 6.1'
23+
gem 'activeadmin', '~> 3.0'
24+
end
25+
26+
appraise 'rails-7.x-active-admin-3.x' do
27+
gem 'rails', '~> 7.1'
28+
gem 'activeadmin', '~> 3.0'
29+
end

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
### Unreleased Changes
44

5-
[Compare changes](https://github.com/codevise/activeadmin-searchable_select/compare/1-2-stable...master)
5+
[Compare changes](https://github.com/codevise/activeadmin-searchable_select/compare/1-8-stable...master)
66

77
None so far.
88

99
See
10-
[1-2-stable branch](https://github.com/codevise/activeadmin-searchable_select/blob/1-2-stable/CHANGELOG.md)
10+
[1-8-stable branch](https://github.com/codevise/activeadmin-searchable_select/blob/1-8-stable/CHANGELOG.md)
1111
for previous changes.

README.md

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# ActiveAdmin Searchable Select
22

33
[![Gem Version](https://badge.fury.io/rb/activeadmin-searchable_select.svg)](http://badge.fury.io/rb/activeadmin-searchable_select)
4-
[![Build Status](https://travis-ci.org/codevise/activeadmin-searchable_select.svg?branch=master)](https://travis-ci.org/codevise/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)
6+
[![Build Status](https://github.com/codevise/activeadmin-searchable_select/actions/workflows/tests.yml/badge.svg)](https://github.com/codevise/activeadmin-searchable_select/actions)
57

68
Searchable select boxes (via [Select2](https://select2.org/)) for
79
ActiveAdmin forms and filters. Extends the ActiveAdmin resource DSL to
@@ -12,9 +14,10 @@ allow defining JSON endpoints to fetch options from asynchronously.
1214
Add `activeadmin-searchable_select` to your Gemfile:
1315

1416
```ruby
15-
gem 'activeadmin-searchable_select
17+
gem 'activeadmin-searchable_select'
1618
```
1719

20+
##### Using assets via Sprockets
1821
Import stylesheets and require javascripts:
1922

2023
```scss
@@ -27,6 +30,39 @@ Import stylesheets and require javascripts:
2730
//= require active_admin/searchable_select
2831
```
2932

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+
3066
## Usage
3167

3268
### Making Select Boxes Searchable
@@ -193,6 +229,25 @@ build your query in a way that it can query multiple attributes at once.
193229

194230
In this example, the `all` scope will query `email OR username`.
195231

232+
You can add the additional payload as dsl option:
233+
234+
```ruby
235+
ActiveAdmin.register Category do
236+
searchable_select_options(scope: Category.all,
237+
text_attribute: :name,
238+
additional_payload: ->(record) { { foo: record.bar } } )
239+
end
240+
```
241+
242+
response example which uses additional_payload:
243+
244+
```json
245+
{
246+
"results": [{ "id": "1", "text": "Bicycles", "foo": "Bar" }],
247+
"pagination": { "more": "false" }
248+
}
249+
```
250+
196251
#### Passing Parameters
197252

198253
You can pass additional parameters to the options endpoint:
@@ -223,6 +278,67 @@ argument:
223278
end
224279
```
225280

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+
226342
#### Inlining Ajax Options in Feature Tests
227343

228344
When writing UI driven feature specs (i.e. with Capybara),
@@ -238,12 +354,35 @@ for feature specs:
238354
```ruby
239355
RSpec.configure do |config|
240356
config.before(:each) do |example|
241-
ActiveAdmin::Select2.inline_ajax_options = (example.metadata[:type] == :feature)
357+
ActiveAdmin::SearchableSelect.inline_ajax_options = (example.metadata[:type] == :feature)
242358
end
243359
end
244360

245361
```
246362

363+
### Passing options to Select2
364+
365+
It is possible to pass and define configuration options to Select2
366+
via `data-attributes` using nested (subkey) options.
367+
368+
Attributes need to be added to the `input_html` option in the form input.
369+
For example you can tell Select2 how long to wait after a user
370+
has stopped typing before sending the request:
371+
372+
```ruby
373+
...
374+
f.input(:category,
375+
as: :searchable_select,
376+
ajax: true,
377+
input_html: {
378+
data: {
379+
'ajax--delay' => 500
380+
}
381+
})
382+
...
383+
```
384+
385+
247386
## Development
248387

249388
To run the tests install bundled gems and invoke RSpec:

activeadmin-searchable_select.gemspec

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,28 @@ Gem::Specification.new do |spec|
1414
spec.files = `git ls-files -z`.split("\x0")
1515
spec.require_paths = ['lib']
1616

17-
spec.required_ruby_version = '~> 2.1'
17+
spec.required_ruby_version = ['>= 2.1', '< 4']
1818

19-
spec.add_development_dependency 'bundler', '~> 1.5'
19+
spec.add_development_dependency 'bundler', ['>= 1.5', '< 3']
2020
spec.add_development_dependency 'rake'
2121
spec.add_development_dependency 'appraisal', '~> 2.2'
2222
spec.add_development_dependency 'rspec-rails', '~> 3.6'
2323
spec.add_development_dependency 'combustion', '~> 1.0'
24-
spec.add_development_dependency 'database_cleaner', '~> 1.6'
24+
spec.add_development_dependency 'database_cleaner-active_record', '~> 2.1'
2525
spec.add_development_dependency 'sqlite3', '~> 1.3'
26-
spec.add_development_dependency 'capybara', '~> 2.15'
27-
spec.add_development_dependency 'poltergeist', '~> 1.15'
26+
27+
spec.add_development_dependency 'capybara', '~> 3.9'
28+
spec.add_development_dependency 'puma', '~> 5.0'
29+
spec.add_development_dependency 'selenium-webdriver', '~> 4.1'
30+
spec.add_development_dependency 'webdrivers', '= 5.3.0'
31+
32+
spec.add_development_dependency 'coffee-rails'
33+
spec.add_development_dependency 'rails'
2834
spec.add_development_dependency 'rubocop', '~> 0.42.0'
2935
spec.add_development_dependency 'semmy', '~> 1.0'
30-
spec.add_development_dependency 'rails'
36+
spec.add_development_dependency 'sprockets', '~> 3.7'
3137

32-
spec.add_runtime_dependency 'activeadmin', ['>= 1.x', '< 3']
38+
spec.add_runtime_dependency 'activeadmin', ['>= 1.x', '< 4']
3339
spec.add_runtime_dependency 'jquery-rails', ['>= 3.0', '< 5']
3440
spec.add_runtime_dependency 'select2-rails', '~> 4.0'
3541
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import 'select2';
2+
import './searchable_select/init';

gemfiles/rails_4.2_active_admin_1.0.0.pre4.gemfile

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)