tableasy 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/VERSION +1 -1
- data/lib/formatters.rb +22 -17
- data/lib/tableasy.rb +3 -3
- data/lib/tableasy/formatter.rb +21 -13
- data/lib/tableasy/html_attributes.rb +0 -7
- data/lib/tableasy/table.rb +13 -0
- data/lib/tableasy/table/cell.rb +29 -0
- data/lib/tableasy/table/row.rb +22 -0
- data/lib/tableasy/tables_helper.rb +45 -33
- data/spec/helpers/formatting_helper_spec.rb +23 -15
- data/spec/helpers/tables_helper_spec.rb +18 -11
- data/spec/table/cell_spec.rb +43 -0
- data/spec/table/row_spec.rb +25 -0
- data/spec/table/table_spec.rb +21 -0
- data/tableasy.gemspec +11 -4
- metadata +11 -4
- data/lib/tableasy/column.rb +0 -17
- data/lib/tableasy/row.rb +0 -20
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/formatters.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
-
formatter(:linked) do |
|
2
|
-
link_to(
|
1
|
+
formatter(:linked) do |cell|
|
2
|
+
cell.value = link_to(cell.value, cell.subject)
|
3
3
|
end
|
4
4
|
|
5
|
-
formatter(:linked_to) do |
|
6
|
-
|
7
|
-
object ? link_to(object, object) : ''
|
5
|
+
formatter(:linked_to) do |cell|
|
6
|
+
cell.value = cell.value ? link_to(cell.value, cell.value) : ''
|
8
7
|
end
|
9
8
|
|
10
|
-
formatter(:tail_link, :no_header => true) do |
|
9
|
+
formatter(:tail_link, :no_header => true, :no_initial => true) do |cell, text, *args|
|
11
10
|
html_options = args.extract_options!.clone
|
12
|
-
url = args + [subject]
|
11
|
+
url = args + [cell.subject]
|
13
12
|
|
14
|
-
if html_options.delete(:ajax)
|
13
|
+
cell.value = if html_options.delete(:ajax)
|
15
14
|
options = {:method => html_options.delete(:method), :confirm => html_options.delete(:confirm), :url => url}
|
16
15
|
link_to_remote text, options, html_options
|
17
16
|
else
|
@@ -19,24 +18,30 @@ formatter(:tail_link, :no_header => true) do |subject, text, *args|
|
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
22
|
-
formatter(:edit_link) do |
|
21
|
+
formatter(:edit_link, :no_initial => true, :no_header => true) do |cell, text, options|
|
23
22
|
options ||= {}
|
24
23
|
options[:method] = 'get' if options[:ajax]
|
25
|
-
tail_link(text, :edit, options).execute(
|
24
|
+
tail_link(text, :edit, options).execute(cell)
|
26
25
|
end
|
27
26
|
|
28
|
-
formatter(:destroy_link) do |
|
27
|
+
formatter(:destroy_link, :no_initial => true, :no_header => true) do |cell, text, options|
|
29
28
|
options ||= {}
|
30
29
|
options.reverse_merge!(:confirm => 'Are you sure?', :method => 'delete')
|
31
|
-
tail_link(text, options).execute(
|
30
|
+
tail_link(text, options).execute(cell)
|
32
31
|
end
|
33
32
|
|
34
|
-
formatter(:with_percent) do |
|
35
|
-
number, total =
|
33
|
+
formatter(:with_percent) do |cell, total_column|
|
34
|
+
number, total = cell.value.to_i, cell.subject.send(total_column).to_i
|
36
35
|
percent = total == 0 ? 0 : number.to_f / total * 100
|
37
|
-
"#{number} (#{
|
36
|
+
cell.value = "#{number} (#{number_to_percentage(percent)})"
|
38
37
|
end
|
39
38
|
|
40
|
-
formatter(:random) do |
|
41
|
-
rand(range.last - range.first) + range.first
|
39
|
+
formatter(:random, :no_initial => true) do |cell, header, range|
|
40
|
+
cell.value = rand(range.last - range.first) + range.first
|
41
|
+
end
|
42
|
+
|
43
|
+
formatter(:header_row, :no_header => true, :no_initial => true) do |cell, text|
|
44
|
+
cell.value = text
|
45
|
+
cell.html[:colspan] = 2
|
46
|
+
cell.header = true
|
42
47
|
end
|
data/lib/tableasy.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'action_view'
|
3
3
|
require 'action_controller'
|
4
|
-
%w{html_attributes
|
4
|
+
%w{html_attributes table/cell table/row total formatter tables_helper table}.each {|f| require File.dirname(__FILE__) + "/tableasy/#{f}" }
|
5
5
|
|
6
6
|
module Tableasy
|
7
7
|
module FormattersHelper
|
@@ -10,11 +10,11 @@ module Tableasy
|
|
10
10
|
class FormattersContext
|
11
11
|
def self.formatter(name, options = {}, &block)
|
12
12
|
Formatter.new(&block).tap do |formatter|
|
13
|
-
formatter.format_header { nil } if options
|
13
|
+
formatter.format_header { nil } if options.delete(:no_header)
|
14
14
|
|
15
15
|
FormattersHelper.module_eval do
|
16
16
|
define_method(name) do |column, *args|
|
17
|
-
|
17
|
+
Formatter::Column.new(self, formatter, column, *args.push(options))
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
data/lib/tableasy/formatter.rb
CHANGED
@@ -1,21 +1,29 @@
|
|
1
1
|
module Tableasy
|
2
|
-
class
|
3
|
-
|
4
|
-
|
5
|
-
@formatter = formatter
|
6
|
-
@args = args
|
7
|
-
end
|
2
|
+
class Formatter
|
3
|
+
class Column
|
4
|
+
attr_reader :column
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
def initialize(context, formatter, column, *args)
|
7
|
+
options = args.extract_options!
|
8
|
+
@context = context
|
9
|
+
@formatter = formatter
|
10
|
+
@args = args
|
11
|
+
if options[:no_initial]
|
12
|
+
@args.unshift(column)
|
13
|
+
else
|
14
|
+
@column = column
|
15
|
+
end
|
16
|
+
end
|
12
17
|
|
13
|
-
|
14
|
-
|
18
|
+
def execute(record)
|
19
|
+
@context.instance_exec(record, *@args, &@formatter.block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_sym
|
23
|
+
@formatter.format_header(@column || @args.first)
|
24
|
+
end
|
15
25
|
end
|
16
|
-
end
|
17
26
|
|
18
|
-
class Formatter
|
19
27
|
attr_reader :block
|
20
28
|
|
21
29
|
def initialize(&block)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Tableasy
|
2
|
+
class Table
|
3
|
+
class Cell
|
4
|
+
include HtmlAttributes
|
5
|
+
attr_accessor :value, :header
|
6
|
+
attr_reader :subject
|
7
|
+
|
8
|
+
def initialize(subject, value, header = false)
|
9
|
+
@subject = subject
|
10
|
+
@header = header
|
11
|
+
self.value = value
|
12
|
+
end
|
13
|
+
|
14
|
+
def value=(value)
|
15
|
+
if value.is_a?(Tableasy::Formatter::Column)
|
16
|
+
@value = @subject.send(value.column) if value.column
|
17
|
+
value.execute(self)
|
18
|
+
else
|
19
|
+
value = @subject.send(value) if value.is_a?(Symbol)
|
20
|
+
@value = value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def tag
|
25
|
+
@header ? 'th' : 'td'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Tableasy
|
2
|
+
class Table
|
3
|
+
class Row
|
4
|
+
include HtmlAttributes
|
5
|
+
attr_reader :cells
|
6
|
+
|
7
|
+
def initialize(cells, html = {})
|
8
|
+
@cells = cells.select {|cell| cell.value }
|
9
|
+
@total, @header = html.delete(:total), html.delete(:header)
|
10
|
+
@html = html
|
11
|
+
end
|
12
|
+
|
13
|
+
def total_row?
|
14
|
+
@total
|
15
|
+
end
|
16
|
+
|
17
|
+
def header_row?
|
18
|
+
@header
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -3,45 +3,57 @@ module Tableasy
|
|
3
3
|
def table_for(klass, list, *columns)
|
4
4
|
options = columns.extract_options!
|
5
5
|
|
6
|
+
table = Table.new
|
7
|
+
# Add header row
|
8
|
+
row = columns.collect {|column| header_cell(column, klass) }
|
9
|
+
table.add_row(row, :header => true)
|
10
|
+
|
11
|
+
list.each_with_index do |object, index|
|
12
|
+
table.add_row(table_row(object, columns), :id => dom_id(object, :row), :class => "#{dom_class(object)} #{index.even? ? "odd" : "even"}")
|
13
|
+
end
|
14
|
+
|
15
|
+
if options[:total]
|
16
|
+
item = Total.new(list)
|
17
|
+
caption = I18n.t('tableasy.total', :raise => true) rescue 'Total: '
|
18
|
+
table.add_row(table_row(item, columns[1..-1]).unshift(Table::Cell.new(item, caption, true)), :class => 'total-row', :total => true)
|
19
|
+
end
|
20
|
+
|
21
|
+
content_tag('table', options[:html]) do
|
22
|
+
table.rows[1..-1].each {|row| yield row } if block_given?
|
23
|
+
table.rows.collect {|row| content_row(row) }.join
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def data_list(object, *columns)
|
28
|
+
options = columns.extract_options!
|
29
|
+
table = Table.new
|
30
|
+
|
31
|
+
columns.each do |column|
|
32
|
+
table.add_row([header_cell(column, object.class), Table::Cell.new(object, column)].compact)
|
33
|
+
end
|
34
|
+
|
6
35
|
content_tag('table', options[:html]) do
|
7
|
-
|
8
|
-
content << content_tag('tr') do
|
9
|
-
columns.select {|column| column.to_sym }.collect do |column|
|
10
|
-
content_tag 'th', klass.human_attribute_name(column.to_sym)
|
11
|
-
end.join
|
12
|
-
end
|
13
|
-
|
14
|
-
content << list.each_with_index.collect do |item, index|
|
15
|
-
row = Row.new(item, columns)
|
16
|
-
row.html[:class] = index.odd? ? 'even' : 'odd'
|
17
|
-
yield row if block_given?
|
18
|
-
content_row(item, row)
|
19
|
-
end.join
|
20
|
-
|
21
|
-
if options[:total]
|
22
|
-
item = Total.new(list)
|
23
|
-
caption = I18n.t('tableasy.total', :raise => true) rescue 'Total: '
|
24
|
-
row = Row.new(item, columns[1..-1].unshift(caption), true)
|
25
|
-
row.html[:class] = 'total-row'
|
26
|
-
yield row if block_given?
|
27
|
-
content << content_row(item, row, :first_header => true)
|
28
|
-
end
|
29
|
-
|
30
|
-
content
|
36
|
+
table.rows.collect {|row| content_row(row) }.join
|
31
37
|
end
|
32
38
|
end
|
33
39
|
|
34
|
-
def content_row(
|
35
|
-
|
36
|
-
|
37
|
-
if options[:first_header]
|
38
|
-
column = row.columns.shift
|
39
|
-
result << content_tag('th', column.value, column.html)
|
40
|
-
end
|
41
|
-
result << row.columns.collect { |column| content_tag('td', column.value, column.html) }.join
|
42
|
-
end
|
40
|
+
def content_row(row)
|
41
|
+
content_tag('tr', row.html) do
|
42
|
+
row.cells.collect {|cell| content_tag(cell.tag, cell.value, cell.html) }.join
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
protected
|
47
|
+
|
48
|
+
def header_cell(column, klass)
|
49
|
+
header = column.to_sym
|
50
|
+
Table::Cell.new(nil, klass.human_attribute_name(header), true) if header
|
51
|
+
end
|
52
|
+
|
53
|
+
def table_row(object, columns)
|
54
|
+
columns.collect do |column|
|
55
|
+
Table::Cell.new(object, column)
|
56
|
+
end
|
57
|
+
end
|
46
58
|
end
|
47
59
|
end
|
@@ -8,14 +8,14 @@ describe 'Formatters' do
|
|
8
8
|
it "should show linked object" do
|
9
9
|
formatter = helper.linked(:name)
|
10
10
|
formatter.to_sym.should == :name
|
11
|
-
|
11
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == '<a href="/people/Andrius">Andrius</a>'
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should show object linked to itself" do
|
15
15
|
build :project
|
16
16
|
formatter = helper.linked_to(:leader)
|
17
17
|
formatter.to_sym.should == :leader
|
18
|
-
|
18
|
+
Tableasy::Table::Cell.new(@project, formatter).value.should == '<a href="/people/Andrius">Andrius</a>'
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should show nothing when linked object doesn't exist" do
|
@@ -23,7 +23,7 @@ describe 'Formatters' do
|
|
23
23
|
@project.leader = nil
|
24
24
|
formatter = helper.linked_to(:leader)
|
25
25
|
formatter.to_sym.should == :leader
|
26
|
-
|
26
|
+
Tableasy::Table::Cell.new(@project, formatter).value.should == ''
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should show number with percent" do
|
@@ -33,7 +33,7 @@ describe 'Formatters' do
|
|
33
33
|
formatter = helper.with_percent(:remaining, :work_hours)
|
34
34
|
formatter.to_sym.should == :remaining
|
35
35
|
["8 (8.000%)", "-8 (-8.000%)", "8 (61.538%)", "0 (0.000%)"].each do |result|
|
36
|
-
|
36
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == result
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -41,7 +41,7 @@ describe 'Formatters' do
|
|
41
41
|
helper.expects(:rand).with(30).returns(23)
|
42
42
|
formatter = helper.random(:hours, 100..130)
|
43
43
|
formatter.to_sym.should == :hours
|
44
|
-
|
44
|
+
Tableasy::Table::Cell.new(nil, formatter).value.should == 123
|
45
45
|
end
|
46
46
|
|
47
47
|
describe 'tail_link' do
|
@@ -51,38 +51,46 @@ describe 'Formatters' do
|
|
51
51
|
|
52
52
|
it "should allow show link with no symbol" do
|
53
53
|
formatter = helper.tail_link('hello')
|
54
|
-
|
54
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == '<a href="/people/Andrius">hello</a>'
|
55
55
|
formatter.to_sym.should == nil
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should allow passing custom url" do
|
59
59
|
formatter = helper.tail_link('hello', :edit)
|
60
|
-
|
60
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == '<a href="/edit/people/Andrius">hello</a>'
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should allow passing custom url and html attributes" do
|
64
64
|
formatter = helper.tail_link('hello', :edit, :method => :delete)
|
65
|
-
|
65
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == helper.link_to('hello', [:edit, @andrius], :method => :delete)
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should allow creating ajax link" do
|
69
69
|
formatter = helper.tail_link('hello', :edit, :ajax => true)
|
70
|
-
|
70
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == helper.link_to_remote('hello', :url => [:edit, @andrius])
|
71
71
|
end
|
72
72
|
|
73
73
|
it "should allow creating edit url" do
|
74
|
-
formatter = helper.edit_link('
|
75
|
-
|
74
|
+
formatter = helper.edit_link('(edit)')
|
75
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == helper.link_to('(edit)', [:edit, @andrius])
|
76
76
|
end
|
77
77
|
|
78
78
|
it "should allow creating ajax edit url" do
|
79
|
-
formatter = helper.edit_link('
|
80
|
-
|
79
|
+
formatter = helper.edit_link('(edit)', :ajax => true)
|
80
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == helper.link_to_remote('(edit)', :url => [:edit, @andrius], :method => :get)
|
81
81
|
end
|
82
82
|
|
83
83
|
it "should allow creating delete link" do
|
84
|
-
formatter = helper.destroy_link('
|
85
|
-
|
84
|
+
formatter = helper.destroy_link('(delete)')
|
85
|
+
Tableasy::Table::Cell.new(@andrius, formatter).value.should == helper.link_to('(delete)', @andrius, :method => :delete, :confirm => 'Are you sure?')
|
86
86
|
end
|
87
87
|
end
|
88
|
+
|
89
|
+
it "should allow creating header cell" do
|
90
|
+
formatter = helper.header_row('Hello')
|
91
|
+
cell = Tableasy::Table::Cell.new(@andrius, formatter)
|
92
|
+
cell.value.should == 'Hello'
|
93
|
+
cell.html[:colspan].should == 2
|
94
|
+
cell.header.should == true
|
95
|
+
end
|
88
96
|
end
|
@@ -11,8 +11,8 @@ describe Tableasy::TablesHelper do
|
|
11
11
|
it "should yield row object with columns and record when creating table using block" do
|
12
12
|
build :project
|
13
13
|
output = helper.table_for(Project, [@project], :name, :leader) do |row|
|
14
|
-
row.
|
15
|
-
row.
|
14
|
+
row.cells[0].value = row.cells[0].subject.name
|
15
|
+
row.cells[1].value = 'xxxxx'
|
16
16
|
end
|
17
17
|
|
18
18
|
output.should == "<table><tr><th>Name</th><th>Leader</th></tr><tr class=\"project odd\" id=\"row_project_1\"><td>project</td><td>xxxxx</td></tr></table>"
|
@@ -21,13 +21,11 @@ describe Tableasy::TablesHelper do
|
|
21
21
|
it "should allow passing custom html options for row and columns" do
|
22
22
|
build :project
|
23
23
|
output = helper.table_for(Project, [@project], :name) do |row|
|
24
|
-
row.title = 'my row'
|
25
24
|
row.html[:class] = 'my-row'
|
26
|
-
row.
|
27
|
-
row.columns[0].html[:class] = 'name'
|
25
|
+
row.cells[0].html[:class] = 'name'
|
28
26
|
end
|
29
27
|
|
30
|
-
output.should == "<table><tr><th>Name</th></tr><tr class=\"
|
28
|
+
output.should == "<table><tr><th>Name</th></tr><tr class=\"my-row\" id=\"row_project_1\"><td class=\"name\">project</td></tr></table>"
|
31
29
|
end
|
32
30
|
|
33
31
|
it "should allow creating a table with totals row" do
|
@@ -38,22 +36,22 @@ describe Tableasy::TablesHelper do
|
|
38
36
|
<tr><th>Name</th><th>Id</th></tr>\
|
39
37
|
<tr class=\"person odd\" id=\"row_person_1\"><td>Admin</td><td>1</td></tr>\
|
40
38
|
<tr class=\"person even\" id=\"row_person_1\"><td>Andrius</td><td>1</td></tr>\
|
41
|
-
<tr class=\"
|
39
|
+
<tr class=\"total-row\"><th>Total: </th><td>2</td></tr>\
|
42
40
|
</table>"
|
43
41
|
end
|
44
42
|
|
45
43
|
it "should allow creating custom total row" do
|
46
44
|
build :admin, :andrius
|
47
45
|
output = helper.table_for(Person, [@admin, @andrius], :name, :id, :total => true) do |row|
|
48
|
-
row.
|
49
|
-
row.
|
46
|
+
row.cells[0].value = 'Total of:' if row.total_row?
|
47
|
+
row.cells[1].value = "@#{row.cells[1].subject.id}"
|
50
48
|
end
|
51
49
|
|
52
50
|
output.should == "<table>\
|
53
51
|
<tr><th>Name</th><th>Id</th></tr>\
|
54
52
|
<tr class=\"person odd\" id=\"row_person_1\"><td>Admin</td><td>@1</td></tr>\
|
55
53
|
<tr class=\"person even\" id=\"row_person_1\"><td>Andrius</td><td>@1</td></tr>\
|
56
|
-
<tr class=\"
|
54
|
+
<tr class=\"total-row\"><th>Total of:</th><td>@2</td></tr>\
|
57
55
|
</table>"
|
58
56
|
end
|
59
57
|
|
@@ -74,7 +72,7 @@ describe Tableasy::TablesHelper do
|
|
74
72
|
<tr><th>Name</th><th>Id2</th><th>Id</th></tr>\
|
75
73
|
<tr class=\"person odd\" id=\"row_person_1\"><td>Admin</td><td>2</td><td>1 (50.000%)</td></tr>\
|
76
74
|
<tr class=\"person even\" id=\"row_person_1\"><td>Andrius</td><td>2</td><td>1 (50.000%)</td></tr>\
|
77
|
-
<tr class=\"
|
75
|
+
<tr class=\"total-row\"><th>Total: </th><td>4</td><td>2 (50.000%)</td></tr>\
|
78
76
|
</table>"
|
79
77
|
end
|
80
78
|
|
@@ -90,4 +88,13 @@ describe Tableasy::TablesHelper do
|
|
90
88
|
output = helper.table_for(Project, [@project], :name, :html => {:id => 'my_table'})
|
91
89
|
output.should == "<table id=\"my_table\"><tr><th>Name</th></tr><tr class=\"project odd\" id=\"row_project_1\"><td>project</td></tr></table>"
|
92
90
|
end
|
91
|
+
|
92
|
+
describe 'data list' do
|
93
|
+
it "should allow building data list" do
|
94
|
+
build :andrius
|
95
|
+
output = helper.data_list(@andrius, :name, :id)
|
96
|
+
|
97
|
+
output.should == "<table><tr><th>Name</th><td>Andrius</td></tr><tr><th>Id</th><td>1</td></tr></table>"
|
98
|
+
end
|
99
|
+
end
|
93
100
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
describe Tableasy::Table::Cell do
|
4
|
+
before do
|
5
|
+
build :andrius
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'with cell created' do
|
9
|
+
before do
|
10
|
+
@cell = Tableasy::Table::Cell.new(@andrius, 'andrius')
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be created" do
|
14
|
+
@cell.subject.should == @andrius
|
15
|
+
@cell.value = 'andrius'
|
16
|
+
@cell.header.should be_false
|
17
|
+
@cell.tag.should == 'td'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should allow to assign html attributes" do
|
21
|
+
@cell.html[:class] = 'class'
|
22
|
+
@cell.html.should == {:class => 'class'}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should allow to create header cell" do
|
27
|
+
cell = Tableasy::Table::Cell.new(@andrius, 'andrius', true)
|
28
|
+
cell.header.should be_true
|
29
|
+
cell.tag.should == 'th'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should take value from object if symbol is passed as value" do
|
33
|
+
cell = Tableasy::Table::Cell.new(@andrius, :name)
|
34
|
+
cell.value.should == 'Andrius'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should execute formatter if it's passed as value" do
|
38
|
+
f = Tableasy::Formatter.new {|cell, string| cell.value += string }
|
39
|
+
cf = Tableasy::Formatter::Column.new(self, f, :to_s, ' hello')
|
40
|
+
cell = Tableasy::Table::Cell.new(@andrius, cf)
|
41
|
+
cell.value.should == 'Andrius hello'
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
describe Tableasy::Table::Row do
|
4
|
+
it "should skip cells that have value nil" do
|
5
|
+
cell1 = Tableasy::Table::Cell.new(nil, 'a')
|
6
|
+
cell2 = Tableasy::Table::Cell.new(nil, nil)
|
7
|
+
row = Tableasy::Table::Row.new([cell1, cell2])
|
8
|
+
row.cells.should == [cell1]
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should allow marking row as total row" do
|
12
|
+
row = Tableasy::Table::Row.new([], :total => true)
|
13
|
+
row.should be_total_row
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow marking row as header row" do
|
17
|
+
row = Tableasy::Table::Row.new([], :header => true)
|
18
|
+
row.should be_header_row
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should allow passing html attributes" do
|
22
|
+
row = Tableasy::Table::Row.new([], :title => 'hello')
|
23
|
+
row.html[:title].should == 'hello'
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper"
|
2
|
+
|
3
|
+
describe Tableasy::Table do
|
4
|
+
describe 'with created table' do
|
5
|
+
before :each do
|
6
|
+
build :andrius
|
7
|
+
@table = Tableasy::Table.new
|
8
|
+
@cell = Tableasy::Table::Cell.new(nil, 'val')
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should allow to add row" do
|
12
|
+
@table.add_row([@cell, @cell])
|
13
|
+
@table.rows[0].cells.should == [@cell, @cell]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow to add row with html" do
|
17
|
+
@table.add_row([@cell], :class => 'total')
|
18
|
+
@table.rows[0].html.should == {:class => 'total'}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/tableasy.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{tableasy}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Andrius Chamentauskas"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-03-04}
|
13
13
|
s.description = %q{Rails tables builder gem that makes creating tables painless. Includes ability to write custom column formatters or even customize row completely.
|
14
14
|
Includes library of predefined column formatters. Also has ability to generate "totals" row.}
|
15
15
|
s.email = %q{sinsiliux@gmail.com}
|
@@ -26,10 +26,11 @@ Includes library of predefined column formatters. Also has ability to generate "
|
|
26
26
|
"VERSION",
|
27
27
|
"lib/formatters.rb",
|
28
28
|
"lib/tableasy.rb",
|
29
|
-
"lib/tableasy/column.rb",
|
30
29
|
"lib/tableasy/formatter.rb",
|
31
30
|
"lib/tableasy/html_attributes.rb",
|
32
|
-
"lib/tableasy/
|
31
|
+
"lib/tableasy/table.rb",
|
32
|
+
"lib/tableasy/table/cell.rb",
|
33
|
+
"lib/tableasy/table/row.rb",
|
33
34
|
"lib/tableasy/tables_helper.rb",
|
34
35
|
"lib/tableasy/total.rb",
|
35
36
|
"spec/blueprint.rb",
|
@@ -38,6 +39,9 @@ Includes library of predefined column formatters. Also has ability to generate "
|
|
38
39
|
"spec/helpers/tables_helper_spec.rb",
|
39
40
|
"spec/spec.opts",
|
40
41
|
"spec/spec_helper.rb",
|
42
|
+
"spec/table/cell_spec.rb",
|
43
|
+
"spec/table/row_spec.rb",
|
44
|
+
"spec/table/table_spec.rb",
|
41
45
|
"tableasy.gemspec"
|
42
46
|
]
|
43
47
|
s.homepage = %q{http://github.com/sinsiliux/tableasy}
|
@@ -50,6 +54,9 @@ Includes library of predefined column formatters. Also has ability to generate "
|
|
50
54
|
"spec/helpers/formatting_helper_spec.rb",
|
51
55
|
"spec/spec_helper.rb",
|
52
56
|
"spec/fake_models.rb",
|
57
|
+
"spec/table/cell_spec.rb",
|
58
|
+
"spec/table/table_spec.rb",
|
59
|
+
"spec/table/row_spec.rb",
|
53
60
|
"spec/blueprint.rb"
|
54
61
|
]
|
55
62
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tableasy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrius Chamentauskas
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-03-04 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -82,10 +82,11 @@ files:
|
|
82
82
|
- VERSION
|
83
83
|
- lib/formatters.rb
|
84
84
|
- lib/tableasy.rb
|
85
|
-
- lib/tableasy/column.rb
|
86
85
|
- lib/tableasy/formatter.rb
|
87
86
|
- lib/tableasy/html_attributes.rb
|
88
|
-
- lib/tableasy/
|
87
|
+
- lib/tableasy/table.rb
|
88
|
+
- lib/tableasy/table/cell.rb
|
89
|
+
- lib/tableasy/table/row.rb
|
89
90
|
- lib/tableasy/tables_helper.rb
|
90
91
|
- lib/tableasy/total.rb
|
91
92
|
- spec/blueprint.rb
|
@@ -94,6 +95,9 @@ files:
|
|
94
95
|
- spec/helpers/tables_helper_spec.rb
|
95
96
|
- spec/spec.opts
|
96
97
|
- spec/spec_helper.rb
|
98
|
+
- spec/table/cell_spec.rb
|
99
|
+
- spec/table/row_spec.rb
|
100
|
+
- spec/table/table_spec.rb
|
97
101
|
- tableasy.gemspec
|
98
102
|
has_rdoc: true
|
99
103
|
homepage: http://github.com/sinsiliux/tableasy
|
@@ -128,4 +132,7 @@ test_files:
|
|
128
132
|
- spec/helpers/formatting_helper_spec.rb
|
129
133
|
- spec/spec_helper.rb
|
130
134
|
- spec/fake_models.rb
|
135
|
+
- spec/table/cell_spec.rb
|
136
|
+
- spec/table/table_spec.rb
|
137
|
+
- spec/table/row_spec.rb
|
131
138
|
- spec/blueprint.rb
|
data/lib/tableasy/column.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
module Tableasy
|
2
|
-
class Column
|
3
|
-
include HtmlAttributes
|
4
|
-
attr_reader :value
|
5
|
-
|
6
|
-
def initialize(record, value)
|
7
|
-
@record = record
|
8
|
-
self.value = value
|
9
|
-
end
|
10
|
-
|
11
|
-
def value=(value)
|
12
|
-
value = value.execute(@record) if value.is_a?(Tableasy::ColumnFormatter)
|
13
|
-
value = @record.send(value) if value.is_a?(Symbol)
|
14
|
-
@value = value
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
data/lib/tableasy/row.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
module Tableasy
|
2
|
-
class Row
|
3
|
-
include HtmlAttributes
|
4
|
-
attr_reader :record, :columns
|
5
|
-
|
6
|
-
def initialize(record, columns, total_row = false)
|
7
|
-
@record = record
|
8
|
-
|
9
|
-
@columns = columns.collect do |value|
|
10
|
-
Column.new(record, value)
|
11
|
-
end
|
12
|
-
@total_row = total_row
|
13
|
-
@html = {}
|
14
|
-
end
|
15
|
-
|
16
|
-
def total_row?
|
17
|
-
@total_row
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|