Class: DataDoc::Present
- Inherits:
-
Object
- Object
- DataDoc::Present
- Defined in:
- lib/data_doc/present.rb
Overview
Presents the results of a query in an html table.
Class Method Summary collapse
-
.present(doc, arel_or_str, &blk) ⇒ Object
Accept display options from a block.
Instance Method Summary collapse
-
#calculated(col, &blk) ⇒ Object
Define a calculated column based on a block.
-
#caption(text) ⇒ Object
Set the caption for the table.
-
#column_order(*order) ⇒ Object
Defines presentation set and order of columns.
-
#each_cell(col, &blk) ⇒ Object
Define a block which is called to override the contents of a cell.
-
#label(column, text) ⇒ Object
Rename the column heading text for a particular column.
-
#no_headers ⇒ Object
Suppress header row.
-
#render ⇒ Object
Generate html.
Class Method Details
.present(doc, arel_or_str, &blk) ⇒ Object
Accept display options from a block. Returns html table.
present 'select one, two from relation' do
'Table caption'
column_order 'two', 'one'
end
For more table configuration options see the member functions of DataDoc::Present
21 22 23 24 25 26 |
# File 'lib/data_doc/present.rb', line 21 def self.present(doc, arel_or_str, &blk) rows = doc.connection.select_all(arel_or_str) p = Present.new(doc, rows) p.instance_eval(&blk) unless blk.nil? p.render end |
Instance Method Details
#calculated(col, &blk) ⇒ Object
Define a calculated column based on a block.
present 'select one, two from relation' do
calculated 'three' do |col, row|
row['two'] == 'true' ? 'Short' : 'Long'
end
end
88 89 90 |
# File 'lib/data_doc/present.rb', line 88 def calculated(col, &blk) # :yields: row @calculated[col] = blk end |
#caption(text) ⇒ Object
Set the caption for the table
present 'select 1' do
'Table caption'
end
48 49 50 |
# File 'lib/data_doc/present.rb', line 48 def (text) @caption = text end |
#column_order(*order) ⇒ Object
Defines presentation set and order of columns.
Not every field queried needs to be presented.
present 'select one, two from relation' do
column_order 'two', 'one'
end
37 38 39 |
# File 'lib/data_doc/present.rb', line 37 def column_order(*order) @column_order = order end |
#each_cell(col, &blk) ⇒ Object
Define a block which is called to override the contents of a cell.
Return nil to revert to the default behaviour.
present 'select one, two from relation' do
each_cell 'one' do |col, row|
row[col] == 'true' ? 'Short' : 'Long'
end
end
75 76 77 |
# File 'lib/data_doc/present.rb', line 75 def each_cell(col, &blk) # :yields: col, row @each_cell[col] = blk end |
#label(column, text) ⇒ Object
Rename the column heading text for a particular column.
present 'select one, two from relation' do
label 'one', 'New column heading'
end
59 60 61 |
# File 'lib/data_doc/present.rb', line 59 def label(column, text) @labels[column] = text end |
#no_headers ⇒ Object
Suppress header row.
present 'select one, two from relation' do
no_headers
end
99 100 101 |
# File 'lib/data_doc/present.rb', line 99 def no_headers @no_headers = true end |
#render ⇒ Object
Generate html.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/data_doc/present.rb', line 106 def render h = Builder::XmlMarkup.new h.table { h.(@caption) unless @caption.nil? unless @no_headers h.thead { h.tr { @column_order.each { |c| h.th(@labels[c] || c.to_s.humanize) } } } end h.tfoot h.tbody { @rows.each do |r| h.tr { @column_order.each do |col| r[col.to_s] = @calculated[col].call(col, r) unless @calculated[col].nil? if @each_cell[col].nil? h.td(r[col.to_s]) else h.td(@each_cell[col].call(col, r) || r[col.to_s]) end end } end } } end |