Module: Para::FormBuilder::NestedForm

Defined in:
lib/para/form_builder/nested_form.rb

Instance Method Summary collapse

Instance Method Details

#allow_destroy?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/para/form_builder/nested_form.rb', line 47

def allow_destroy?
  return false unless nested?

  nested_options = parent_object.nested_attributes_options
  relation = nested_options[nested_attribute_name]
  relation && relation[:allow_destroy]
end

#inverse_of?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/para/form_builder/nested_form.rb', line 55

def inverse_of?(field_name)
  return false unless nested?

  reflection = parent_object.class.reflect_on_association(nested_attribute_name)
  reflection && (reflection.options[:inverse_of] == field_name)
end

#nested?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/para/form_builder/nested_form.rb', line 62

def nested?
  nested_attribute_name.present?
end

#nested_attribute_nameObject



66
67
68
# File 'lib/para/form_builder/nested_form.rb', line 66

def nested_attribute_name
  options[:nested_attribute_name]
end

#nested_fieldsObject

FIXME : When we have a nested field that maps to an STI model, the _id

field is passed instead of the relation, and the inverse_of
guard doesn't work


8
9
10
11
12
# File 'lib/para/form_builder/nested_form.rb', line 8

def nested_fields
  @nested_fields ||= fields.reject do |field|
    inverse_of?(field.name)
  end
end

#nested_resource_dom_idObject



24
25
26
27
28
29
30
31
32
# File 'lib/para/form_builder/nested_form.rb', line 24

def nested_resource_dom_id
  return "" unless nested?

  @nested_resource_dom_id ||= [
    object.class.model_name.singular,
    (Time.now.to_f * 1000).to_i,
    (object.id || "_new_#{ nested_attribute_name }_id")
  ].join('-')
end

#nested_resource_nameObject



14
15
16
17
18
19
20
21
22
# File 'lib/para/form_builder/nested_form.rb', line 14

def nested_resource_name
  @nested_resource_name ||= begin
    name_method = Para.config.resource_name_methods.find do |method_name|
      object.respond_to?(method_name)
    end

    (name_method && object.try(name_method).presence) || default_resource_name
  end
end

#parent_objectObject



70
71
72
# File 'lib/para/form_builder/nested_form.rb', line 70

def parent_object
  nested? && options[:parent_builder] && options[:parent_builder].object
end

#remove_association_buttonObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/para/form_builder/nested_form.rb', line 34

def remove_association_button
  return "" unless allow_destroy?

  template.(:div, class: 'panel-btns pull-right') do
    template.link_to_remove_association(
      self, wrapper_class: 'form-fields', class: 'btn btn-danger'
    ) do
      template.(:i, '', class: 'fa fa-trash') +
      ::I18n.t('para.form.nested.remove')
    end
  end
end