tableficate 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/README.markdown +6 -4
  2. data/app/views/tableficate/_data.html.erb +7 -1
  3. data/app/views/tableficate/_header.html.erb +2 -2
  4. data/app/views/tableficate/_table.html.erb +16 -23
  5. data/app/views/tableficate/_table_for.html.erb +9 -0
  6. data/app/views/tableficate/filters/_check_box_choice.html.erb +3 -0
  7. data/app/views/tableficate/filters/_form.html.erb +16 -0
  8. data/app/views/tableficate/filters/_radio_choice.html.erb +3 -0
  9. data/changelog.markdown +17 -0
  10. data/lib/generators/tableficate/theme/theme_generator.rb +17 -1
  11. data/lib/tableficate.rb +3 -0
  12. data/lib/tableficate/action_column.rb +2 -2
  13. data/lib/tableficate/caption.rb +29 -0
  14. data/lib/tableficate/column.rb +13 -7
  15. data/lib/tableficate/empty.rb +34 -0
  16. data/lib/tableficate/filters/base.rb +7 -5
  17. data/lib/tableficate/filters/check_box.rb +1 -1
  18. data/lib/tableficate/filters/choice.rb +2 -2
  19. data/lib/tableficate/filters/collection_base.rb +13 -0
  20. data/lib/tableficate/filters/input.rb +2 -2
  21. data/lib/tableficate/filters/input_range.rb +6 -6
  22. data/lib/tableficate/filters/radio.rb +1 -1
  23. data/lib/tableficate/filters/select.rb +1 -1
  24. data/lib/tableficate/filters/select_range.rb +4 -4
  25. data/lib/tableficate/finder.rb +70 -28
  26. data/lib/tableficate/helper.rb +44 -47
  27. data/lib/tableficate/table.rb +64 -24
  28. data/lib/tableficate/utils.rb +27 -2
  29. data/lib/tableficate/version.rb +1 -1
  30. data/spec/action_column_spec.rb +9 -1
  31. data/spec/base_spec.rb +10 -0
  32. data/spec/caption_spec.rb +33 -0
  33. data/spec/column_spec.rb +29 -0
  34. data/spec/empty_spec.rb +45 -0
  35. data/spec/filters/base_spec.rb +6 -5
  36. data/spec/filters/choice_spec.rb +5 -2
  37. data/spec/filters/collection_spec.rb +4 -4
  38. data/spec/filters/input_range_spec.rb +10 -8
  39. data/spec/filters/select_range_spec.rb +79 -0
  40. data/spec/finder_spec.rb +48 -1
  41. data/spec/generators/tableficate_theme_spec.rb +14 -0
  42. data/spec/table_spec.rb +114 -3
  43. data/spec/test_app/app/controllers/filters_controller.rb +1 -1
  44. data/spec/test_app/app/controllers/tests_controller.rb +5 -0
  45. data/spec/test_app/app/controllers/themes_controller.rb +7 -0
  46. data/spec/test_app/app/views/filters/html5_input_types.html.erb +31 -0
  47. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_check_box_choice.html.erb +2 -0
  48. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_radio_choice.html.erb +2 -0
  49. data/spec/test_app/app/views/tests/index.html.erb +13 -0
  50. data/spec/test_app/app/views/themes/caption.html.erb +9 -0
  51. data/spec/test_app/app/views/themes/column_attrs.html.erb +7 -0
  52. data/spec/test_app/app/views/themes/column_cell_attrs.html.erb +7 -0
  53. data/spec/test_app/app/views/themes/column_cell_attrs_with_proc.html.erb +7 -0
  54. data/spec/test_app/app/views/themes/column_header_attrs.html.erb +7 -0
  55. data/spec/test_app/app/views/themes/empty_with_data.html.erb +9 -0
  56. data/spec/test_app/app/views/themes/empty_with_no_data.html.erb +9 -0
  57. data/spec/test_app/app/views/themes/no_caption.html.erb +7 -0
  58. data/spec/test_app/app/views/themes/no_column_attrs.html.erb +7 -0
  59. data/spec/test_app/config/application.rb +1 -1
  60. data/spec/test_app/config/routes.rb +2 -0
  61. data/spec/test_app/db/migrate/20111227224959_additional_columns.rb +73 -0
  62. data/spec/test_app/db/migrate/20111230203456_created_at_specific_dates.rb +11 -0
  63. data/spec/test_app/db/schema.rb +10 -3
  64. data/spec/test_app/db/test.sqlite3 +0 -0
  65. data/spec/theme/caption_spec.rb +15 -0
  66. data/spec/theme/column_spec.rb +35 -0
  67. data/spec/theme/empty_spec.rb +15 -0
  68. data/spec/utils_spec.rb +44 -4
  69. metadata +69 -58
  70. data/spec/test_app/app/views/tableficate/custom_check_box_block/_data.html.erb +0 -1
  71. data/spec/test_app/app/views/tableficate/custom_check_box_block/_header.html.erb +0 -8
  72. data/spec/test_app/app/views/tableficate/custom_check_box_block/_row.html.erb +0 -5
  73. data/spec/test_app/app/views/tableficate/custom_check_box_block/_table.html.erb +0 -36
  74. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_check_box.html.erb +0 -7
  75. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_input.html.erb +0 -4
  76. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_input_range.html.erb +0 -6
  77. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_radio.html.erb +0 -4
  78. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_select.html.erb +0 -4
  79. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_select_range_filter.html.erb +0 -6
  80. data/spec/test_app/app/views/tableficate/custom_radio_block/_data.html.erb +0 -1
  81. data/spec/test_app/app/views/tableficate/custom_radio_block/_header.html.erb +0 -8
  82. data/spec/test_app/app/views/tableficate/custom_radio_block/_row.html.erb +0 -5
  83. data/spec/test_app/app/views/tableficate/custom_radio_block/_table.html.erb +0 -36
  84. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_check_box.html.erb +0 -4
  85. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_input.html.erb +0 -4
  86. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_input_range.html.erb +0 -6
  87. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_radio.html.erb +0 -7
  88. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_select.html.erb +0 -4
  89. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_select_range_filter.html.erb +0 -6
  90. data/spec/test_app/public/index.html +0 -241
