Class: SimpleTable::Table

Inherits:
Tag
  • Object
show all
Defined in:
lib/simple_table/table.rb

Instance Attribute Summary collapse

Attributes inherited from Tag

#options, #parent

Instance Method Summary collapse

Methods inherited from Tag

#add_class, #head?, #table

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 = [], options = {})
  @view = view
  @collection = collection
  @columns = []
  @collection_name = options.delete(:collection_name).to_s if options.key?(:collection_name)

  super(nil, options.reverse_merge(:id => collection_name, :class => "#{collection_name} list"))
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/simple_table/table.rb', line 6

def body
  @body
end

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/simple_table/table.rb', line 6

def collection
  @collection
end

#columnsObject (readonly)

Returns the value of attribute columns.



6
7
8
# File 'lib/simple_table/table.rb', line 6

def columns
  @columns
end

#footObject (readonly)

Returns the value of attribute foot.



6
7
8
# File 'lib/simple_table/table.rb', line 6

def foot
  @foot
end

#headObject (readonly)

Returns the value of attribute head.



6
7
8
# File 'lib/simple_table/table.rb', line 6

def head
  @head
end

#viewObject (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_classObject



44
45
46
# File 'lib/simple_table/table.rb', line 44

def collection_class
  collection.first.class
end

#collection_nameObject



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)
  options = names.last.is_a?(Hash) ? names.pop : {}
  names.each { |name| columns << Column.new(self, name, options) }
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

#renderObject



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_emptyObject



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? ? '' : (*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