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 = {}) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
13
14
15
16
17
# 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)

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

  yield(self) if block_given?
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

Instance Method Details

#collection_classObject



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

def collection_class
  # @collection.first.class.base_class
  @collection.first.class
end

#collection_nameObject



47
48
49
# File 'lib/simple_table/table.rb', line 47

def collection_name
  @collection_name ||= collection_class.name.tableize.gsub('/', '_').gsub('rails_', '')
end

#column(*names) ⇒ Object



27
28
29
30
31
32
# File 'lib/simple_table/table.rb', line 27

def column(*names)
  options = names.last.is_a?(Hash) ? names.pop : {}
  names.each do |name|
    @columns << Column.new(self, name, options)
  end
end

#empty(*args, &block) ⇒ Object



34
35
36
# File 'lib/simple_table/table.rb', line 34

def empty(*args, &block)
  @empty = (args << block).compact
end

#renderObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/simple_table/table.rb', line 51

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



62
63
64
65
# File 'lib/simple_table/table.rb', line 62

def render_empty
  @empty.insert(1, @empty.pop.call) if @empty.last.respond_to?(:call)
  (*@empty)
end

#row(*args, &block) ⇒ Object



38
39
40
# File 'lib/simple_table/table.rb', line 38

def row(*args, &block)
  body.row(*args, &block)
end