Skip to content

Commit

Permalink
Merge pull request #196 from rubenbe/master
Browse files Browse the repository at this point in the history
fix nested select when used inside a nested form
  • Loading branch information
jgmontoya committed Feb 28, 2020
2 parents 4e26cc6 + 492082f commit 691cc48
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 1 deletion.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AllCops:
Exclude:
- "vendor/**/*"
- "spec/dummy/db/*"
- "db/**/*"
- "bin/**/*"
DisplayCopNames: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ var initializer = function() {

if (!!parent) {
var parentSelectorId = '#' + model + '_' + parent;
if (!$(parentSelectorId).length) {
parentSelectorId = $(container).find('*[id*=' + parent + ']')[0];
}
var parentSelector = $(parentSelectorId)[0];

$(parentSelector).on('select2:select', setParentValue);
Expand Down
18 changes: 18 additions & 0 deletions spec/dummy/app/admin/departments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ActiveAdmin.register Department do
permit_params :name, departments_cities_attributes: [:id, :city_id, :_destroy]

form do |f|
f.inputs do
f.input :name
end
f.inputs do
f.has_many :departments_cities, allow_destroy: true do |city|
city.input :city_id, as: :nested_select, required: true,
level_1: { attribute: :country_id },
level_2: { attribute: :region_id },
level_3: { attribute: :city_id }
end
end
f.actions
end
end
4 changes: 4 additions & 0 deletions spec/dummy/app/models/department.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Department < ActiveRecord::Base
has_many :departments_cities
accepts_nested_attributes_for :departments_cities, allow_destroy: true
end
4 changes: 4 additions & 0 deletions spec/dummy/app/models/departments_city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class DepartmentsCity < ActiveRecord::Base
has_many :departments
has_many :cities
end
9 changes: 9 additions & 0 deletions spec/dummy/db/migrate/20180228085959_create_departments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateDepartments < ActiveRecord::Migration
def change
create_table :departments do |t|
t.string :name

t.timestamps null: false
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateDepartmentsCities < ActiveRecord::Migration
def change
create_table :departments_cities do |t|
t.references :department, index: true, foreign_key: true
t.references :city, index: true, foreign_key: true
end
end
end
16 changes: 15 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180222225709) do
ActiveRecord::Schema.define(version: 20180228122115) do

create_table "active_admin_comments", force: :cascade do |t|
t.string "namespace"
Expand Down Expand Up @@ -76,6 +76,20 @@
t.text "information"
end

create_table "departments", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "departments_cities", force: :cascade do |t|
t.integer "department_id"
t.integer "city_id"
end

add_index "departments_cities", ["city_id"], name: "index_departments_cities_on_city_id"
add_index "departments_cities", ["department_id"], name: "index_departments_cities_on_department_id"

create_table "invoices", force: :cascade do |t|
t.datetime "legal_date"
t.string "number"
Expand Down
64 changes: 64 additions & 0 deletions spec/features/inputs/nested_select_input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,68 @@
end
end
end

context "with nested resources" do
before do
register_page(Region, false) {}
register_page(Country, false) {}
register_page(City, false) {}

register_form(Department, false) do |f|
f.has_many :departments_cities, allow_destroy: true do |city|
city.input :city_id, as: :nested_select, required: true,
display_name: :id,
minimum_input_length: 0,
level_1: { attribute: :country_id },
level_2: { attribute: :region_id },
level_3: { attribute: :city_id }
end
end
create_cities
visit new_admin_department_path
end

it "sets new values correctly", js: true do
click_add_nested
prefix = "department_departments_cities_attributes_"
santiago = @santiago.id
on_nested_ctx(1) do
on_input_ctx("#{prefix}0_country_id") do
open_select2_options
click_select2_option(@chile.id)
end
on_input_ctx("#{prefix}0_region_id") do
open_select2_options
click_select2_option(@metropolitana.id)
end
on_input_ctx("#{prefix}0_city_id") do
open_select2_options
click_select2_option(santiago)
end
expect_nested_select2_result_text_to_eq(3, santiago)
end

click_add_nested
on_nested_ctx(2) do
mendoza = @mendoza.id
on_input_ctx("#{prefix}1_country_id") do
open_select2_options
click_select2_option(@argentina.id)
end
on_input_ctx("#{prefix}1_region_id") do
open_select2_options
click_select2_option(@cuyo.id)
end
on_input_ctx("#{prefix}1_city_id") do
open_select2_options
click_select2_option(mendoza)
end
expect_nested_select2_result_text_to_eq(3, mendoza)
end

on_nested_ctx(1) do
expect_nested_select2_result_text_to_eq(3, santiago)
end
end
end
end
16 changes: 16 additions & 0 deletions spec/support/capybara_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,20 @@ def expect_select2_results_count_to_eq(count)
expect(page).not_to have_content(no_results)
end
end

def click_add_nested
find("a.has_many_add").click
end

def on_nested_ctx(resource_number, &block)
within("li.has_many_container fieldset:nth-child(#{resource_number + 1}) ") do
block.call
end
end

def expect_nested_select2_result_text_to_eq(result_number, text)
expect(page).to have_css(
"li.nested_level:nth-child(#{result_number})", text: /#{text}/
)
end
end

0 comments on commit 691cc48

Please sign in to comment.