Module: CoolNestedFormsHelper
- Defined in:
- app/helpers/cool_nested_forms_helper.rb
Instance Method Summary collapse
-
#entries_container(builder, association, current_entries, options = {}) ⇒ Object
entries_container ## arguments - builder: a FormBuilder Object obtained from form_with or form_for - association: a model class i.e.
-
#new_entry_button(name, association, options = {}) ⇒ Object
new_entry_button ## arguments - name: A string to be displayed as the button text - association: a model class i.e.
-
#new_entry_template(builder, association, options = {}) ⇒ Object
new_entry_template ## arguments - builder: a FormBuilder Object obtained from form_with or form_for - association: a model class i.e.
-
#remove_entry_button(name, association, options = {}) ⇒ Object
remove_entry_button arguments - name: A string to be displayed as the button text - target: a html tag ID that contains the element to be removed - options: hash of override options - class: a string with any css class names to be used for styling - style: a string with any css styles - tag: override the returned tag - tag_content: override the returned tag content.
Instance Method Details
#entries_container(builder, association, current_entries, options = {}) ⇒ Object
entries_container ## arguments
-
builder: a FormBuilder Object obtained from form_with or form_for
-
association: a model class i.e. MyTask
-
current_entries: all the current entries for the parent i.e. a Form.tasks
-
options: hash of override options
-
partial: name of the erb partial to use as a tempalte for the existing entries
-
id: ID to be used in for the container
-
plurilized: the plural version of the model name
-
singularized: the singular version of the model name
-
parent_singularized: the name of the parent record. used for generating the entry’s container id
-
parent_id: the id of the parent record. used for generating the entry’s container id
-
class: a string with any css class names to be used for styling
-
style: a string with any css styles
-
tag: override the returned tag
-
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/helpers/cool_nested_forms_helper.rb', line 17 def entries_container(builder,association,current_entries, = {}) # initialize options [:partial] ||= "#{association.name.underscore}_edit" [:id] ||= association.name.underscore.pluralize [:pluralized] ||= association.name.underscore.pluralize [:singularized] ||= association.name.underscore [:parent_singularized] ||= builder.object.class.name.underscore [:parent_id] ||= builder.object.id.to_s [:class] ||= '' [:style] ||= '' [:tag] ||= :div # render all the current entries output = builder.fields_for([:pluralized], current_entries) do |assoc_builder| id = "#{[:parent_singularized]}_#{[:parent_id]}_#{[:singularized]}_#{assoc_builder.object.id}" render [:partial], :builder => assoc_builder, :id => id end return content_tag( [:tag], output.html_safe, :style => [:style], :class => [:class], :id => [:id] ) end |
#new_entry_button(name, association, options = {}) ⇒ Object
new_entry_button ## arguments
-
name: A string to be displayed as the button text
-
association: a model class i.e. MyTask
-
target: a html tag ID where the button will be attached
-
options: hash of override options
-
plurilized: the plural version of the model name
-
js_template_name: name for the javascript variable generated by this method ( useful when there are multiple nested associations)
-
class: a string with any css class names to be used for styling
-
style: a string with any css styles
-
child_js_template_names: an array with any children js template names. every time a nested association that contains other nested associations is added, a button to add a child entry is added to the new entry
-
tag: override the returned tag
-
tag_content: override the returned tag content
-
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/helpers/cool_nested_forms_helper.rb', line 102 def (name, association, = {}) [:pluralized] ||= association.name.underscore.pluralize [:js_template_name] ||= association.name.underscore.pluralize [:class] ||= "" [:style] ||= "" [:child_js_template_names] ||= "" [:tag] ||= :span [:tag_content] ||= "<span>#{name}</span>" [:target] ||= association.name.underscore.pluralize return content_tag([:tag], [:tag_content].html_safe, :class => "cnf-add-entry #{[:class]}", :style => [:style], "data-association" => [:pluralized], "data-js-template-name" => [:js_template_name], "data-child-js-template-names" => [:child_js_template_names], :id => "#{[:js_template_name]}_button", "data-target" => [:target]) end |
#new_entry_template(builder, association, options = {}) ⇒ Object
new_entry_template ## arguments
-
builder: a FormBuilder Object obtained from form_with or form_for
-
association: a model class i.e. MyTask
-
options: hash of override options
-
partial: name of the erb partial to use as a tempalte
-
id: ID to be used in for the container
-
plurilized: the plural version of the model name
-
js_template_name: name for the javascript variable generated by this method ( useful when there are multiple nested associations)
-
children: an array with childre association options for nested association of a nested association
-
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/helpers/cool_nested_forms_helper.rb', line 53 def new_entry_template(builder,association, = {}) # initialize options [:partial] ||= "#{association.name.underscore}_new" [:id] ||= "#{association.name.underscore}_[tempid]" [:pluralized] ||= association.name.underscore.pluralize [:js_template_name] ||= association.name.underscore.pluralize [:children] ||= [] # children output children_output = '' # render the template using the form builder object and the association output = builder.fields_for([:pluralized],association.new, :child_index => "new_#{[:pluralized]}") do |assoc_builder| # render each children [:children].each do |child| children_output += new_entry_template(assoc_builder,child[:association],child) end # render erb partial render(:partial=>[:partial],:locals =>{:builder => assoc_builder, :id => [:id] }) end # remove any new line characters output = output.gsub( /\r?\n|\r/, ' ') # create the script tag that will be output to the page script_tag = "<script> var #{[:js_template_name]} = '#{output}'; </script>" # add children_output script_tag += children_output # return the output as html safe return script_tag.html_safe end |
#remove_entry_button(name, association, options = {}) ⇒ Object
remove_entry_button arguments
-
name: A string to be displayed as the button text
-
target: a html tag ID that contains the element to be removed
-
options: hash of override options
-
class: a string with any css class names to be used for styling
-
style: a string with any css styles
-
tag: override the returned tag
-
tag_content: override the returned tag content
-
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/helpers/cool_nested_forms_helper.rb', line 132 def (name, association, = {}) [:class] ||= '' [:style] ||= '' [:tag] ||= :div [:tag_content] ||= "<span>#{name}</span>" [:target] ||= "" return content_tag( [:tag], [:tag_content].html_safe, :style => [:style], :class => "cnf-remove-entry #{[:class]}", "data-target" => [:target] ) end |