Module: VanillaNested::ViewHelpers

Defined in:
lib/vanilla_nested/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

Returns link tag.

Parameters:

  • form (FormBuild)

    builder on a “form_for” block

  • association (Symbol)

    name of the association

  • container_selector (String)

    selector of the element to inser the fields

  • link_text (String, nil) (defaults to: nil)

    text to use for the link tag

  • link_classes (String) (defaults to: '')

    space separated classes for the link tag

  • insert_method (:append, :prepend) (defaults to: :append)

    tells javascript if the new fields should be appended or prepended to the container

  • partial_form_variable (String, Symbol) (defaults to: :form)

    name of the variable that represents the form builder inside the fields partial

  • link_content (Block)

    block of code for the link content

Returns:

  • (String)

    link tag



14
15
16
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
43
44
# File 'lib/vanilla_nested/view_helpers.rb', line 14

def link_to_add_nested(form, association, container_selector, link_text: nil, link_classes: '', insert_method: :append, partial: nil, partial_form_variable: :form, &link_content)
  association_class = form.object.class.reflections[association.to_s].klass
  object = association_class.new

  partial_name = partial || "#{association_class.name.underscore}_fields"

  html = capture do
    form.fields_for association, object, child_index: '_idx_placeholder_' do |ff|
      render partial: partial_name, locals: { partial_form_variable => ff }
    end
  end

  method_for_insert = i[append prepend].include?(insert_method.to_sym) ? insert_method : :append

  classes = "vanilla-nested-add #{link_classes}"
  data = {
    'container-selector': container_selector,
    'html': html,
    'method-for-insert': method_for_insert
  }

  nested_options = form.object.class.nested_attributes_options[association.to_sym]
  data['limit'] = nested_options[:limit] if nested_options[:limit]

  if block_given?
    link_to '#', class: classes, data: data, &link_content
  else
    text = link_text || "Add #{association_class.model_name}"
    link_to text, '#', class: classes, data: data
  end
end

Returns hidden field and link tag.

Parameters:

  • form (FormBuilder)

    builder on a “form_for” block

  • link_text (String, nil) (defaults to: 'X')

    text for the link, defaults to ‘X’

  • fields_wrapper_selector (String) (defaults to: nil)

    selector for the wrapper of the fields, must be an ancestor

  • undo_link_timeout (Integer) (defaults to: nil)

    time until undo timeouts

  • undo_link_text (String) (defaults to: 'Undo')

    text to show as “undo”

  • undo_link_classes (String) (defaults to: '')

    space separated list of classes for the “undo” link

  • ulink_classes (String)

    space separated list of classes for the “x” link

  • link_content (Block)

    block of code for the link content

Returns:

  • (String)

    hidden field and link tag



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vanilla_nested/view_helpers.rb', line 55

def link_to_remove_nested(form, link_text: 'X', fields_wrapper_selector: nil, undo_link_timeout: nil, undo_link_text: 'Undo', undo_link_classes: '', link_classes: '', &link_content)
  data = {
    'fields-wrapper-selector': fields_wrapper_selector,
    'undo-timeout': undo_link_timeout,
    'undo-text': undo_link_text,
    'undo-link-classes': undo_link_classes
  }

  classes = "vanilla-nested-remove #{link_classes}"

  capture do
    concat form.hidden_field(:_destroy, value: 0)
    concat(
      if block_given?
        link_to('#', class: classes, data: data, &link_content)
      else
        link_to(link_text.html_safe, '#', class: classes, data: data)
      end
    )
  end
end