simple_table 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ require 'simple_table/tag'
2
+ require 'simple_table/cell'
3
+ require 'simple_table/row'
4
+ require 'simple_table/rows'
5
+ require 'simple_table/body'
6
+ require 'simple_table/head'
7
+ require 'simple_table/foot'
8
+ require 'simple_table/column'
9
+ require 'simple_table/table'
10
+
11
+
12
+ module SimpleTable
13
+ mattr_accessor :options
14
+ self.options = {
15
+ :alternate_rows => true,
16
+ :i18n_scope => nil
17
+ }
18
+
19
+ def table_for(collection = [], options = {}, &block)
20
+ html = Table.new(self, collection, options, &block).render.html_safe
21
+ concat html
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ module SimpleTable
2
+ class Body < Rows
3
+ self.tag_name = :tbody
4
+
5
+ include ActionController::RecordIdentifier
6
+ include ActionView::Helpers::RecordIdentificationHelper
7
+
8
+ def row(options = {}, &block)
9
+ table.collection.each_with_index do |record, ix|
10
+ super(record, options_for_record(record, ix, options))
11
+ end
12
+ end
13
+
14
+ protected
15
+
16
+ def build
17
+ row do |row, record|
18
+ row.cell *table.columns.map { |column| record.send(column.attribute_name) }
19
+ end if @rows.empty?
20
+ end
21
+
22
+ def options_for_record(record, ix, options = {})
23
+ options = options.dup
24
+ options[:id] = dom_id(record) if record.respond_to?(:new_record?)
25
+ add_class!(options, 'alternate') if ix % 2 == 1
26
+ options
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ module SimpleTable
2
+ class Cell < Tag
3
+ self.level = 3
4
+
5
+ attr_reader :content
6
+
7
+ def initialize(parent, content = nil, options = {})
8
+ super(parent, options)
9
+ @content = content
10
+ options[:colspan] = table.columns.size if options[:colspan] == :all
11
+ end
12
+
13
+ def render
14
+ super(content, true)
15
+ end
16
+
17
+ def tag_name
18
+ parent.head? ? :th : :td
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ module SimpleTable
2
+ class Column
3
+ attr_reader :name, :options
4
+
5
+ def initialize(table, name, options = {})
6
+ @table = table
7
+ @name = name
8
+ @value = options.delete(:value)
9
+ @options = options.dup || {}
10
+ @options[:class] ||= name
11
+ end
12
+
13
+ def content
14
+ name.is_a?(Symbol) ? translate(name) : name
15
+ end
16
+
17
+ def translate(content)
18
+ scope = [SimpleTable.options[:i18n_scope], @table.collection_name, :columns].compact
19
+ I18n.t(content, :scope => scope)
20
+ end
21
+
22
+ def attribute_name
23
+ name.to_s.underscore
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,6 @@
1
+ module SimpleTable
2
+ class Foot < Rows
3
+ self.level = 1
4
+ self.tag_name = :tfoot
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ module SimpleTable
2
+ class Head < Rows
3
+ self.level = 1
4
+ self.tag_name = :thead
5
+
6
+ protected
7
+
8
+ def build
9
+ row = Row.new(self, options)
10
+ table.columns.each do |column|
11
+ row.cell(column.content, column.options.reverse_merge(:scope => 'col'))
12
+ end
13
+ @rows << row
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ module SimpleTable
2
+ class Row < Tag
3
+ self.level = 2
4
+ self.tag_name = :tr
5
+
6
+ def initialize(parent, record = nil, options = {}, &block)
7
+ super(parent, options)
8
+
9
+ @parent = parent
10
+ @cells = []
11
+ @block = block
12
+
13
+ yield(*[self, record].compact) if block_given?
14
+ end
15
+
16
+ def cell(*contents)
17
+ options = contents.last.is_a?(Hash) ? contents.pop : {}
18
+ add_class!(options, current_column_class) if parent.is_a?(Body)
19
+ contents.each do |content|
20
+ @cells << Cell.new(self, content, options)
21
+ end
22
+ end
23
+
24
+ def render
25
+ super(@cells.map(&:render))
26
+ end
27
+
28
+ protected
29
+
30
+ def alternate(options)
31
+ options[:class] ||= ''
32
+ options[:class] = options[:class].split(' ').push('alternate').join(' ')
33
+ end
34
+
35
+ def current_column_class
36
+ column = table.columns[@cells.size]
37
+ column && column.options[:class] || ''
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ module SimpleTable
2
+ class Rows < Tag
3
+ self.level = 1
4
+
5
+ attr_reader :rows
6
+
7
+ def initialize(parent, options = {})
8
+ super
9
+ @rows = []
10
+ end
11
+
12
+ def empty?
13
+ @rows.empty?
14
+ end
15
+
16
+ def row(*args, &block)
17
+ options = args.extract_options!
18
+ @rows << Row.new(self, args.shift, options, &block)
19
+ end
20
+
21
+ def render
22
+ build if respond_to?(:build)
23
+ super(@rows.map(&:render))
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,75 @@
1
+ module SimpleTable
2
+ class Table < Tag
3
+ self.level = 0
4
+ self.tag_name = :table
5
+
6
+ attr_reader :body, :head, :foot, :collection, :columns
7
+
8
+ def initialize(view = nil, collection = [], options = {})
9
+ @view = view
10
+ @collection = collection
11
+ @columns = []
12
+ @collection_name = options.delete(:collection_name)
13
+
14
+ super(nil, options.reverse_merge(:id => collection_name, :class => "#{collection_name} list"))
15
+
16
+ yield(self) if block_given?
17
+ end
18
+
19
+ ['head', 'body', 'foot'].each do |name|
20
+ class_eval <<-code
21
+ def #{name} # def head
22
+ @#{name} ||= #{name.classify}.new(self) # @head ||= Head.new(self)
23
+ end # end
24
+ code
25
+ end
26
+
27
+ def column(*names)
28
+ options = names.last.is_a?(Hash) ? names.pop : {}
29
+ names.each do |name|
30
+ @columns << Column.new(self, name, options)
31
+ end
32
+ end
33
+
34
+ def empty(*args, &block)
35
+ @empty = (args << block).compact
36
+ end
37
+
38
+ def row(*args, &block)
39
+ body.row(*args, &block)
40
+ end
41
+
42
+ def collection_class
43
+ # @collection.first.class.base_class
44
+ @collection.first.class
45
+ end
46
+
47
+ def collection_name
48
+ @collection_name ||= collection_class.name.tableize.gsub('/', '_').gsub('rails_', '')
49
+ end
50
+
51
+ def render
52
+ (@collection.empty? && @empty) ? render_empty : begin
53
+ column(*collection_attribute_names) if @columns.empty?
54
+ super do |html|
55
+ html << head.render
56
+ html << body.render
57
+ html << foot.render if @foot && !@foot.empty?
58
+ end.gsub(/\n\s*\n/, "\n")
59
+ end
60
+ end
61
+
62
+ def render_empty
63
+ @empty.insert(1, @empty.pop.call) if @empty.last.respond_to?(:call)
64
+ content_tag(*@empty)
65
+ end
66
+
67
+ protected
68
+
69
+ def collection_attribute_names
70
+ record = @collection.first
71
+ names = record.respond_to?(:attribute_names) ? record.attribute_names : []
72
+ names.map(&:titleize)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,56 @@
1
+ module SimpleTable
2
+ class Tag
3
+ class_inheritable_accessor :level, :tag_name
4
+
5
+ include ActionView::Helpers::TagHelper
6
+
7
+ attr_reader :options, :parent
8
+
9
+ def initialize(parent = nil, options = {})
10
+ @parent = parent
11
+ @options = options
12
+ end
13
+
14
+ def collection_class
15
+ table.collection_class
16
+ end
17
+
18
+ def collection_name
19
+ table.collection_name
20
+ end
21
+
22
+ def table
23
+ is_a?(Table) ? self : parent.try(:table)
24
+ end
25
+
26
+ def head?
27
+ is_a?(Head) || !!parent.try(:head?)
28
+ end
29
+
30
+ def render(content = nil, escape = false)
31
+ yield(content = '') if content.nil? && block_given?
32
+ content = lf(indent(content.to_s))
33
+ lf(content_tag(tag_name, content, options, escape))
34
+ end
35
+
36
+ def add_class(klass)
37
+ add_class!(options, klass)
38
+ end
39
+
40
+ protected
41
+ def lf(str)
42
+ "\n#{str}\n"
43
+ end
44
+
45
+ def indent(str)
46
+ str.gsub(/^/, " ")
47
+ end
48
+
49
+ def add_class!(options, klass)
50
+ unless klass.blank?
51
+ options[:class] ||= ''
52
+ options[:class] = options[:class].split(' ').push(klass).join(' ')
53
+ end
54
+ end
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_table
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sven Fuchs
14
+ - Raphaela Wrede
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-07-31 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: "[description]"
24
+ email: raphaela.wrede@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/simple_table/body.rb
33
+ - lib/simple_table/cell.rb
34
+ - lib/simple_table/column.rb
35
+ - lib/simple_table/foot.rb
36
+ - lib/simple_table/head.rb
37
+ - lib/simple_table/row.rb
38
+ - lib/simple_table/rows.rb
39
+ - lib/simple_table/table.rb
40
+ - lib/simple_table/tag.rb
41
+ - lib/simple_table.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/rwrede/simple_table
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project: "[none]"
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: "[summary]"
76
+ test_files: []
77
+