table_helpers 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/table_data.rb ADDED
@@ -0,0 +1,50 @@
1
+ class TableData < ActionView::Base
2
+ @label_size = nil
3
+ @set_label_size = false
4
+
5
+ def initialize(label_size)
6
+ @label_size = label_size
7
+ end
8
+
9
+ def row(label, data, colspan=false)
10
+ unless @set_label_size && @label_size
11
+ unless colspan
12
+ result = content_tag :tr do
13
+ content_tag(:td, label, :class => "label", :width => @label_size) <<
14
+ content_tag(:td, data)
15
+ end
16
+ else
17
+ result = content_tag :tr do
18
+ content_tag(:td, content_tag(:span, label, :class => "label") << data, :colspan => 2, :width => @label_size)
19
+ end
20
+ end
21
+
22
+ @set_label_size = true
23
+ else
24
+ unless colspan
25
+ result = content_tag :tr do
26
+ content_tag(:td, label, :class => "label") <<
27
+ content_tag(:td, data)
28
+ end
29
+ else
30
+ result = content_tag :tr do
31
+ content_tag(:td, content_tag(:span, label, :class => "label") << data, :colspan => 2, :width => @label_size)
32
+ end
33
+ end
34
+ end
35
+
36
+ return result
37
+ end
38
+
39
+ def section(name=nil)
40
+ unless name.nil?
41
+ content_tag :tr do
42
+ content_tag :td, content_tag(:div, name, :class => "section"), :colspan => 2
43
+ end
44
+ else
45
+ content_tag :tr do
46
+ content_tag :td, tag(:hr, :class => "section"), :colspan => 2
47
+ end
48
+ end
49
+ end
50
+ end
data/lib/table_helpers.rb CHANGED
@@ -1 +1,75 @@
1
- # TableHelpers
1
+ module TableHelpers
2
+ def get_table_headers(columns=[], widths={})
3
+ headers = []
4
+
5
+ i = 1
6
+
7
+ for column_name in columns
8
+ if widths.has_key?(i)
9
+ headers.push(content_tag(:th, column_name, :width => widths[i]))
10
+ else
11
+ headers.push(content_tag(:th, column_name))
12
+ end
13
+ i += 1
14
+ end
15
+
16
+ return headers
17
+ end
18
+
19
+ def get_sortable_table_headers(columns=[], widths={})
20
+ headers = []
21
+
22
+ p = params
23
+ i = 1
24
+
25
+ for j in 0 .. columns.length - 1
26
+ column_name = columns[j][0]
27
+ sort_columns = columns[j][1..columns[j].length-1]
28
+
29
+ if params[:order_by] == sort_columns.join(', ')
30
+ link = link_to(column_name, p.merge('order_by' => "#{sort_columns.join(' DESC, ') + ' DESC'}"), :class=> "sortdown")
31
+ elsif params[:order_by] == sort_columns.join(' DESC, ') + ' DESC'
32
+ link = link_to(column_name, p.merge('order_by' => sort_columns.join(', ')), :class=> "sortup")
33
+ else
34
+ link = link_to(column_name, p.merge('order_by' => sort_columns.join(', ')), :class=> "sort-none")
35
+ end
36
+
37
+ if widths.has_key?(i)
38
+ headers.push("<th width=\"%s\">%s</th>" % [widths[i],link])
39
+ else
40
+ headers.push("<th>%s</th>" % [link])
41
+ end
42
+
43
+ i += 1
44
+ j += 1
45
+ end
46
+
47
+ return headers
48
+ end
49
+
50
+ def table_list(html_options = {}, &block)
51
+ raise "need a block" unless block_given?
52
+
53
+ t = TableList.new self
54
+
55
+ result = capture do
56
+ content_tag :table, html_options do
57
+ block.call(t)
58
+ end
59
+ end
60
+
61
+ concat(result)
62
+ end
63
+
64
+ def table_data(options = {}, &block)
65
+ options[:label_size] ||= nil
66
+
67
+ t = TableData.new(options[:label_size])
68
+
69
+ result = content_tag(:table, :class => 'info') do
70
+ block.call(t)
71
+ end
72
+
73
+ concat(result)
74
+ end
75
+ end
data/lib/table_list.rb ADDED
@@ -0,0 +1,27 @@
1
+ class TableList
2
+ def initialize(instance)
3
+ @instance = instance
4
+ end
5
+
6
+ def sortable_headers(columns=[], widths={})
7
+ @instance.content_tag :tr do
8
+ @instance.get_sortable_table_headers(columns, widths)
9
+ end
10
+ end
11
+
12
+ def headers(columns=[], widths={})
13
+ @instance.content_tag :tr do
14
+ @instance.get_table_headers(columns, widths)
15
+ end
16
+ end
17
+
18
+ def td(data)
19
+ @instance.content_tag :td, data
20
+ end
21
+
22
+ def tr(&block)
23
+ raise "need a block" unless block_given?
24
+
25
+ @instance.content_tag(:tr, block.call(self))
26
+ end
27
+ end
data/rails/init.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'app/helpers/table_data_helper'
2
- require 'app/helpers/table_list_helper'
1
+ require 'table_helpers'
3
2
 
