Module: CoolNestedFormsHelper

Defined in:
app/helpers/cool_nested_forms_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_child_button(name, association, target, association_template = "", classes = "", child_template = "") ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/cool_nested_forms_helper.rb', line 29

def add_child_button(name, association,target,association_template="",classes="",child_template="")
  association_template = association if association_template.blank?
  (:span,"<span>#{name}</span>".html_safe,
    :class => "add_child #{classes}",
    :style => "",
    :"data-association" => association,
    :"data-association-template" => association_template,
    :"data-child-template" => child_template,
    :id => "#{association_template}_button",
    :target => target)
end

#new_fields_template(f, association, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/cool_nested_forms_helper.rb', line 3

def new_fields_template(f,association,options={})
  options[:object] ||= f.object.class.reflect_on_association(association).klass.new
  options[:partial] ||= association.to_s.singularize
  options[:template] ||= association.to_s
  options[:child_template_options] ||= []
  options[:f] ||= f

  child_tmpl = ""
  tmpl = (:div,:id =>"#{options[:template]}") do
    tmpl = f.fields_for(association,options[:object], :child_index => "new_#{association}") do |b|
      if options[:child_template_options].count > 0
        options[:child_template_options].each do |child|
          child_tmpl += new_fields_template(b,child[:association],child)
        end
        child_tmpl = child_tmpl.gsub( /\r?\n|\r/, ' ')
      end
      render(:partial=>options[:partial],:locals =>{:f => b})
    end
  end

  tmpl = tmpl.gsub( /\r?\n|\r/, ' ')
  script = "<script> var #{options[:template]} = '#{tmpl.to_s}'; </script>"
  script += child_tmpl
  return script.html_safe
end

#remove_child_button(name, classes = "", inner_html = "<span>Remove</span>") ⇒ Object



40
41
42
43
44
# File 'app/helpers/cool_nested_forms_helper.rb', line 40

def remove_child_button(name, classes="", inner_html="<span>Remove</span>")
  (:div,inner_html.html_safe,
    :style => "",
    :class => "remove_child #{classes}")
end