Class: Para::Inputs::NestedOneInput

Inherits:
NestedBaseInput
  • Object
show all
Defined in:
lib/para/inputs/nested_one_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

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Instance Method Details

#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
41
42
43
44
45
46
# File 'lib/para/inputs/nested_one_input.rb', line 6

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

  parent_model = object.class
  association = object.association(attribute_name)
  relation = parent_model.reflect_on_association(attribute_name)

  resource = object.send(attribute_name)
  @model = (resource && resource.class) || relation.klass

  unless resource
    # Build association without trying to save the new record
    resource = case association
    when ActiveRecord::Associations::HasOneThroughAssociation
      association.send(:replace, model.new)
    when ActiveRecord::Associations::HasOneAssociation
      association.send(:replace, model.new, false)
    else
      association.send(:replace, model.new)
    end
  end

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

  template.render(
    partial: 'para/inputs/nested_one',
    locals: {
      form: @builder,
      model: model,
      resource: resource,
      attribute_name: attribute_name,
      nested_locals: locals,
      collapsible: collapsible || subclass,
      dom_identifier: dom_identifier,
      subclass: subclass,
      subclasses: subclasses,
      add_button_label: add_button_label,
      add_button_class: add_button_class
    }
  )
end