Class: Para::Inputs::NestedOneInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Defined in:
lib/para/inputs/nested_one_input.rb

Instance Method Summary collapse

Instance Method Details

#input(wrapper_options = nil) ⇒ Object



4
5
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
# File 'lib/para/inputs/nested_one_input.rb', line 4

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)
  model = relation.klass

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

  template.render(
    partial: 'para/inputs/nested_one',
    locals: {
      form: @builder,
      model: model,
      resource: resource,
      attribute_name: attribute_name
    }
  )
end