Skip to content

Commit dff7a08

Browse files
committed
Make :path_params nested inside of :ajax
Removed ActiveAdmin.unload! to fix specs in older versions
1 parent e0f0804 commit dff7a08

File tree

4 files changed

+66
-54
lines changed

4 files changed

+66
-54
lines changed

README.md

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -242,50 +242,7 @@ argument:
242242
end
243243
```
244244

245-
#### Inlining Ajax Options in Feature Tests
246-
247-
When writing UI driven feature specs (i.e. with Capybara),
248-
asynchronous loading of select options can increase test
249-
complexity. `activeadmin-searchable_select` provides an option to
250-
render all available options just like a normal select input while
251-
still exercsing the same code paths including `scope` and
252-
`text_attribute` handling.
253-
254-
For example with RSpec/Capybara, simply set `inline_ajax_options` true
255-
for feature specs:
256-
257-
```ruby
258-
RSpec.configure do |config|
259-
config.before(:each) do |example|
260-
ActiveAdmin::SearchableSelect.inline_ajax_options = (example.metadata[:type] == :feature)
261-
end
262-
end
263-
264-
```
265-
266-
### Passing options to Select2
267-
268-
It is possible to pass and define configuration options to Select2
269-
via `data-attributes` using nested (subkey) options.
270-
271-
Attributes need to be added to the `input_html` option in the form input.
272-
For example you can tell Select2 how long to wait after a user
273-
has stopped typing before sending the request:
274-
275-
```ruby
276-
...
277-
f.input(:category,
278-
as: :searchable_select,
279-
ajax: true,
280-
input_html: {
281-
data: {
282-
'ajax--delay' => 500
283-
}
284-
})
285-
...
286-
```
287-
288-
### Path options for nested resources
245+
#### Path options for nested resources
289246

290247
Example for the following setup:
291248

@@ -333,17 +290,62 @@ ActiveAdmin.register(Variant) do
333290
...
334291
f.input(:option_value,
335292
as: :searchable_select,
336-
path_params: {
337-
option_type_id: f.object.product.option_type_id
338-
},
339-
ajax: { resource: OptionValue })
293+
ajax: {
294+
resource: OptionValue,
295+
path_params: {
296+
option_type_id: f.object.product.option_type_id
297+
}
298+
})
340299
...
341300
end
342301
end
343302
```
344303

345304
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`)
346305

306+
#### Inlining Ajax Options in Feature Tests
307+
308+
When writing UI driven feature specs (i.e. with Capybara),
309+
asynchronous loading of select options can increase test
310+
complexity. `activeadmin-searchable_select` provides an option to
311+
render all available options just like a normal select input while
312+
still exercsing the same code paths including `scope` and
313+
`text_attribute` handling.
314+
315+
For example with RSpec/Capybara, simply set `inline_ajax_options` true
316+
for feature specs:
317+
318+
```ruby
319+
RSpec.configure do |config|
320+
config.before(:each) do |example|
321+
ActiveAdmin::SearchableSelect.inline_ajax_options = (example.metadata[:type] == :feature)
322+
end
323+
end
324+
325+
```
326+
327+
### Passing options to Select2
328+
329+
It is possible to pass and define configuration options to Select2
330+
via `data-attributes` using nested (subkey) options.
331+
332+
Attributes need to be added to the `input_html` option in the form input.
333+
For example you can tell Select2 how long to wait after a user
334+
has stopped typing before sending the request:
335+
336+
```ruby
337+
...
338+
f.input(:category,
339+
as: :searchable_select,
340+
ajax: true,
341+
input_html: {
342+
data: {
343+
'ajax--delay' => 500
344+
}
345+
})
346+
...
347+
```
348+
347349

348350
## Development
349351

lib/activeadmin/searchable_select/select_input_extension.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ module SearchableSelect
2020
# - `params`: Hash of query parameters that shall be passed to the
2121
# options endpoint.
2222
#
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+
#
2327
# If the `ajax` option is present, the `collection` option is
2428
# ignored.
2529
module SelectInputExtension
@@ -125,7 +129,7 @@ def ajax_params
125129
end
126130

127131
def path_params
128-
options[:path_params]
132+
ajax_options.fetch(:path_params, {})
129133
end
130134

131135
def ajax_options

spec/features/end_to_end_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
end
9696

9797
context 'class with nested belongs_to association' do
98-
before(:each) do
98+
before(:all) do
9999
ActiveAdminHelpers.setup do
100100
ActiveAdmin.register(OptionType)
101101

@@ -118,9 +118,11 @@
118118
input :price
119119
input(:option_value,
120120
as: :searchable_select,
121-
path_params: { option_type_id: f.object.product.option_type_id },
122121
ajax: {
123-
resource: OptionValue
122+
resource: OptionValue,
123+
path_params: {
124+
option_type_id: f.object.product.option_type_id
125+
}
124126
})
125127
end
126128
end

spec/support/active_admin_helpers.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
module ActiveAdminHelpers
22
module_function
33

4+
def reload_routes!
5+
Rails.application.reload_routes!
6+
end
7+
48
def setup
5-
ActiveAdmin.unload!
9+
ActiveAdmin.application = nil
610
yield
7-
Rails.application.reload_routes!
11+
reload_routes!
812
end
913
end

0 commit comments

Comments
 (0)