Class: Compendium::Presenters::Table

Inherits:
Query
  • Object
show all
Defined in:
app/classes/compendium/presenters/table.rb

Direct Known Subclasses

CSV

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#to_s

Constructor Details

#initialize {|@settings| ... } ⇒ Table

Returns a new instance of Table.

Yields:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/classes/compendium/presenters/table.rb', line 5

def initialize(*)
  super

  @records = results.records

  @settings = settings_class.new(query)
  @settings.set_headings(results.keys)
  @settings.update(&query.table_settings) if query.table_settings
  yield @settings if block_given?

  if has_totals_row?
    @totals = @records.pop
    totals[totals.keys.first] = translate(:total)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Compendium::Presenters::Base

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



3
4
5
# File 'app/classes/compendium/presenters/table.rb', line 3

def records
  @records
end

#settingsObject (readonly)

Returns the value of attribute settings.



3
4
5
# File 'app/classes/compendium/presenters/table.rb', line 3

def settings
  @settings
end

#totalsObject (readonly)

Returns the value of attribute totals.



3
4
5
# File 'app/classes/compendium/presenters/table.rb', line 3

def totals
  @totals
end

Instance Method Details

#renderObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/classes/compendium/presenters/table.rb', line 21

def render
  (:table, class: @settings.table_class) do
    table = ActiveSupport::SafeBuffer.new
    table << (:thead, build_row(headings, settings.header_class, :th, &heading_proc))
    table << (:tbody) do
      tbody = ActiveSupport::SafeBuffer.new
      records.each { |row| tbody << build_row(row, settings.row_class, &data_proc) }
      tbody
    end
    table << (:tfoot, build_row(totals, @settings.totals_class, :th, &totals_proc)) if has_totals_row?
    table
  end
end