Skip to content

Commit f629a91

Browse files
authored
Merge pull request #463 from platanus/fix-nested-with-collection-filtering
Fix nested with collection filtering
2 parents f42a919 + 3b3d031 commit f629a91

File tree

5 files changed

+47
-21
lines changed

5 files changed

+47
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
88
* Defines required ruby version to >=2.7.0 [#460](https://github.com/platanus/activeadmin_addons/pull/460)
99
* Nested and search select now use the name of the association instead of the name of id [#462](https://github.com/platanus/activeadmin_addons/pull/462)
1010

11+
#### Fixes
12+
* Include only items that belong to parent when using collection option in a nested input level [#463](https://github.com/platanus/activeadmin_addons/pull/463)
13+
1114
### 2.0.0.beta-0
1215

1316
#### Breaking changes

app/inputs/nested_level_input.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def load_collection_data
4848

4949
collection_options = collection_to_select_options do |item, option|
5050
if @options[:parent_attribute].present?
51-
option[@options[:parent_id_attribute]] = item.send(@options[:parent_id_attribute])
51+
option[@options[:parent_attribute]] = item.send(@options[:parent_id_attribute])
5252
end
5353
end
5454

app/javascript/activeadmin_addons/inputs/slim-select-nested.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ function collectionSearch(search, args, collection) {
2525
return Promise.reject('Search term too short');
2626
}
2727

28-
const data = JSON.parse(collection).map(
28+
const data = JSON.parse(collection).filter(
29+
(item) => (!args.parent || item[args.parent] === Number(args.parentId)) &&
30+
String(item.text).toLowerCase().includes(search.toLowerCase()),
31+
).map(
2932
(item) => ({ value: String(item.id), text: item.text }),
30-
).filter(
31-
(item) => String(item.text).toLowerCase().includes(search.toLowerCase()));
33+
);
3234

3335
return Promise.resolve(data);
3436
}

spec/features/inputs/nested_select_input_spec.rb

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,33 +99,48 @@
9999
end
100100
end
101101

102-
context "updating medium level" do
103-
before do
104-
on_input_ctx("invoice_region") do
105-
open_slimselect_options
106-
slimselect_search_input.set("Antof")
102+
context "when updating medium level (that uses collection option)" do
103+
context 'when searching for region that does not belong to selected country' do
104+
before do
105+
on_input_ctx("invoice_region") do
106+
open_slimselect_options
107+
slimselect_search_input.set("Cuy")
108+
end
107109
end
108-
end
109110

110-
it "shows results based on entered text", js: true do
111-
expect_slimselect_result_text_to_eq(1, "Antofagasta")
111+
it "does not show results", js: true do
112+
expect_slimselect_no_result
113+
end
112114
end
113115

114-
context "after click option" do
116+
context 'when searching for region that belongs to selected country' do
115117
before do
116-
on_input_ctx("invoice_region") { click_slimselect_option("Antofagasta") }
118+
on_input_ctx("invoice_region") do
119+
open_slimselect_options
120+
slimselect_search_input.set("Antof")
121+
end
117122
end
118123

119-
it "sets value", js: true do
120-
on_input_ctx("invoice_region") { expect_slimselect_selection("Antofagasta") }
124+
it "shows results based on entered text", js: true do
125+
expect_slimselect_result_text_to_eq(1, "Antofagasta")
121126
end
122127

123-
it "preserves parent value", js: true do
124-
on_input_ctx("invoice_country") { expect_slimselect_selection("Chile") }
125-
end
128+
context "when clicking option" do
129+
before do
130+
on_input_ctx("invoice_region") { click_slimselect_option("Antofagasta") }
131+
end
126132

127-
it "resets children values", js: true do
128-
on_input_ctx("invoice_city") { expect_slimselect_empty_selection }
133+
it "sets value", js: true do
134+
on_input_ctx("invoice_region") { expect_slimselect_selection("Antofagasta") }
135+
end
136+
137+
it "preserves parent value", js: true do
138+
on_input_ctx("invoice_country") { expect_slimselect_selection("Chile") }
139+
end
140+
141+
it "resets children values", js: true do
142+
on_input_ctx("invoice_city") { expect_slimselect_empty_selection }
143+
end
129144
end
130145
end
131146
end

spec/support/capybara_helpers.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ def expect_slimselect_result_text_to_eq(result_number, text)
9999
)
100100
end
101101

102+
def expect_slimselect_no_result
103+
expect(page).to have_css(
104+
"div.ss-search", text: 'No Result'
105+
)
106+
end
107+
102108
def expect_slimselect_error(text)
103109
expect(page).to have_css(".ss-error", text: text)
104110
end

0 commit comments

Comments
 (0)