Skip to content

Commit 691cc48

Browse files
authored
Merge pull request #196 from rubenbe/master
fix nested select when used inside a nested form
2 parents 4e26cc6 + 492082f commit 691cc48

File tree

10 files changed

+142
-1
lines changed

10 files changed

+142
-1
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
AllCops:
22
Exclude:
33
- "vendor/**/*"
4+
- "spec/dummy/db/*"
45
- "db/**/*"
56
- "bin/**/*"
67
DisplayCopNames: false

app/assets/javascripts/activeadmin_addons/inputs/nested-select.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ var initializer = function() {
151151

152152
if (!!parent) {
153153
var parentSelectorId = '#' + model + '_' + parent;
154+
if (!$(parentSelectorId).length) {
155+
parentSelectorId = $(container).find('*[id*=' + parent + ']')[0];
156+
}
154157
var parentSelector = $(parentSelectorId)[0];
155158

156159
$(parentSelector).on('select2:select', setParentValue);

spec/dummy/app/admin/departments.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
ActiveAdmin.register Department do
2+
permit_params :name, departments_cities_attributes: [:id, :city_id, :_destroy]
3+
4+
form do |f|
5+
f.inputs do
6+
f.input :name
7+
end
8+
f.inputs do
9+
f.has_many :departments_cities, allow_destroy: true do |city|
10+
city.input :city_id, as: :nested_select, required: true,
11+
level_1: { attribute: :country_id },
12+
level_2: { attribute: :region_id },
13+
level_3: { attribute: :city_id }
14+
end
15+
end
16+
f.actions
17+
end
18+
end

spec/dummy/app/models/department.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Department < ActiveRecord::Base
2+
has_many :departments_cities
3+
accepts_nested_attributes_for :departments_cities, allow_destroy: true
4+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class DepartmentsCity < ActiveRecord::Base
2+
has_many :departments
3+
has_many :cities
4+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreateDepartments < ActiveRecord::Migration
2+
def change
3+
create_table :departments do |t|
4+
t.string :name
5+
6+
t.timestamps null: false
7+
end
8+
end
9+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CreateDepartmentsCities < ActiveRecord::Migration
2+
def change
3+
create_table :departments_cities do |t|
4+
t.references :department, index: true, foreign_key: true
5+
t.references :city, index: true, foreign_key: true
6+
end
7+
end
8+
end

spec/dummy/db/schema.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20180222225709) do
14+
ActiveRecord::Schema.define(version: 20180228122115) do
1515

1616
create_table "active_admin_comments", force: :cascade do |t|
1717
t.string "namespace"
@@ -76,6 +76,20 @@
7676
t.text "information"
7777
end
7878

79+
create_table "departments", force: :cascade do |t|
80+
t.string "name"
81+
t.datetime "created_at", null: false
82+
t.datetime "updated_at", null: false
83+
end
84+
85+
create_table "departments_cities", force: :cascade do |t|
86+
t.integer "department_id"
87+
t.integer "city_id"
88+
end
89+
90+
add_index "departments_cities", ["city_id"], name: "index_departments_cities_on_city_id"
91+
add_index "departments_cities", ["department_id"], name: "index_departments_cities_on_department_id"
92+
7993
create_table "invoices", force: :cascade do |t|
8094
t.datetime "legal_date"
8195
t.string "number"

spec/features/inputs/nested_select_input_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,68 @@
185185
end
186186
end
187187
end
188+
189+
context "with nested resources" do
190+
before do
191+
register_page(Region, false) {}
192+
register_page(Country, false) {}
193+
register_page(City, false) {}
194+
195+
register_form(Department, false) do |f|
196+
f.has_many :departments_cities, allow_destroy: true do |city|
197+
city.input :city_id, as: :nested_select, required: true,
198+
display_name: :id,
199+
minimum_input_length: 0,
200+
level_1: { attribute: :country_id },
201+
level_2: { attribute: :region_id },
202+
level_3: { attribute: :city_id }
203+
end
204+
end
205+
create_cities
206+
visit new_admin_department_path
207+
end
208+
209+
it "sets new values correctly", js: true do
210+
click_add_nested
211+
prefix = "department_departments_cities_attributes_"
212+
santiago = @santiago.id
213+
on_nested_ctx(1) do
214+
on_input_ctx("#{prefix}0_country_id") do
215+
open_select2_options
216+
click_select2_option(@chile.id)
217+
end
218+
on_input_ctx("#{prefix}0_region_id") do
219+
open_select2_options
220+
click_select2_option(@metropolitana.id)
221+
end
222+
on_input_ctx("#{prefix}0_city_id") do
223+
open_select2_options
224+
click_select2_option(santiago)
225+
end
226+
expect_nested_select2_result_text_to_eq(3, santiago)
227+
end
228+
229+
click_add_nested
230+
on_nested_ctx(2) do
231+
mendoza = @mendoza.id
232+
on_input_ctx("#{prefix}1_country_id") do
233+
open_select2_options
234+
click_select2_option(@argentina.id)
235+
end
236+
on_input_ctx("#{prefix}1_region_id") do
237+
open_select2_options
238+
click_select2_option(@cuyo.id)
239+
end
240+
on_input_ctx("#{prefix}1_city_id") do
241+
open_select2_options
242+
click_select2_option(mendoza)
243+
end
244+
expect_nested_select2_result_text_to_eq(3, mendoza)
245+
end
246+
247+
on_nested_ctx(1) do
248+
expect_nested_select2_result_text_to_eq(3, santiago)
249+
end
250+
end
251+
end
188252
end

spec/support/capybara_helpers.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,20 @@ def expect_select2_results_count_to_eq(count)
9595
expect(page).not_to have_content(no_results)
9696
end
9797
end
98+
99+
def click_add_nested
100+
find("a.has_many_add").click
101+
end
102+
103+
def on_nested_ctx(resource_number, &block)
104+
within("li.has_many_container fieldset:nth-child(#{resource_number + 1}) ") do
105+
block.call
106+
end
107+
end
108+
109+
def expect_nested_select2_result_text_to_eq(result_number, text)
110+
expect(page).to have_css(
111+
"li.nested_level:nth-child(#{result_number})", text: /#{text}/
112+
)
113+
end
98114
end

0 commit comments

Comments
 (0)