Module: RondoForm::ViewHelpers
- Defined in:
- lib/rondo_form/view_helpers.rb
Instance Method Summary collapse
-
#link_to_add_association(*args, &block) ⇒ Object
shows a link that will allow to dynamically add a new associated object.
-
#link_to_remove_association(*args, &block) ⇒ Object
this will show a link to remove the current association.
-
#render_association(association, f, new_object, render_options) ⇒ Object
:nodoc:.
Instance Method Details
#link_to_add_association(*args, &block) ⇒ Object
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
-
association : the associated objects, e.g. :tasks, this should be the name of the
has_many
relation. -
render_options: options to be passed to
render
-
partial: ‘file_name’
-
locals: { hash_of: ‘local variables for rendered partial’ }
-
-
html_options: html options to be passed to
link_to
(seelink_to
) -
*&block*: see
link_to
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rondo_form/view_helpers.rb', line 43 def link_to_add_association(*args, &block) if block_given? f, association, , = *args ||= {} ||= {} link_to_add_association(capture(&block), f, association, , ) else name, f, association, , = *args ||= {} ||= {} [:class] = [[:class], "add_fields"].compact.join(' ') [:'data-association'] = association.to_s.singularize [:'data-associations'] = association.to_s.pluralize [:'data-action'] = "click->nested-rondo#addField" new_object = f.object.class.reflect_on_association(association).klass.new model_name = new_object.class.name.underscore hidden_div = content_tag("template", id: "#{model_name}_fields_template", data: {'nested-rondo_target': 'template'}) do render_association(association, f, new_object, ) end hidden_div.html_safe + link_to(name, '', ) end end |
#link_to_remove_association(*args, &block) ⇒ Object
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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rondo_form/view_helpers.rb', line 15 def link_to_remove_association(*args, &block) if block_given? f = args.first = args.second || {} name = capture(&block) link_to_remove_association(name, f, ) else name, f, = *args ||= {} is_dynamic = f.object.new_record? [:class] = [[:class], "remove_fields #{is_dynamic ? 'dynamic' : 'existing'}"].compact.join(' ') [:'data-action'] = "click->nested-rondo#removeField" f.hidden_field(:_destroy) + link_to(name, '', ) end end |
#render_association(association, f, new_object, render_options) ⇒ Object
:nodoc:
69 70 71 72 73 74 75 76 |
# File 'lib/rondo_form/view_helpers.rb', line 69 def render_association(association, f, new_object, ) locals = .delete(:locals) || {} [:partial] = "#{association.to_s.singularize}_fields" unless [:partial] f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| locals.store(:f, builder) render([:partial], locals) end end |