diff --git a/lib/activeadmin/searchable_select/select_input_extension.rb b/lib/activeadmin/searchable_select/select_input_extension.rb index 2ecda82..6ec26d9 100644 --- a/lib/activeadmin/searchable_select/select_input_extension.rb +++ b/lib/activeadmin/searchable_select/select_input_extension.rb @@ -84,7 +84,7 @@ def selected_values end def option_collection_scope - option_collection.scope(template, ajax_params) + option_collection.scope(template, path_params.merge(ajax_params)) end def option_collection diff --git a/spec/features/ajax_params_spec.rb b/spec/features/ajax_params_spec.rb index cfa97c4..78ca8e5 100644 --- a/spec/features/ajax_params_spec.rb +++ b/spec/features/ajax_params_spec.rb @@ -50,4 +50,52 @@ expect(response.body).to have_selector('.searchable-select-input' \ "[data-ajax-url*='#{url_matcher}']") end + + context 'when using belongs_to' do + before(:each) do + ActiveAdminHelpers.setup do + + ActiveAdmin.register(OptionType) + + ActiveAdmin.register(Product) do + + ActiveAdmin.register(OptionValue) do + belongs_to :option_type + searchable_select_options(scope: lambda do |params| + OptionValue.where( + option_type_id: params[:option_type_id] + ) + end, + text_attribute: :value) + end + + ActiveAdmin.register(Variant) do + belongs_to :product + + form do |f| + f.input(:option_value, + as: :searchable_select, + ajax: { + resource: OptionValue, + path_params: { + option_type_id: f.object.product.option_type_id + } + }) + end + end + end + end + + it 'pre-select items in the associations' do + option_type = OptionType.create + product = Product.create(option_type: option_type) + option_value = OptionValue.create(option_type: option_type, value: 'Red') + variant = Variant.create(product: product, option_value: option_value) + + get "/admin/products/#{product.id}/variants/#{variant.id}/edit" + + expect(response.body).to have_selector('.searchable-select-input option[selected]', + count: 1) + end + end end