Class: FormForms::Elements::TableFields

Inherits:
BaseElement show all
Defined in:
lib/form_forms/elements/table_fields.rb

Instance Attribute Summary

Attributes inherited from BaseElement

#elements

Instance Method Summary collapse

Methods inherited from BaseElement

#delete

Constructor Details

#initialize(association, form_args = {}) ⇒ TableFields

Returns a new instance of TableFields.



43
44
45
46
47
48
49
50
# File 'lib/form_forms/elements/table_fields.rb', line 43

def initialize(association, form_args={})
  self.association association
  self.args form_args
  self.table_args {}
  self.row_args {}

  super
end

Instance Method Details

#header(row_args = {}, &generator) ⇒ Object



57
58
59
60
# File 'lib/form_forms/elements/table_fields.rb', line 57

def header(row_args = {}, &generator)
  @header = TableHeader.new(row_args, &generator) if block_given?
  @header
end

#render(builder, view) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/form_forms/elements/table_fields.rb', line 62

def render(builder, view)
  association = eval_property(:association, builder, view)
  form_args = eval_property(:args, builder, view)
  table_args = eval_property(:table_args, builder, view)

  view.(:table, table_args) do
    # render the header row
    view.concat view.(:thead, header.render(builder, view)) if @header

    # render the table body
    body = builder.association(association, form_args) do |sub_builder|
      row_args = eval_property(:row_args, sub_builder, view)
      view.(:tr, row_args) do
        super(sub_builder, view)
      end
    end
    view.concat view.(:tbody, body)
  end
end