Module: Cocoon::ViewHelpers

Defined in:
lib/cocoon/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_object(f, association) ⇒ Object

creates new association object with its conditions, like “ has_many :admin_comments, class_name: “Comment”, conditions: { author: “Admin” } will create new Comment with author “Admin”



82
83
84
85
86
# File 'lib/cocoon/view_helpers.rb', line 82

def create_object(f, association)
  assoc      = f.object.class.reflect_on_association(association)
  conditions = assoc.conditions.flatten
  new_object = assoc.klass.new(*conditions)
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 has_many relation.

  • html_options: html options to be passed to link_to (see link_to)

  • *&block*: see link_to



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cocoon/view_helpers.rb', line 50

def link_to_add_association(*args, &block)
  if block_given?
    f            = args[0]
    association  = args[1]
    html_options = args[2] || {}
    options      = args[3] || {}
    link_to_add_association(capture(&block), f, association, html_options)
  else
    name         = args[0]
    f            = args[1]
    association  = args[2]
    html_options = args[3] || {}
    options      = args[4] || {}

    render_options   = html_options.delete(:render_options)
    render_options ||= {}

    html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
    html_options[:'data-association'] = association.to_s.singularize
    html_options[:'data-associations'] = association.to_s.pluralize

    new_object = create_object(f, association)
    html_options[:'data-template'] = CGI.escapeHTML(render_association(association, f, new_object, render_options, options[:partial])).html_safe

    link_to(name, '#', html_options )
  end
end

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 link_to)

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 link_to)

  • *&block*: the output of the block will be show in the link, see link_to



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cocoon/view_helpers.rb', line 16

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

#render_association(association, f, new_object, render_options = {}, custom_partial = nil) ⇒ Object

:nodoc:



34
35
36
37
38
39
40
# File 'lib/cocoon/view_helpers.rb', line 34

def render_association(association, f, new_object, render_options={}, custom_partial=nil)
  partial = setup_partial(custom_partial, association)
  method_name = f.respond_to?(:semantic_fields_for) ? :semantic_fields_for : (f.respond_to?(:simple_fields_for) ? :simple_fields_for : :fields_for)
  f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge(render_options)) do |builder|
    render(partial, :f => builder, :dynamic => true)
  end
end

#setup_partial(partial, association) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/cocoon/view_helpers.rb', line 88

def setup_partial(partial, association)
  if partial
    partial
  else
    association.to_s.singularize + "_fields"
  end
end