Class: Interview::NestedForm

Inherits:
Control
  • Object
show all
Includes:
NestedBuildable
Defined in:
lib/interview/controls/nested_form.rb

Direct Known Subclasses

PolymorphicNestedForm

Instance Attribute Summary collapse

Attributes inherited from Control

#parent

Instance Method Summary collapse

Methods included from NestedBuildable

#create_nested_builder, #render_nested_builder

Methods inherited from Control

#ancestors, #build_child, #build_with_params, #find_attribute, #find_attribute!, #initialize, #set_attributes, #set_defaults

Constructor Details

This class inherits a constructor from Interview::Control

Instance Attribute Details

#form_builderObject (readonly)

Returns the value of attribute form_builder.



7
8
9
# File 'lib/interview/controls/nested_form.rb', line 7

def form_builder
  @form_builder
end

#image_basedObject

todo: move to an other control?



6
7
8
# File 'lib/interview/controls/nested_form.rb', line 6

def image_based
  @image_based
end

#objectObject (readonly)

Returns the value of attribute object.



7
8
9
# File 'lib/interview/controls/nested_form.rb', line 7

def object
  @object
end

Instance Method Details

#build(b, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/interview/controls/nested_form.rb', line 9

def build(b, &block)
  assoc_form_builder = find_attribute! :form_builder
  assoc_method = find_attribute! :assoc_method
  
  b.section html_class: 'nested_form_container' do # todo: class überarbeiten
    if @image_based
      b.nested_form_add_images
    end
    b << assoc_form_builder.fields_for(assoc_method.to_sym) do |form_builder|
      render_nested_form(b, form_builder, &block)
    end
    b.section style: 'script', html_class: 'nested_form_script', html_options: { type: 'text/x-tmpl' } do
      new_object = assoc_form_builder.object.association(assoc_method.to_sym).build # todo: polymorphic add link
      b << assoc_form_builder.fields_for(assoc_method.to_sym, new_object, :child_index => "${index}") do |form_builder|
        render_nested_form(b, form_builder, &block)
      end
    end
  end
end

#render_nested_form(b, form_builder) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/interview/controls/nested_form.rb', line 29

def render_nested_form(b, form_builder)
  create_nested_builder(b)
  @form_builder = form_builder
  @object = form_builder.object
  b.section html_class: 'nested_form collapse in' do
    yield if block_given?
    b << form_builder.hidden_field(:id)
    b << form_builder.hidden_field(:_destroy, class: 'nested_form_destroy')
    if @image_based
      b << form_builder.hidden_field(:upload_buffer_id, class: 'upload_buffer_id')
    end
  end
  return render_nested_builder(b)
end