Skip to content

Commit 833273f

Browse files
authored
Merge pull request codevise#43 from moka-care/fix-path-params
Fix pre-selected item when using belongs_to
2 parents 00d3147 + 8eed567 commit 833273f

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/activeadmin/searchable_select/select_input_extension.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def selected_values
8484
end
8585

8686
def option_collection_scope
87-
option_collection.scope(template, ajax_params)
87+
option_collection.scope(template, path_params.merge(ajax_params))
8888
end
8989

9090
def option_collection

spec/features/ajax_params_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,52 @@
5050
expect(response.body).to have_selector('.searchable-select-input' \
5151
"[data-ajax-url*='#{url_matcher}']")
5252
end
53+
54+
context 'when using belongs_to' do
55+
before(:each) do
56+
ActiveAdminHelpers.setup do
57+
58+
ActiveAdmin.register(OptionType)
59+
60+
ActiveAdmin.register(Product) do
61+
62+
ActiveAdmin.register(OptionValue) do
63+
belongs_to :option_type
64+
searchable_select_options(scope: lambda do |params|
65+
OptionValue.where(
66+
option_type_id: params[:option_type_id]
67+
)
68+
end,
69+
text_attribute: :value)
70+
end
71+
72+
ActiveAdmin.register(Variant) do
73+
belongs_to :product
74+
75+
form do |f|
76+
f.input(:option_value,
77+
as: :searchable_select,
78+
ajax: {
79+
resource: OptionValue,
80+
path_params: {
81+
option_type_id: f.object.product.option_type_id
82+
}
83+
})
84+
end
85+
end
86+
end
87+
end
88+
89+
it 'pre-select items in the associations' do
90+
option_type = OptionType.create
91+
product = Product.create(option_type: option_type)
92+
option_value = OptionValue.create(option_type: option_type, value: 'Red')
93+
variant = Variant.create(product: product, option_value: option_value)
94+
95+
get "/admin/products/#{product.id}/variants/#{variant.id}/edit"
96+
97+
expect(response.body).to have_selector('.searchable-select-input option[selected]',
98+
count: 1)
99+
end
100+
end
53101
end

0 commit comments

Comments
 (0)