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 insert 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

  • tag (String) (defaults to: 'a')

    HTML tag to use for the html generated, defaults to and ‘a` tag

  • link_content (Block)

    block of code for the link content

  • tag_attributes (Hash<attribute, value>) (defaults to: {})

    hash with attribute,value pairs for the html tag

Returns:

  • (String)

    link tag



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
45
46
47
48
49
50
51
# File 'lib/vanilla_nested/view_helpers.rb', line 16

def link_to_add_nested(form, association, container_selector, link_text: nil, link_classes: '', insert_method: :append, partial: nil, partial_form_variable: :form, tag: 'a', tag_attributes: {}, &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]

  attributes = tag_attributes
  attributes[:class] = "#{attributes.fetch(:class, '')} #{classes}"
  attributes[:data] = attributes.fetch(:data, {}).merge(data)

  (tag, attributes) do
    if block_given?
      yield link_content
    else
      link_text || "Add #{association_class.model_name}"
    end
  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

  • tag (String) (defaults to: 'a')

    HTML tag to use for the html generated, defaults to and ‘a` tag

  • link_content (Block)

    block of code for the link content

  • tag_attributes (Hash<attribute, value>) (defaults to: {})

    hash with attribute,value pairs for the html tag

Returns:

  • (String)

    hidden field and link tag



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vanilla_nested/view_helpers.rb', line 64

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: '', tag: 'a', tag_attributes: {}, &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}"

  attributes = tag_attributes
  attributes[:class] = "#{attributes.fetch(:class, '')} #{classes}"
  attributes[:data] = attributes.fetch(:data, {}).merge(data)

  capture do
    concat form.hidden_field(:_destroy, value: 0)
    concat(
      (tag, attributes) do
        if block_given?
          yield link_content
        else
          link_text.html_safe
        end
      end
    )
  end
end