@@ -3,100 +3,97 @@ module Tableficate
3
3
  def table_for(rows, options = {})
4
4
  t = Tableficate::Table.new(self, rows, options, rows.tableficate_get_data)
5
5
  yield(t)
6
- t.render
6
+ t.template.render(partial: Tableficate::Utils::template_path(t.template, 'table_for', t.theme), locals: {table: t})
7
+ end
8
+
9
+ def tableficate_table_tag(table)
10
+ render partial: Tableficate::Utils::template_path(table.template, 'table', table.theme), locals: {table: table}
7
11
  end
8
12
 
9
13
  def tableficate_header_tag(column)
10
- render partial: Tableficate::Utils::template_path('header', column.table.options[:theme]), locals: {column: column}
14
+ table = column.table
15
+ render partial: Tableficate::Utils::template_path(table.template, 'header', table.theme), locals: {column: column}
11
16
  end
12
17
 
13
18
  def tableficate_data_tag(row, column)
14
- render partial: Tableficate::Utils::template_path('data', column.table.options[:theme]), locals: {row: row, column: column}
19
+ table = column.table
20
+ render partial: Tableficate::Utils::template_path(table.template, 'data', table.theme), locals: {row: row, column: column}
15
21
  end
16
22
 
17
23
  def tableficate_row_tag(row, columns)
18
- render partial: Tableficate::Utils::template_path('row', columns.first.table.options[:theme]), locals: {row: row, columns: columns}
24
+ table = columns.first.table
25
+ render partial: Tableficate::Utils::template_path(table.template, 'row', table.theme), locals: {row: row, columns: columns}
26
+ end
27
+
28
+ def tableficate_filter_form_tag(table)
29
+ render partial: Tableficate::Utils::template_path(table.template, 'filters/form', table.theme), locals: {table: table}
19
30
  end
20
31
 
21
32
  def tableficate_filter_tag(filter)
22
- render partial: Tableficate::Utils::template_path(filter.template, filter.table.options[:theme]), locals: {filter: filter}
33
+ table = filter.table
34
+ render partial: Tableficate::Utils::template_path(table.template, filter.template, table.theme), locals: {filter: filter}
23
35
  end
24
36
 
25
37
  def tableficate_label_tag(filter)
26
- label_tag(filter.field_name, filter.label, filter.options[:label_options])
38
+ label_tag(filter.field_name, filter.label, filter.label_options)
27
39
  end
