diff --git a/lib/active_admin/form_builder.rb b/lib/active_admin/form_builder.rb index 009e9d8e110..48c4676073b 100644 --- a/lib/active_admin/form_builder.rb +++ b/lib/active_admin/form_builder.rb @@ -95,7 +95,7 @@ def content_has_many(&block) contents = without_wrapper { inputs(options, &form_block) } contents ||= "".html_safe - js = new_record ? js_for_has_many(options[:class], &form_block) : "" + js = new_record ? js_for_has_many(&form_block) : "" contents << js end @@ -159,14 +159,13 @@ def without_wrapper end # Capture the ADD JS - def js_for_has_many(class_string, &form_block) + def js_for_has_many(&form_block) assoc_name = assoc_klass.model_name - placeholder = "NEW_#{assoc_name.to_s.underscore.upcase.gsub(/\//, '_')}_RECORD" - opts = { + placeholder = "NEW_#{assoc_name.to_s.underscore.upcase.tr('/', '_')}_RECORD" + opts = options.merge( for: [assoc, assoc_klass.new], - class: class_string, for_options: { child_index: placeholder } - } + ) html = template.capture { __getobj__.send(:inputs_for_nested_attributes, opts, &form_block) } text = new_record.is_a?(String) ? new_record : I18n.t("active_admin.has_many_new", model: assoc_name.human) diff --git a/spec/unit/form_builder_spec.rb b/spec/unit/form_builder_spec.rb index eba404e7685..4260af49ea2 100644 --- a/spec/unit/form_builder_spec.rb +++ b/spec/unit/form_builder_spec.rb @@ -648,6 +648,48 @@ def user end end + describe "with custom class" do + let :body do + build_form({ url: "/categories" }, Category.new) do |f| + f.object.posts.build + f.has_many :posts, class: 'myclass' do |p| + p.input :title + end + end + end + + it "should generate a fieldset with the given class" do + expect(body).to have_css(".has_many_container > fieldset.myclass") + end + + it "should add the custom class on the fieldset generated by the new record link" do + link = body.find(".has_many_container > a.has_many_add") + new_record_html = Capybara.string(link[:'data-html']) + expect(new_record_html).to have_css("fieldset.myclass") + end + end + + describe "with custom attributes" do + let :body do + build_form({ url: "/categories" }, Category.new) do |f| + f.object.posts.build + f.has_many :posts, attr: "value", data: { 'custom-attribute': "custom-value" } do |p| + p.input :title + end + end + end + + it "should generate a fieldset with the given custom attributes" do + expect(body).to have_css(".has_many_container > fieldset[attr='value'][data-custom-attribute='custom-value']") + end + + it "should add custom attributes on the fieldset generated by the new record link" do + link = body.find(".has_many_container > a.has_many_add") + new_record_html = Capybara.string(link[:'data-html']) + expect(new_record_html).to have_css("fieldset[attr='value'][data-custom-attribute='custom-value']") + end + end + describe "with allow destroy" do shared_examples_for "has many with allow_destroy = true" do |child_num| it "should render the nested form" do