@@ -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
185196end
0 commit comments