table_helpers 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,52 @@
1
+ TableHelpers
2
+ ============
3
+
4
+ Rails table helpers plugin.
5
+
6
+ Example
7
+ =======
8
+
9
+ <% table_list :class => 'list' do |t| %>
10
+ <%= t.headers ['First column', 'Second column'] %>
11
+ <% t.tr do |tr| %>
12
+ <%= tr.td 'Example 1' %>
13
+ <%= tr.td 'Example 2' %>
14
+ <% end %>
15
+ <% end %>
16
+
17
+ will output:
18
+
19
+ <table class="list">
20
+ <tr>
21
+ <th>First column</th>
22
+ <th>Second column</th>
23
+ </tr>
24
+ <tr>
25
+ <td>Example 1</td>
26
+ <td>Example 2</td>
27
+ </tr>
28
+ </table>
29
+
30
+ <% table_data :label_size => 150 do |t| %>
31
+ <% t.row 'Label name:', 'Data' %>
32
+ <% t.section 'Next section:' %>
33
+ <% t.row 'Label name 2:', 'Data 2' %>
34
+ <% end %>
35
+
36
+ will output:
37
+
38
+ <table class="info">
39
+ <tr>
40
+ <td class="label" width="150">Label name:</td>
41
+ <td>Data</td>
42
+ </tr>
43
+ <tr>
44
+ <td colspan="2"><div class="section">Next section:</div></td>
45
+ </tr>
46
+ <tr>
47
+ <td class="label">Label name 2 :</td>
48
+ <td class="label">Data 2:</td>
49
+ </tr>
50
+ </table>
51
+
52
+ Copyright (c) 2011 Damian Baćkowski, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the table_helpers plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the table_helpers plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'TableHelpers'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,64 @@
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
@@ -0,0 +1,83 @@
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
@@ -0,0 +1 @@
1
+ # TableHelpers
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :table_helpers do
3
+ # # Task goes here
4
+ # end
data/rails/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'app/helpers/table_data_helper'
2
+ require 'app/helpers/table_list_helper'
3
+
4
+ ActionView::Base.send :include, TableDataHelper
5
+ ActionView::Base.send :include, TableListHelper
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+ require 'action_view'
3
+ require 'action_controller'
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'
7
+
8
+ class TableHelpersTest < ActionView::TestCase
9
+ include TableDataHelper
10
+ include TableListHelper
11
+
12
+ test "table_list_helper_with_headers" do
13
+ result = capture do
14
+ table_list(:class => 'list') do |t|
15
+ t.headers(['Column 1', 'Column 2']) +
16
+ t.tr do |tr|
17
+ tr.td('Example 1') +
18
+ tr.td('Example 2')
19
+ end
20
+ end
21
+ end
22
+
23
+ expected = <<EOF
24
+ <table class="list">
25
+ <tr>
26
+ <th>Column 1</th>
27
+ <th>Column 2</th>
28
+ </tr>
29
+ <tr>
30
+ <td>Example 1</td>
31
+ <td>Example 2</td>
32
+ </tr>
33
+ </table>
34
+ EOF
35
+
36
+ assert_dom_equal expected, result
37
+ end
38
+
39
+ test "table_data_helper" do
40
+ result = capture do
41
+ table_data(:label_size => 200) do |t|
42
+ t.row('Firm:', 'Test') +
43
+ t.section +
44
+ t.row('Client Name:', 'Bill Gates')
45
+ end
46
+ end
47
+
48
+ expected = <<EOF
49
+ <table class="info">
50
+ <tr>
51
+ <td class="label" width="200">Firm:</td>
52
+ <td>Test</td>
53
+ </tr>
54
+ <tr>
55
+ <td colspan="2"><hr class="section" /></td>
56
+ </tr>
57
+ <tr>
58
+ <td class="label">Client Name:</td>
59
+ <td>Bill Gates</td>
60
+ </tr>
61
+ </table>
62
+ EOF
63
+
64
+ assert_dom_equal expected, result
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_helpers
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - "Damian Ba\xC4\x87kowski"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-02 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: The table_helpers library provides a simple helpers to create table lists with headers and presenting data in tables.
22
+ email: damianbackowski@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - Rakefile
31
+ - lib/tasks/table_helpers.rake
32
+ - lib/table_helpers.rb
33
+ - lib/app/helpers/table_data_helper.rb
34
+ - lib/app/helpers/table_list_helper.rb
35
+ - rails/init.rb
36
+ - test/test_helper.rb
37
+ - test/table_helpers_test.rb
38
+ - README
39
+ has_rdoc: true
40
+ homepage: https://github.com/paki-paki/table_helpers
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.4.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Table helpers for Rails
73
+ test_files: []
74
+