Class: Spectabular::Table
- Inherits:
-
Object
- Object
- Spectabular::Table
- Defined in:
- lib/spectabular/table.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#collection_name ⇒ Object
Returns the value of attribute collection_name.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#context ⇒ Object
Returns the value of attribute context.
- #default_empty ⇒ Object
-
#skip_sorting_row ⇒ Object
Returns the value of attribute skip_sorting_row.
Instance Method Summary collapse
- #cell(column, record) ⇒ Object
- #empty? ⇒ Boolean
- #empty_message ⇒ Object
- #headers ⇒ Object
-
#initialize(options = {}) ⇒ Table
constructor
A new instance of Table.
- #rows ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Table
Returns a new instance of Table.
7 8 9 10 11 |
# File 'lib/spectabular/table.rb', line 7 def initialize(={}) .each do |key,value| self.send("#{key}=",value) end end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
4 5 6 |
# File 'lib/spectabular/table.rb', line 4 def collection @collection end |
#collection_name ⇒ Object
Returns the value of attribute collection_name.
4 5 6 |
# File 'lib/spectabular/table.rb', line 4 def collection_name @collection_name end |
#columns ⇒ Object
Returns the value of attribute columns.
4 5 6 |
# File 'lib/spectabular/table.rb', line 4 def columns @columns end |
#context ⇒ Object
Returns the value of attribute context.
4 5 6 |
# File 'lib/spectabular/table.rb', line 4 def context @context end |
#default_empty ⇒ Object
28 29 30 |
# File 'lib/spectabular/table.rb', line 28 def default_empty @default_empty ||= 'n/a' end |
#skip_sorting_row ⇒ Object
Returns the value of attribute skip_sorting_row.
4 5 6 |
# File 'lib/spectabular/table.rb', line 4 def skip_sorting_row @skip_sorting_row end |
Instance Method Details
#cell(column, record) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/spectabular/table.rb', line 48 def cell(column,record) if column.respond_to? :to_sym record.send column.to_sym else if column[:helper] column[:helper].respond_to?(:call) ? column[:helper].call(record) : context.send(column[:helper],record) elsif column[:value] record.send column[:value] else nil end end end |
#empty? ⇒ Boolean
24 25 26 |
# File 'lib/spectabular/table.rb', line 24 def empty? collection.blank? end |
#empty_message ⇒ Object
32 33 34 |
# File 'lib/spectabular/table.rb', line 32 def "No #{collection_name} added yet" end |
#headers ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/spectabular/table.rb', line 62 def headers @headers ||= columns.map do |column| if column.respond_to? :to_sym column.to_s.humanize else column[:header].is_a?(Symbol) ? column[:header].to_s.humanize : column[:header].to_s end end end |
#rows ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/spectabular/table.rb', line 36 def rows ordered_hash.tap do |collection_hash| collection.each do |record| column_hash = ordered_hash columns.each_with_index do |column,i| column_hash[headers[i].parameterize] = cell(column,record) || default_empty end collection_hash[record] = column_hash end end end |