* added html_options and block args to link_to_remove association and link_to_add_association

This commit is contained in:
Mihai Târnovan 2011-03-03 18:54:43 +01:00
parent 65eaf17f4e
commit dc1ad12845

View File

@ -6,9 +6,18 @@ module Cocoon
# #
# - *name* : the text of the link # - *name* : the text of the link
# - *f* : the form this link should be placed in # - *f* : the form this link should be placed in
def link_to_remove_association(name, f) # - *html_options*: html options to be passed to link_to (see <tt>link_to</tt>)
is_dynamic = f.object.new_record? # - *&block*: see <tt>link_to</tt>
f.hidden_field(:_destroy) + link_to(name, '#', :class => "remove_fields #{is_dynamic ? 'dynamic' : 'existing'}")
def link_to_remove_association(name, f, html_options = {}, &block)
if block_given?
name = capture(&block)
link_to_remove_association(name, f, html_options)
else
is_dynamic = f.object.new_record?
html_options[:class] = [html_options[:class], "remove_fields #{is_dynamic ? 'dynamic' : 'existing'}"].compact.join(' ')
f.hidden_field(:_destroy) + link_to(name, '#', html_options)
end
end end
# :nodoc: # :nodoc:
@ -23,14 +32,25 @@ module Cocoon
# - *name* : the text to show in the link # - *name* : the text to show in the link
# - *f* : the form this should come in (the formtastic form) # - *f* : the form this should come in (the formtastic form)
# - *association* : the associated objects, e.g. :tasks, this should be the name of the <tt>has_many</tt> relation. # - *association* : the associated objects, e.g. :tasks, this should be the name of the <tt>has_many</tt> relation.
# # - *html_options*: html options to be passed to <tt>link_to</tt> (see <tt>link_to</tt>)
def link_to_add_association(name, f, association) # - *&block*: see <tt>link_to</tt>
new_object = f.object.class.reflect_on_association(association).klass.new
model_name = new_object.class.name.underscore def link_to_add_association(name, f, association, html_options = {}, &block)
hidden_div = content_tag('div', :id => "#{model_name}_fields_template", :style => "display:none;") do if block_given?
render_association(association, f, new_object) name = capture(&block)
link_to_add_association(name, f, association, html_options)
else
html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
html_options[:'data-association'] = association.to_s.singularize
new_object = f.object.class.reflect_on_association(association).klass.new
model_name = new_object.class.name.underscore
hidden_div = content_tag('div', :id => "#{model_name}_fields_template", :style => "display:none;") do
render_association(association, f, new_object)
end
hidden_div.html_safe + link_to(name, '#', html_options )
end end
hidden_div.html_safe + link_to(name, '#', :class => 'add_fields', :'data-association' => association.to_s.singularize)
end end
end end