Class: Para::Inputs::NestedManyInput

Inherits:
NestedBaseInput
  • Object
show all
Defined in:
lib/para/inputs/nested_many_input.rb

Constant Summary

Constants inherited from NestedBaseInput

Para::Inputs::NestedBaseInput::GLOBAL_NESTED_FIELD_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/para/inputs/nested_many_input.rb', line 4

def resource
  @resource
end

Instance Method Details

#add_button_classObject



79
80
81
# File 'lib/para/inputs/nested_many_input.rb', line 79

def add_button_class
  options.fetch(:add_button_class) { 'btn-primary' }
end

#add_button_labelObject



75
76
77
# File 'lib/para/inputs/nested_many_input.rb', line 75

def add_button_label
  options.fetch(:add_button_label) { I18n.t('para.form.nested.add') }
end

#dom_identifierObject



50
51
52
53
54
55
56
57
58
# File 'lib/para/inputs/nested_many_input.rb', line 50

def dom_identifier
  @dom_identifier ||= begin
    name = attribute_name
    id = @builder.object.id || "_new_#{ parent_nested_field&.attribute_name }_"
    time = (Time.now.to_f * 1000).to_i
    random = (rand * 1000).to_i
    [name, id, time, random].join('-')
  end
end

#input(wrapper_options = nil) ⇒ Object



6
7
8
9
10
11
12
13
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
# File 'lib/para/inputs/nested_many_input.rb', line 6

def input(wrapper_options = nil)
  input_html_options[:class] << "nested-many"

  orderable = options.fetch(:orderable, model.orderable?)
  add_button = options.fetch(:add_button, true)
  # Load existing resources
  resources = object.send(attribute_name)
  # Order them if the list should be orderable
  resources = resources.sort_by(&:position) if orderable

  locals = options.fetch(:locals, {})

  with_global_nested_field do
    template.render(
      partial: 'para/inputs/nested_many',
      locals: {
        form: @builder,
        model: model,
        attribute_name: attribute_name,
        orderable: orderable,
        add_button: add_button,
        dom_identifier: dom_identifier,
        resources: resources,
        nested_locals: locals,
        subclass: subclass,
        subclasses: subclasses,
        inset: inset?,
        add_button_label: add_button_label,
        add_button_class: add_button_class,
        render_partial: render_partial?,
        remote_partial_params: remote_partial_params
      }
    )
  end
end

#inset?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/para/inputs/nested_many_input.rb', line 71

def inset?
  options.fetch(:inset, false)
end

#modelObject



46
47
48
# File 'lib/para/inputs/nested_many_input.rb', line 46

def model
  @model ||= parent_model.reflect_on_association(attribute_name).klass
end

#parent_modelObject



42
43
44
# File 'lib/para/inputs/nested_many_input.rb', line 42

def parent_model
  @parent_model ||= @builder.object.class
end

#remote_partial_paramsObject



87
88
89
90
91
# File 'lib/para/inputs/nested_many_input.rb', line 87

def remote_partial_params
  @remote_partial_params ||= options.fetch(:remote_partial_params, {}).merge(
    namespace: :nested_form
  )
end

#render_partial?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/para/inputs/nested_many_input.rb', line 83

def render_partial?
  options[:render_partial] || object.errors.any? || (object.persisted? && inset?)
end

#subclassObject



60
61
62
# File 'lib/para/inputs/nested_many_input.rb', line 60

def subclass
  @subclass ||= options.fetch(:subclass, subclasses.presence)
end

#subclassesObject



64
65
66
67
68
69
# File 'lib/para/inputs/nested_many_input.rb', line 64

def subclasses
  options.fetch(
    :subclasses,
    (model.try(:descendants) || []).sort_by { |m| m.model_name.human }
  )
end