Skip to content

Commit fa6cfe0

Browse files
committed
Merge pull request activeadmin#1782 from ptn/add_tests_for_has_many_js
Add tests for PR 1699
2 parents 30e0967 + e1a1519 commit fa6cfe0

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

lib/active_admin/form_builder.rb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,7 @@ def has_many(association, options = {}, &block)
7676
form_buffers.last << template.content_tag(:h3, object.class.reflect_on_association(association).klass.model_name.human(:count => 1.1))
7777
inputs options, &form_block
7878

79-
# Capture the ADD JS
80-
placeholder = "NEW_#{object.class.reflect_on_association(association).klass.model_name.human.upcase}_RECORD"
81-
js = with_new_form_buffer do
82-
inputs_for_nested_attributes :for => [association, object.class.reflect_on_association(association).klass.new],
83-
:class => "inputs has_many_fields",
84-
:for_options => {
85-
:child_index => placeholder
86-
}, &form_block
87-
end
88-
89-
js = template.escape_javascript(js)
90-
js = template.link_to I18n.t('active_admin.has_many_new', :model => object.class.reflect_on_association(association).klass.model_name.human), "#", :onclick => "$(this).before('#{js}'.replace(/#{placeholder}/g, new Date().getTime())); return false;", :class => "button"
91-
79+
js = js_for_has_many(association, form_block, template)
9280
form_buffers.last << js.html_safe
9381
end
9482
end
@@ -181,5 +169,28 @@ def with_new_form_buffer
181169
return_value
182170
end
183171

172+
# Capture the ADD JS
173+
def js_for_has_many(association, form_block, template)
174+
association_reflection = object.class.reflect_on_association(association)
175+
association_human_name = association_reflection.klass.model_name.human
176+
placeholder = "NEW_#{association_human_name.upcase.split(' ').join('_')}_RECORD"
177+
178+
js = with_new_form_buffer do
179+
inputs_for_nested_attributes :for => [association, association_reflection.klass.new],
180+
:class => "inputs has_many_fields",
181+
:for_options => { :child_index => placeholder },
182+
&form_block
183+
end
184+
185+
js = template.escape_javascript(js)
186+
187+
text = I18n.t 'active_admin.has_many_new', :model => association_human_name
188+
onclick = "$(this).before('#{js}'.replace(/#{placeholder}/g, new Date().getTime())); return false;"
189+
190+
template.link_to text, "#",
191+
:onclick => onclick,
192+
:class => "button"
193+
end
194+
184195
end
185196
end

spec/unit/form_builder_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ def build_form(options = {}, form_object = Post.new, &block)
338338
Capybara.string(body).should have_css(".has_many > fieldset > ol > li > a", :class => "button", :href => "#", :content => "Delete")
339339
end
340340

341+
it "should include the nested record's class name in the js" do
342+
body.should have_tag("a", :attributes => { :onclick => /NEW_POST_RECORD/ })
343+
end
344+
341345
it "should add a link to add new nested records" do
342346
Capybara.string(body).should have_css(".has_many > fieldset > ol > li > a", :class => "button", :href => "#", :content => "Add New Post")
343347
end

0 commit comments

Comments
 (0)