4
- ActionView::Base.send :include, TableDataHelper
5
- ActionView::Base.send :include, TableListHelper
3
+ ActionView::Base.send :include, TableHelpers
@@ -2,12 +2,12 @@ require 'test_helper'
2
2
  require 'action_view'
3
3
  require 'action_controller'
4
4
  require 'action_view/test_case'
5
- require File.dirname(__FILE__) + '/../lib/app/helpers/table_data_helper.rb'
6
- require File.dirname(__FILE__) + '/../lib/app/helpers/table_list_helper.rb'
5
+ require File.dirname(__FILE__) + '/../lib/table_data.rb'
6
+ require File.dirname(__FILE__) + '/../lib/table_list.rb'
7
+ require File.dirname(__FILE__) + '/../lib/table_helpers.rb'
7
8
 
8
9
  class TableHelpersTest < ActionView::TestCase
9
- include TableDataHelper
10
- include TableListHelper
10
+ include TableHelpers
11
11
 
12
12
  test "table_list_helper_with_headers" do
13
13
  result = capture do
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
5
- prerelease:
4
+ hash: 15
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Damian Ba\xC4\x87kowski"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-02 00:00:00 +01:00
17
+ date: 2011-02-03 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -30,11 +30,11 @@ files:
30
30
  - Rakefile
31
31
  - lib/tasks/table_helpers.rake
32
32
  - lib/table_helpers.rb
33
- - lib/app/helpers/table_data_helper.rb
34
- - lib/app/helpers/table_list_helper.rb
33
+ - lib/table_list.rb
34
+ - lib/table_data.rb
35
35
  - rails/init.rb
36
- - test/test_helper.rb
37
36
  - test/table_helpers_test.rb
37
+ - test/test_helper.rb
38
38
  - README
39
39
  has_rdoc: true
40
40
  homepage: https://github.com/paki-paki/table_helpers
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  requirements: []
67
67
 
68
68
  rubyforge_project:
69
- rubygems_version: 1.4.2
69
+ rubygems_version: 1.3.7
70
70
  signing_key:
71
71
  specification_version: 3
72
72
  summary: Table helpers for Rails
