Skip to content

Instantly share code, notes, and snippets.

@ruckus-matte
Created March 14, 2011 20:49
Show Gist options
  • Save ruckus-matte/869849 to your computer and use it in GitHub Desktop.
Save ruckus-matte/869849 to your computer and use it in GitHub Desktop.
new view_helpers.rb for Cocoon gem. fixes problem where :class_name is different from has_many association name
module Cocoon
module ViewHelpers
# this will show a link to remove the current association. This should be placed inside the partial.
# either you give
# - *name* : the text of the link
# - *f* : the form this link should be placed in
# - *html_options*: html options to be passed to link_to (see <tt>link_to</tt>)
#
# or you use the form without *name* with a *&block*
# - *f* : the form this link should be placed in
# - *html_options*: html options to be passed to link_to (see <tt>link_to</tt>)
# - *&block*: the output of the block will be show in the link, see <tt>link_to</tt>
def link_to_remove_association(*args, &block)
if block_given?
f = args.first
html_options = args.second || {}
name = capture(&block)
link_to_remove_association(name, f, html_options)
else
name = args[0]
f = args[1]
html_options = args[2] || {}
is_dynamic = f.object.new_record?
html_options[:class] = [html_options[:class], "remove_fields #{is_dynamic ? 'dynamic' : 'existing'}"].compact.join(' ')
hidden_field_tag("#{f.object_name}[_destroy]") + link_to(name, '#', html_options)
end
end
# :nodoc:
def render_association(association, f, new_object)
new_object = f.object.class.reflect_on_association(association).klass.new
model_name = new_object.class.name.underscore
f.fields_for(association, new_object, :child_index => "new_#{model_name}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true)
end
end
# shows a link that will allow to dynamically add a new associated object.
#
# - *name* : the text to show in the link
# - *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.
# - *html_options*: html options to be passed to <tt>link_to</tt> (see <tt>link_to</tt>)
# - *&block*: see <tt>link_to</tt>
def link_to_add_association(*args, &block)
if block_given?
f = args[0]
association = args[1]
html_options = args[2] || {}
link_to_add_association(capture(&block), f, association, html_options)
else
name = args[0]
f = args[1]
association = args[2]
html_options = args[3] || {}
new_object = f.object.class.reflect_on_association(association).klass.new
model_name = new_object.class.name.underscore
html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
html_options[:'data-association'] = model_name
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
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment