Class: Table

Inherits:
ActiveComponent::Base show all
Defined in:
lib/active_component/components/table.rb

Constant Summary

Constants included from ActiveComponent

ActiveComponent::BLOCK_ELEMENTS, ActiveComponent::EMPTY_ELEMENTS, ActiveComponent::HEADING_ELEMENTS, ActiveComponent::HTML5_ELEMENTS, ActiveComponent::PHRASING_ELEMENTS, ActiveComponent::SECTION_ELEMENTS

Instance Attribute Summary collapse

Attributes inherited from ActiveComponent::Base

#attributes, #childrenHash, #node_content, #node_name, #parent, #title

Instance Method Summary collapse

Methods inherited from ActiveComponent::Base

#<<, #<=>, #[], #add, #breadth, #breadth_each, #children, #class_name, #content, #content=, def_component_helper, def_html_sub_components, #depth, #detached_copy, #each, #each_leaf, #firstChild, #firstSibling, #freezeTree!, #hasChildren?, #hasnode_content?, #html_class, #in_degree, inherited, #init_component, #init_node, #isFirstSibling?, #isLastSibling?, #isLeaf?, #isOnlyChild?, #is_html_tag_wrapper?, #is_root?, json_create, #lastChild, #lastSibling, #length, #marshal_dump, #marshal_load, #nextSibling, #nodeDepth, #nodeHeight, #out_degree, #parentage, #preordered_each, #prepend, #previousSibling, #printTree, #remove!, #removeAll!, #removeFromParent!, #root, #siblings, #size, #to_json, #to_s

Methods included from Enumerable

#find_a, #includes_a?, #transmogrify

Methods included from ActiveComponent

#print_contents, #print_object, #print_tag, #wrap_contents

Constructor Details

#initialize(*args) ⇒ Table

Returns a new instance of Table.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_component/components/table.rb', line 7

def initialize(*args) 
  init_component(args, [:content, :title, :cols, :headers, :attributes, :row_attrs, :header_attrs, :field_attrs])

  # Defaults
  if @title.nil?
    @title                    = content.first.class.to_s.hyphenize.pluralize
    @attributes[:class].to_a.unshift @title
  end
  if @cols.nil? && content.first.respond_to?(:attributes)
    @cols                     = content.first.attributes.keys
    @headers                ||= @cols.collect {|col| col.to_s.humanize}
  end
  @attributes[:cellspacing] ||= 0
  @row_attrs                ||= {}
  @header_attrs             ||= {}
  @field_attrs              ||= {}
end

Instance Attribute Details

#colsObject

Returns the value of attribute cols.



5
6
7
# File 'lib/active_component/components/table.rb', line 5

def cols
  @cols
end

#field_attrsObject

Returns the value of attribute field_attrs.



5
6
7
# File 'lib/active_component/components/table.rb', line 5

def field_attrs
  @field_attrs
end

#header_attrsObject

Returns the value of attribute header_attrs.



5
6
7
# File 'lib/active_component/components/table.rb', line 5

def header_attrs
  @header_attrs
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/active_component/components/table.rb', line 5

def headers
  @headers
end

#row_attrsObject

Returns the value of attribute row_attrs.



5
6
7
# File 'lib/active_component/components/table.rb', line 5

def row_attrs
  @row_attrs
end

Instance Method Details

#to_htmlObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_component/components/table.rb', line 25

def to_html
  print_buffer do
    tag_to_buffer :table, @attributes do
      row_count = 0
      unless @headers.blank?
        tag_to_buffer :tr, get_row_attrs(row_count) do
          @headers.each_with_index do |header, i|
            tag_to_buffer :th, header, get_header_attrs(i)
          end
          row_count = 1
        end
      end
      content.each_with_index do |row, i|
        unless row.blank?
          tag_to_buffer :tr, get_row_attrs(row_count + i) do
            print_contents(:td, row, @cols, @field_attrs)
          end
        end
      end
    end
  end
end