@@ -1,64 +0,0 @@
1
- module TableDataHelper
2
- def table_data(options = {}, &block)
3
- options[:label_size] ||= nil
4
-
5
- t = TableData.new(options[:label_size])
6
-
7
- result = content_tag(:table, :class => 'info') do
8
- block.call(t)
9
- end
10
-
11
- concat(result)
12
- end
13
-
14
- class TableData < ActionView::Base
15
- @label_size = nil
16
- @set_label_size = false
17
-
18
- def initialize(label_size)
19
- @label_size = label_size
20
- end
21
-
22
- def row(label, data, colspan=false)
23
- unless @set_label_size && @label_size
24
- unless colspan
25
- result = content_tag :tr do
26
- content_tag(:td, label, :class => "label", :width => @label_size) <<
27
- content_tag(:td, data)
28
- end
29
- else
30
- result = content_tag :tr do
31
- content_tag(:td, content_tag(:span, label, :class => "label") << data, :colspan => 2, :width => @label_size)
32
- end
33
- end
34
-
35
- @set_label_size = true
36
- else
37
- unless colspan
38
- result = content_tag :tr do
39
- content_tag(:td, label, :class => "label") <<
40
- content_tag(:td, data)
41
- end
42
- else
43
- result = content_tag :tr do
44
- content_tag(:td, content_tag(:span, label, :class => "label") << data, :colspan => 2, :width => @label_size)
45
- end
46
- end
47
- end
48
-
49
- return result
50
- end
51
-
52
- def section(name=nil)
53
- unless name.nil?
54
- content_tag :tr do
55
- content_tag :td, content_tag(:div, name, :class => "section"), :colspan => 2
56
- end
57
- else
58
- content_tag :tr do
59
- content_tag :td, tag(:hr, :class => "section"), :colspan => 2
60
- end
61
- end
62
- end
63
- end
64
- end
@@ -1,83 +0,0 @@
1
- module TableListHelper
2
- def get_table_headers(columns=[], widths={})
3
- headers = []
4
-
5
- i = 1
6
-
7
- for column_name in columns
8
- if widths.has_key?(i)
9
- headers.push(content_tag(:th, column_name, :width => widths[i]))
10
- else
11
- headers.push(content_tag(:th, column_name))
12
- end
13
- i += 1
14
- end
15
-
16
- return headers
17
- end
18
-
19
- def get_sortable_table_headers(columns=[], widths={})
20
- headers = []
21
-
22
- p = params
23
- i = 1
24
-
25
- for j in 0 .. columns.length - 1
26
- column_name = columns[j][0]
27
- sort_columns = columns[j][1..columns[j].length-1]
28
-
29
- if params[:order_by] == sort_columns.join(', ')
30
- link = link_to(column_name, p.merge('order_by' => "#{sort_columns.join(' DESC, ') + ' DESC'}"), :class=> "sortdown")
31
- elsif params[:order_by] == sort_columns.join(' DESC, ') + ' DESC'
32
- link = link_to(column_name, p.merge('order_by' => sort_columns.join(', ')), :class=> "sortup")
33
- else
34
- link = link_to(column_name, p.merge('order_by' => sort_columns.join(', ')), :class=> "sort-none")
35
- end
36
-
37
- if widths.has_key?(i)
38
- headers.push("<th width=\"%s\">%s</th>" % [widths[i],link])
39
- else
40
- headers.push("<th>%s</th>" % [link])
41
- end
42
-
43
- i += 1
44
- j += 1
45
- end
46
-
47
- return headers
48
- end
49
-
50
- def table_list(html_options = {}, &block)
51
- raise "need a block" unless block_given?
52
-
53
- result = capture do
54
- content_tag :table, html_options do
55
- block.call(self)
56
- end
57
- end
58
-
59
- concat(result)
60
- end
61
-
62
- def sortable_headers(columns=[], widths={})
63
- content_tag :tr do
64
- get_sortable_table_headers(columns, widths)
65
- end
66
- end
67
-
68
- def headers(columns=[], widths={})
69
- content_tag :tr do
70
- get_table_headers(columns, widths)
71
- end
72
- end
73
-
74
- def td(data)
75
- content_tag :td, data
76
- end
77
-
78
- def tr(&block)
79
- raise "need a block" unless block_given?
80
-
81
- content_tag(:tr, block.call(self))
82
- end
83
- end