Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/active_admin/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
42 changes: 42 additions & 0 deletions spec/unit/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down