Class: SimpleTable::Table
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#foot ⇒ Object
readonly
Returns the value of attribute foot.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#view ⇒ Object
readonly
Returns the value of attribute view.
Attributes inherited from Tag
Instance Method Summary collapse
- #collection_class ⇒ Object
- #collection_name ⇒ Object
- #column(*names) ⇒ Object
-
#empty(*args, &block) ⇒ Object
Usage: table_for collection do |t| t.empty :div, :class => ‘no-items’ do “everything sold out, mam” end end.
-
#initialize(view = nil, collection = [], options = {}) ⇒ Table
constructor
A new instance of Table.
- #render ⇒ Object
- #render_empty ⇒ Object
- #row(*args, &block) ⇒ Object
Methods inherited from Tag
Constructor Details
#initialize(view = nil, collection = [], options = {}) ⇒ Table
Returns a new instance of Table.
8 9 10 11 12 13 14 15 |
# File 'lib/simple_table/table.rb', line 8 def initialize(view = nil, collection = [], = {}) @view = view @collection = collection @columns = [] @collection_name = .delete(:collection_name).to_s if .key?(:collection_name) super(nil, .reverse_merge(:id => collection_name, :class => "#{collection_name} list")) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'lib/simple_table/table.rb', line 6 def body @body end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
6 7 8 |
# File 'lib/simple_table/table.rb', line 6 def collection @collection end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
6 7 8 |
# File 'lib/simple_table/table.rb', line 6 def columns @columns end |
#foot ⇒ Object (readonly)
Returns the value of attribute foot.
6 7 8 |
# File 'lib/simple_table/table.rb', line 6 def foot @foot end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
6 7 8 |
# File 'lib/simple_table/table.rb', line 6 def head @head end |
#view ⇒ Object (readonly)
Returns the value of attribute view.
6 7 8 |
# File 'lib/simple_table/table.rb', line 6 def view @view end |
Instance Method Details
#collection_class ⇒ Object
44 45 46 |
# File 'lib/simple_table/table.rb', line 44 def collection_class collection.first.class end |
#collection_name ⇒ Object
48 49 50 |
# File 'lib/simple_table/table.rb', line 48 def collection_name @collection_name ||= collection_class.name.tableize.gsub('/', '_').gsub('rails_', '') end |
#column(*names) ⇒ Object
25 26 27 28 |
# File 'lib/simple_table/table.rb', line 25 def column(*names) = names.last.is_a?(Hash) ? names.pop : {} names.each { |name| columns << Column.new(self, name, ) } end |
#empty(*args, &block) ⇒ Object
Usage:
table_for collection do |t|
t.empty :div, :class => 'no-items' do
"everything sold out, mam"
end
end
36 37 38 |
# File 'lib/simple_table/table.rb', line 36 def empty(*args, &block) @empty ||= (args << block).compact end |
#render ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/simple_table/table.rb', line 52 def render (collection.empty? && empty) ? render_empty : begin column(*collection_attribute_names) if columns.empty? super do |html| html << head.render html << body.render html << foot.render if foot && !foot.empty? end.gsub(/\n\s*\n/, "\n") end end |
#render_empty ⇒ Object
63 64 65 66 |
# File 'lib/simple_table/table.rb', line 63 def render_empty empty.insert(1, empty.pop.call) if empty.last.respond_to?(:call) empty.empty? ? '' : content_tag(*empty) end |
#row(*args, &block) ⇒ Object
40 41 42 |
# File 'lib/simple_table/table.rb', line 40 def row(*args, &block) body.row(*args, &block) end |