28
40
 
29
41
  def tableficate_text_field_tag(filter)
30
- text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.options)
42
+ text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.attrs)
31
43
  end
32
44
 
33
45
  def tableficate_select_tag(filter)
34
46
  field_value = filter.field_value(params[filter.table.as])
35
47
 
36
- if field_value.present? and filter.options[:collection].is_a?(String)
48
+ collection = filter.collection
49
+
50
+ if field_value.present? and collection.is_a?(String)
37
51
  Array.wrap(field_value).each do |fv|
38
- if filter.options[:collection].match(/<option[^>]*value\s*=/)
39
- filter.options[:collection].gsub!(/(<option[^>]*value\s*=\s*['"]?#{fv}[^>]*)/, '\1 selected="selected"')
52
+ if collection.match(/<option[^>]*value\s*=/)
53
+ collection.gsub!(/(<option[^>]*value\s*=\s*['"]?#{fv}[^>]*)/, '\1 selected="selected"')
40
54
  else
41
- filter.options[:collection].gsub!(/>#{fv}</, " selected=\"selected\">#{fv}<")
55
+ collection.gsub!(/>#{fv}</, " selected=\"selected\">#{fv}<")
42
56
  end
43
57
  end
44
- elsif not filter.options[:collection].is_a?(String)
45
- filter.options[:collection] = Tableficate::Filter::Collection.new(filter.options[:collection], selected: field_value).map {|choice|
46
- html_attributes = choice.options.length > 0 ? ' ' + choice.options.map {|k, v| %(#{k.to_s}="#{v}")}.join(' ') : ''
58
+ elsif not collection.is_a?(String)
59
+ collection = Tableficate::Filter::Collection.new(collection, selected: field_value).map {|choice|
60
+ html_attributes = choice.attrs.length > 0 ? ' ' + choice.attrs.map {|k, v| %(#{k.to_s}="#{v}")}.join(' ') : ''
47
61
  selected_attribute = choice.selected? ? ' selected="selected"' : ''
48
62
 
49
63
  %(<option value="#{ERB::Util.html_escape(choice.value)}"#{selected_attribute}#{html_attributes}>#{ERB::Util.html_escape(choice.name)}</option>)
50
64
  }.join("\n")
51
65
  end
52
66
 
53
- filter.options[:collection] = filter.options[:collection].html_safe
67
+ collection = collection.html_safe
54
68
 
55
- select_tag(filter.field_name, filter.options.delete(:collection), filter.options)
69
+ select_tag(filter.field_name, collection, filter.attrs)
56
70
  end
57
71
 
58
- def tableficate_radio_tags(filter, &block)
59
- field_value = filter.field_value(params[filter.table.as])
60
-
61
- collection = Tableficate::Filter::Collection.new(filter.options[:collection], selected: filter.field_value(params[filter.table.as]))
72
+ def tableficate_radio_tags(filter)
73
+ tableficate_collection_of_tags(filter)
74
+ end
62
75
 
63
- html = []
64
- if block_given?
65
- html = collection.map {|choice| capture(choice, &block)}
76
+ def tableficate_check_box_tags(filter)
77
+ if filter.collection.empty?
78
+ check_box_tag(filter.field_name, true, filter.field_value(params[filter.table.as]) == 'true', filter.attrs)
66
79
  else
67
- collection.each do |choice|
68
- html.push(
69
- radio_button_tag(filter.field_name, choice.value, choice.checked?, choice.options),
70
- label_tag("#{filter.field_name}[#{choice.value}]", choice.name),
71
- '<br/>'
72
- )
73
- end
80
+ tableficate_collection_of_tags(filter)
74
81
  end
75
-
76
- html.join("\n").html_safe
77
82
  end
78
83
 
79
- def tableficate_check_box_tags(filter, &block)
84
+ def tableficate_collection_of_tags(filter)
85
+ table = filter.table
80
86
  field_value = filter.field_value(params[filter.table.as])
81
87
 
82
- collection = Tableficate::Filter::Collection.new(filter.options[:collection], selected: filter.field_value(params[filter.table.as]))
83
-
84
88
  html = []
85
- if block_given?
86
- html = collection.map {|choice| capture(choice, &block)}
87
- else
88
- collection.each do |choice|
89
- html.push(
90
- check_box_tag(
91
- "#{filter.field_name}[#{choice.value}]", choice.value, choice.checked?, choice.options.reverse_merge(name: "#{filter.field_name}[]")
92
- ),
93
- label_tag("#{filter.field_name}[#{choice.value}]", choice.name),
94
- '<br/>'
95
- )
96
- end
89
+ Tableficate::Filter::Collection.new(filter.collection, selected: field_value).each do |choice|
90
+ html.push(
91
+ render(partial: Tableficate::Utils::template_path(table.template, filter.template + '_choice', table.theme), locals: {filter: filter, choice: choice})
92
+ )
97
93
  end
98
94
 
99
95
  html.join("\n").html_safe
100
96
  end
97
+ private :tableficate_collection_of_tags
101
98
  end
102
99
  end
@@ -1,32 +1,45 @@
1
1
  module Tableficate
2
2
  class Table
3
- attr_reader :columns, :rows, :current_sort, :filters, :options, :as, :template
3
+ attr_reader :columns, :rows, :current_sort, :filters, :attrs, :as, :template, :theme
4
4
 
5
5
  def initialize(template, rows, options, data)
6
6
  @template = template
7
7
  @rows = rows
8
- @columns = []
9
- @filters = []
10
- @as = options[:as] || rows.table_name
11
8
 
12
- @options = {
13
- show_sorts: false,
14
- theme: ''
15
- }.merge(options)
9
+ @as = options.delete(:as) || rows.table_name
10
+ @theme = options.delete(:theme) || ''
11
+ @show_sorts = options.delete(:show_sorts) || false
12
+ @attrs = options
13
+
14
+ @columns = []
15
+ @filters = []
16
16
 
17
17
  @current_sort = data[:current_sort]
18
+ @field_map = data[:field_map] || {}
18
19
  end
19
20
 
20
- def column(name, options = {}, &block)
21
- options.reverse_merge!(
22
- show_sort: @options[:show_sorts]
23
- )
21
+ def empty(*args, &block)
22
+ if args.empty? and not block_given?
23
+ @empty
24
+ else
25
+ @empty = Empty.new(self, *args, &block)
26
+ end
27
+ end
24
28
 
25
- @columns.push(Column.new(self, name, options, &block))
29
+ def caption(*args, &block)
30
+ if args.empty? and not block_given?
31
+ @caption
32
+ else
33
+ @caption = Caption.new(*args, &block)
34
+ end
26
35
  end
27
36
 
28
- def actions(&block)
29
- @columns.push(ActionColumn.new(self, &block))
37
+ def column(name, options = {}, &block)
38
+ @columns.push(Column.new(self, name, options.reverse_merge(show_sort: @show_sorts), &block))
39
+ end
40
+
41
+ def actions(options = {}, &block)
42
+ @columns.push(ActionColumn.new(self, options, &block))
30
43
  end
31
44
 
32
45
  def show_sort?
@@ -54,7 +67,7 @@ module Tableficate
54
67
  checkbox: Filter::CheckBox
55
68
  }
56
69
 
57
- as = options.delete(:as) || (options[:collection] ? :select : :text)
70
+ as = options.delete(:as) || find_as(name, options.has_key?(:collection))
58
71
 
59
72
  raise Filter::UnknownInputType if as_map[as].nil?
60
73
 
@@ -82,7 +95,7 @@ module Tableficate
82
95
  select: Filter::SelectRange
83
96
  }
84
97
 
85
- as = options.delete(:as) || (options[:collection] ? :select : :text)
98
+ as = options.delete(:as) || find_as(name, options.has_key?(:collection))
86
99
 
87
100
  raise Filter::UnknownInputType if as_map[as].nil?
88
101
 
@@ -91,13 +104,40 @@ module Tableficate
91
104
  @filters.push(as_map[as].new(self, name, options))
92
105
  end
93
106
 
94
- def render(options = {})
95
- options.reverse_merge!(
96
- partial: Tableficate::Utils::template_path('table', @options[:theme]),
97
- locals: {table: self}
98
- )
99
-
100
- @template.render options
107
+ def find_as(name, has_collection)
108
+ field_name = (@field_map[name] || name).to_s
109
+ as = :text
110
+
111
+ if has_collection
112
+ as = :select
113
+ else
114
+ case Tableficate::Utils::find_column_type(@rows, field_name)
115
+ when :integer, :float, :decimal
116
+ as = :number
117
+ when :date
118
+ as = :date
119
+ when :time
120
+ as = :time
121
+ when :datetime, :timestamp
122
+ as = :datetime
123
+ when :boolean
124
+ as = :checkbox
125
+ end
126
+ end
127
+
128
+ if as == :text
129
+ case name
130
+ when /email/
131
+ as = :email
132
+ when /url/
133
+ as = :url
134
+ when /phone/
135
+ as = :tel
136
+ end
137
+ end
138
+
139
+ as
101
140
  end
141
+ private :find_as
102
142
  end
103
143
  end
@@ -1,7 +1,32 @@
1
1
  module Tableficate
2
2
  module Utils
3
- def self.template_path(template, theme = '')
4
- File.join(['tableficate', theme, template].delete_if(&:blank?))
3
+ def self.template_path(template, partial, theme = '')
4
+ file = File.join(['tableficate', theme, partial].delete_if(&:blank?))
5
+
6
+ file = File.join(['tableficate', partial]) if not theme.blank? and not template.lookup_context.exists?(file, [], true)
7
+
8
+ file
9
+ end
10
+
11
+ def self.find_column_type(scope, name)
12
+ name = name.to_s
13
+ column = scope.columns.detect{|column| column.name == name} ||
14
+ (
15
+ (scope.respond_to?(:joins_values) ? scope.joins_values : []) +
16
+ (scope.respond_to?(:includes_values) ? scope.includes_values : [])
17
+ ).uniq.map{|join|
18
+ # convert string joins to table names
19
+ if join.is_a?(String)
20
+ join.scan(/(?:(?:,|\bjoin\s*)\s*(\w+))/i)
21
+ else
22
+ join
23
+ end
24
+ }.flatten.map{|table_name|
25
+ ActiveRecord::Base::connection_pool.columns[table_name.to_s.tableize]
26
+ }.flatten.detect{|column|
27
+ column.name == name
28
+ }
29
+ column.try(:type)
5
30
  end
6
31
  end
7
32
  end
@@ -1,3 +1,3 @@
1
1
  module Tableficate
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Tableficate::ActionColumn do
4
4
  before(:all) do
5
5
  @action_column = Tableficate::ActionColumn.new(nil) do
6
- Actions!
6
+ 'Actions Here!'
7
7
  end
8
8
  end
9
9
 
@@ -18,4 +18,12 @@ describe Tableficate::ActionColumn do
18
18
  it 'should have a blank name' do
19
19
  @action_column.name.should == ''
20
20
  end
21
+
22
+ it 'should accept options' do
23
+ action_column = Tableficate::ActionColumn.new(nil, header: 'Actions') do
24
+ 'Actions Here!'
25
+ end
26
+
27
+ action_column.header.should == 'Actions'
28
+ end
21
29
  end
@@ -18,6 +18,16 @@ describe Tableficate::Base do
18
18
  lambda {NoScope.tableficate({})}.should raise_error(Tableficate::MissingScope)
19
19
  end
20
20
 
21
+ it 'should allow for custom column sorting' do
22
+ class ColumnOrder < Tableficate::Base
23
+ scope(:nobel_prize_winner)
24
+
25
+ column(:full_name, sort: 'first_name ASC, last_name ASC')
26
+ end
27
+
28
+ ColumnOrder.send(:instance_variable_get, '@sort')[:full_name].should == 'first_name ASC, last_name ASC'
29
+ end
30
+
21
31
  it 'should allow for a default order' do
22
32
  class DefaultOrder < Tableficate::Base
23
33
  scope(:nobel_prize_winner)
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tableficate::Caption do
4
+ it 'should accept plain text in the arguments' do
5
+ caption = Tableficate::Caption.new('Foo', {class: 'title'})
6
+
7
+ caption.attrs[:class].should == 'title'
8
+ caption.value.should == 'Foo'
9
+ end
10
+
11
+ it 'should take a block in place of the plain text argument' do
12
+ caption = Tableficate::Caption.new({class: 'title'}) do
13
+ 'Foo'
14
+ end
15
+
16
+ caption.attrs[:class].should == 'title'
17
+ caption.value.should == 'Foo'
18
+ end
19
+ it 'should not escape html in block outputs' do
20
+ caption = Tableficate::Caption.new do
21
+ '<b>Foo</b>'
22
+ end
23
+
24
+ ERB::Util::html_escape(caption.value).should == '<b>Foo</b>'
25
+ end
26
+ it 'should allow ERB tags in block outputs' do
27
+ caption = Tableficate::Caption.new do
28
+ ERB.new("<%= 'Foo'.upcase %>").result(binding)
29
+ end
30
+
31
+ caption.value.should == 'FOO'
32
+ end
33
+ end
@@ -9,6 +9,27 @@ describe Tableficate::Column do
9
9
  column.header.should == 'Given Name'
10
10
  end
11
11
 
12
+ it 'should take all non-specialized options as attrs on the `col` tag' do
13
+ column = Tableficate::Column.new(nil, :first_name, class: 'attrs')
14
+ column.attrs.should == {class: 'attrs'}
15
+
16
+ column = Tableficate::Column.new(nil, :first_name)
17
+ column.attrs.should == {}
18
+ end
19
+
20
+ it 'should accept :header_attrs as an option' do
21
+ column = Tableficate::Column.new(nil, :first_name, header_attrs: {class: 'header'})
22
+ column.header_attrs.should == {class: 'header'}
23
+
24
+ column = Tableficate::Column.new(nil, :first_name)
25
+ column.header_attrs.should == {}
26
+ end
27
+
28
+ it 'should accept :cell_attrs as an option' do
29
+ column = Tableficate::Column.new(nil, :first_name, cell_attrs: {class: 'cell'})
30
+ column.cell_attrs.should == {class: 'cell'}
31
+ end
32
+
12
33
  it 'should show the value from the database field if no alternative is provided' do
13
34
  row = NobelPrizeWinner.find_by_first_name('Norman')
14
35
  column = Tableficate::Column.new(nil, :first_name)
@@ -31,6 +52,14 @@ describe Tableficate::Column do
31
52
 
32
53
  ERB::Util::html_escape(column.value(row)).should == 'Norman<br/>Borlaug'
33
54
  end
55
+ it 'should allow ERB tags in block outputs' do
56
+ row = NobelPrizeWinner.find_by_first_name_and_last_name('Norman', 'Borlaug')
57
+ column = Tableficate::Column.new(nil, :first_name) do |row|
58
+ ERB.new("<%= row.first_name.upcase %>").result(binding)
59
+ end
60
+
61
+ column.value(row).should == 'NORMAN'
62
+ end
34
63
 
35
64
  it 'should allow sorting to be turned on and off' do
36
65
  column = Tableficate::Column.new(nil, :first_name, show_sort: false)
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tableficate::Empty do
4
+ before(:each) do
5
+ @table = Tableficate::Table.new(nil, NobelPrizeWinner.limit(1), {}, {})
6
+ end
7
+
8
+ it 'should add a `colspan` attribute' do
9
+ @table.column(:first_name)
10
+ @table.column(:last_name)
11
+ empty = Tableficate::Empty.new(@table, 'Foo')
12
+
13
+ empty.attrs[:colspan].should == 2
14
+ end
15
+
16
+ it 'should accept plain text in the arguments' do
17
+ empty = Tableficate::Empty.new(@table, 'Foo', {class: 'title'})
18
+
19
+ empty.attrs[:class].should == 'title'
20
+ empty.value.should == 'Foo'
21
+ end
22
+
23
+ it 'should take a block in place of the plain text argument' do
24
+ empty = Tableficate::Empty.new(@table, {class: 'title'}) do
25
+ 'Foo'
26
+ end
27
+
28
+ empty.attrs[:class].should == 'title'
29
+ empty.value.should == 'Foo'
30
+ end
31
+ it 'should not escape html in block outputs' do
32
+ empty = Tableficate::Empty.new(@table) do
33
+ '<b>Foo</b>'
34
+ end
35
+
36
+ ERB::Util::html_escape(empty.value).should == '<b>Foo</b>'
37
+ end
38
+ it 'should allow ERB tags in block outputs' do
39
+ empty = Tableficate::Empty.new(@table) do
40
+ ERB.new("<%= 'Foo'.upcase %>").result(binding)
41
+ end
42
+
43
+ empty.value.should == 'FOO'
44
+ end
45
+ end