tableficate 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/README.markdown +53 -48
  2. data/app/views/tableficate/{_column_header.html.erb → _header.html.erb} +0 -0
  3. data/app/views/tableficate/_table.html.erb +4 -4
  4. data/app/views/tableficate/filters/_check_box.html.erb +4 -0
  5. data/app/views/tableficate/{_input_filter.html.erb → filters/_input.html.erb} +0 -0
  6. data/app/views/tableficate/{_input_range_filter.html.erb → filters/_input_range.html.erb} +0 -0
  7. data/app/views/tableficate/filters/_radio.html.erb +4 -0
  8. data/app/views/tableficate/{_select_filter.html.erb → filters/_select.html.erb} +0 -0
  9. data/app/views/tableficate/filters/_select_range.html.erb +6 -0
  10. data/changelog.markdown +8 -0
  11. data/lib/tableficate/action_column.rb +2 -2
  12. data/lib/tableficate/base.rb +2 -2
  13. data/lib/tableficate/column.rb +6 -2
  14. data/lib/tableficate/exceptions.rb +4 -0
  15. data/lib/tableficate/filters/base.rb +21 -0
  16. data/lib/tableficate/filters/check_box.rb +6 -0
  17. data/lib/tableficate/filters/choice.rb +23 -0
  18. data/lib/tableficate/filters/collection.rb +30 -0
  19. data/lib/tableficate/filters/input.rb +12 -0
  20. data/lib/tableficate/filters/input_range.rb +57 -0
  21. data/lib/tableficate/filters/radio.rb +6 -0
  22. data/lib/tableficate/filters/select.rb +6 -0
  23. data/lib/tableficate/filters/select_range.rb +54 -0
  24. data/lib/tableficate/helper.rb +69 -4
  25. data/lib/tableficate/table.rb +56 -10
  26. data/lib/tableficate/version.rb +1 -1
  27. data/lib/tableficate.rb +9 -3
  28. data/spec/action_column_spec.rb +1 -1
  29. data/spec/column_spec.rb +8 -0
  30. data/spec/filters/base_spec.rb +30 -0
  31. data/spec/filters/choice_spec.rb +13 -0
  32. data/spec/filters/collection_spec.rb +94 -0
  33. data/spec/filters/input_range_spec.rb +91 -0
  34. data/spec/filters/input_spec.rb +24 -0
  35. data/spec/generators/tableficate_table_spec.rb +20 -0
  36. data/spec/generators/tableficate_theme_spec.rb +11 -0
  37. data/spec/helper_spec.rb +164 -0
  38. data/spec/spec_helper.rb +3 -0
  39. data/spec/table_spec.rb +38 -13
  40. data/spec/test_app/Gemfile +1 -1
  41. data/spec/test_app/app/controllers/filters_controller.rb +7 -0
  42. data/spec/test_app/app/views/filters/check_box_tags.html.erb +14 -0
  43. data/spec/test_app/app/views/filters/multi_select_from_string.html.erb +11 -0
  44. data/spec/test_app/app/views/filters/multi_select_tag.html.erb +11 -0
  45. data/spec/test_app/app/views/filters/radio_tags.html.erb +14 -0
  46. data/spec/test_app/app/views/filters/select_from_string.html.erb +11 -0
  47. data/spec/test_app/app/views/filters/select_tag.html.erb +11 -0
  48. data/spec/test_app/app/views/tableficate/custom_check_box_block/_data.html.erb +1 -0
  49. data/spec/test_app/app/views/tableficate/custom_check_box_block/_header.html.erb +8 -0
  50. data/spec/test_app/app/views/tableficate/custom_check_box_block/_row.html.erb +5 -0
  51. data/spec/test_app/app/views/tableficate/custom_check_box_block/_table.html.erb +36 -0
  52. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_check_box.html.erb +7 -0
  53. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_input.html.erb +4 -0
  54. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_input_range.html.erb +6 -0
  55. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_radio.html.erb +4 -0
  56. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_select.html.erb +4 -0
  57. data/spec/test_app/app/views/tableficate/custom_check_box_block/filters/_select_range_filter.html.erb +6 -0
  58. data/spec/test_app/app/views/tableficate/custom_radio_block/_data.html.erb +1 -0
  59. data/spec/test_app/app/views/tableficate/custom_radio_block/_header.html.erb +8 -0
  60. data/spec/test_app/app/views/tableficate/custom_radio_block/_row.html.erb +5 -0
  61. data/spec/test_app/app/views/tableficate/custom_radio_block/_table.html.erb +36 -0
  62. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_check_box.html.erb +4 -0
  63. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_input.html.erb +4 -0
  64. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_input_range.html.erb +6 -0
  65. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_radio.html.erb +7 -0
  66. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_select.html.erb +4 -0
  67. data/spec/test_app/app/views/tableficate/custom_radio_block/filters/_select_range_filter.html.erb +6 -0
  68. data/spec/test_app/config/routes.rb +1 -56
  69. data/tableficate.gemspec +4 -2
  70. metadata +122 -27
  71. data/lib/tableficate/filters/filter.rb +0 -19
  72. data/lib/tableficate/filters/input_filters.rb +0 -64
  73. data/lib/tableficate/filters/select_filter.rb +0 -11
  74. data/spec/filter_spec.rb +0 -30
  75. data/spec/input_filter_spec.rb +0 -90
  76. data/spec/select_filter_spec.rb +0 -10
@@ -1,6 +1,6 @@
1
1
  module Tableficate
2
2
  class Table
3
- attr_reader :columns, :rows, :current_sort, :filters, :options, :as
3
+ attr_reader :columns, :rows, :current_sort, :filters, :options, :as, :template
4
4
 
5
5
  def initialize(template, rows, options, data)
6
6
  @template = template
@@ -25,24 +25,70 @@ module Tableficate
25
25
  @columns.push(Column.new(self, name, options, &block))
26
26
  end
27
27
 
28
- def actions(options = {}, &block)
29
- @columns.push(ActionColumn.new(self, options, block))
28
+ def actions(&block)
29
+ @columns.push(ActionColumn.new(self, &block))
30
30
  end
31
31
 
32
32
  def show_sort?
33
33
  self.columns.any?{|column| column.show_sort?}
34
34
  end
35
35
 
36
- def input_filter(name, options = {})
37
- @filters.push(InputFilter.new(self, name, options))
38
- end
36
+ def filter(name, options = {})
37
+ as_map = {
38
+ :'datetime-local' => Filter::Input,
39
+ text: Filter::Input,
40
+ email: Filter::Input,
41
+ url: Filter::Input,
42
+ tel: Filter::Input,
43
+ number: Filter::Input,
44
+ range: Filter::Input,
45
+ date: Filter::Input,
46
+ month: Filter::Input,
47
+ week: Filter::Input,
48
+ time: Filter::Input,
49
+ datetime: Filter::Input,
50
+ search: Filter::Input,
51
+ color: Filter::Input,
52
+ select: Filter::Select,
53
+ radio: Filter::Radio,
54
+ checkbox: Filter::CheckBox
55
+ }
56
+
57
+ as = options.delete(:as) || (options[:collection] ? :select : :text)
58
+
59
+ raise Filter::UnknownInputType if as_map[as].nil?
60
+
61
+ options[:type] = as.to_s
39
62
 
40
- def input_range_filter(name, options = {})
41
- @filters.push(InputRangeFilter.new(self, name, options))
63
+ @filters.push(as_map[as].new(self, name, options))
42
64
  end
43
65
 
44
- def select_filter(name, choices, options = {})
45
- @filters.push(SelectFilter.new(self, name, choices, options))
66
+ def filter_range(name, options = {})
67
+ as_map = {
68
+ :'datetime-local' => Filter::InputRange,
69
+ text: Filter::InputRange,
70
+ email: Filter::InputRange,
71
+ url: Filter::InputRange,
72
+ tel: Filter::InputRange,
73
+ number: Filter::InputRange,
74
+ range: Filter::InputRange,
75
+ date: Filter::InputRange,
76
+ month: Filter::InputRange,
77
+ week: Filter::InputRange,
78
+ time: Filter::InputRange,
79
+ datetime: Filter::InputRange,
80
+ search: Filter::InputRange,
81
+ color: Filter::InputRange,
82
+ select: Filter::SelectRange
83
+ }
84
+
85
+ as = options.delete(:as) || (options[:collection] ? :select : :text)
86
+
87
+ raise Filter::UnknownInputType if as_map[as].nil?
88
+
89
+ options[:type] = as.to_s
90
+
91
+ @filters.push(as_map[as].new(self, name, options))
46
92
  end
47
93
 
48
94
  def render(options = {})
@@ -1,3 +1,3 @@
1
1
  module Tableficate
2
- VERSION = '0.1.3'
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/tableficate.rb CHANGED
@@ -4,9 +4,15 @@ require 'tableficate/utils'
4
4
  require 'tableficate/finder'
5
5
  require 'tableficate/column'
6
6
  require 'tableficate/action_column'
7
- require 'tableficate/filters/filter'
8
- require 'tableficate/filters/input_filters'
9
- require 'tableficate/filters/select_filter'
7
+ require 'tableficate/filters/collection'
8
+ require 'tableficate/filters/choice'
9
+ require 'tableficate/filters/base'
10
+ require 'tableficate/filters/input'
11
+ require 'tableficate/filters/input_range'
12
+ require 'tableficate/filters/select'
13
+ require 'tableficate/filters/select_range'
14
+ require 'tableficate/filters/radio'
15
+ require 'tableficate/filters/check_box'
10
16
  require 'tableficate/table'
11
17
  require 'tableficate/helper'
12
18
  require 'tableficate/base'
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Tableficate::ActionColumn do
4
4
  before(:all) do
5
- @action_column = Tableficate::ActionColumn.new(nil, nil) do
5
+ @action_column = Tableficate::ActionColumn.new(nil) do
6
6
  Actions!
7
7
  end
8
8
  end
data/spec/column_spec.rb CHANGED
@@ -23,6 +23,14 @@ describe Tableficate::Column do
23
23
 
24
24
  column.value(row).should == 'Norman Borlaug'
25
25
  end
26
+ it 'should not escape html in block outputs' do
27
+ row = NobelPrizeWinner.find_by_first_name_and_last_name('Norman', 'Borlaug')
28
+ column = Tableficate::Column.new(nil, :full_name) do |row|
29
+ [row.first_name, row.last_name].join('<br/>')
30
+ end
31
+
32
+ ERB::Util::html_escape(column.value(row)).should == 'Norman<br/>Borlaug'
33
+ end
26
34
 
27
35
  it 'should allow sorting to be turned on and off' do
28
36
  column = Tableficate::Column.new(nil, :first_name, show_sort: false)
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tableficate::Filter::Base do
4
+ before(:all) do
5
+ @table = Tableficate::Table.new(nil, NobelPrizeWinner.limit(1), {}, {})
6
+ @table.column(:first_name, header: 'Given Name')
7
+ end
8
+
9
+ it 'should find the correct template type' do
10
+ Tableficate::Filter::Base.new(@table, :first_name).template.should == 'filters/base'
11
+ end
12
+
13
+ it 'should use the provided label or default to the column header' do
14
+ Tableficate::Filter::Base.new(@table, :first_name).label.should == 'Given Name'
15
+ Tableficate::Filter::Base.new(@table, :first_name, label: 'First').label.should == 'First'
16
+ end
17
+
18
+ it 'should provide a field name' do
19
+ Tableficate::Filter::Base.new(@table, :first_name).field_name.should == "#{@table.as}[filter][first_name]"
20
+ end
21
+
22
+ it 'should provide a field value when given params or a blank value' do
23
+ Tableficate::Filter::Base.new(@table, :first_name).field_value({filter: {first_name: 'Aaron'}}).should == 'Aaron'
24
+ Tableficate::Filter::Base.new(@table, :first_name).field_value({}).should == ''
25
+ end
26
+
27
+ it 'should allow for filters that do not match a particular field' do
28
+ Tableficate::Filter::Base.new(@table, :custom_field).label.should == 'Custom Field'
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tableficate::Filter::Choice do
4
+ it 'should mark a choice as selected if it has a :selected or :checked option' do
5
+ choice = Tableficate::Filter::Choice.new('foo', 'bar', {selected: 'selected'})
6
+ choice.selected?.should be true
7
+ choice.options[:selected].should be nil
8
+
9
+ choice = Tableficate::Filter::Choice.new('foo', 'bar', {checked: 'checked'})
10
+ choice.checked?.should be true
11
+ choice.options[:checked].should be nil
12
+ end
13
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tableficate::Filter::Collection do
4
+ it 'allows for a collection to accept a range' do
5
+ collection = Tableficate::Filter::Collection.new(1..3)
6
+
7
+ 1.upto(3) do |i|
8
+ index = i - 1
9
+ collection[index].name.should == i
10
+ collection[index].value.should == i
11
+ collection[index].selected?.should be false
12
+ end
13
+ end
14
+
15
+ it 'allows for a collection to accept a hash' do
16
+ h = {one: 1, two: 2, three: 3}
17
+
18
+ collection = Tableficate::Filter::Collection.new(h)
19
+
20
+ i = 0
21
+ h.each do |key, value|
22
+ collection[i].name.should == key
23
+ collection[i].value.should == value
24
+ collection[i].selected?.should be false
25
+ i += 1
26
+ end
27
+ end
28
+
29
+ it 'allows for a collection to accept an array' do
30
+ a = [*1..3]
31
+
32
+ collection = Tableficate::Filter::Collection.new(a)
33
+
34
+ 1.upto(3) do |i|
35
+ index = i - 1
36
+ collection[index].name.should == i
37
+ collection[index].value.should == i
38
+ collection[index].selected?.should be false
39
+ end
40
+ end
41
+
42
+ it 'allows for a collection to accept a nested array' do
43
+ a = [[:one, 1], [:two, 2], [:three, 3]]
44
+
45
+ collection = Tableficate::Filter::Collection.new(a)
46
+
47
+ 0.upto(2) do |i|
48
+ key, value = a[i]
49
+ collection[i].name.should == key
50
+ collection[i].value.should == value
51
+ collection[i].selected?.should be false
52
+ end
53
+ end
54
+
55
+ it 'should mark choices as selected based on the :selected option' do
56
+ a = [*1..3]
57
+
58
+ collection = Tableficate::Filter::Collection.new(a, selected: 2)
59
+
60
+ 1.upto(3) do |i|
61
+ collection[i - 1].selected?.should be (i == 2)
62
+ end
63
+
64
+ collection = Tableficate::Filter::Collection.new(a, selected: [2, 3])
65
+
66
+ 1.upto(3) do |i|
67
+ collection[i - 1].selected?.should be (i == 2 or i == 3)
68
+ end
69
+ end
70
+
71
+ it 'should mark choices as disabled based on the :disabled option' do
72
+ a = [*1..3]
73
+
74
+ collection = Tableficate::Filter::Collection.new(a, disabled: 2)
75
+
76
+ 1.upto(3) do |i|
77
+ if i == 2
78
+ collection[i - 1].options[:disabled].should == 'disabled'
79
+ else
80
+ collection[i - 1].options.has_key?(:disabled).should be false
81
+ end
82
+ end
83
+
84
+ collection = Tableficate::Filter::Collection.new(a, disabled: [2, 3])
85
+
86
+ 1.upto(3) do |i|
87
+ if i == 2 or i == 3
88
+ collection[i - 1].options[:disabled].should == 'disabled'
89
+ else
90
+ collection[i - 1].options.has_key?(:disabled).should be false
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tableficate::Filter::InputStart do
4
+ before(:each) do
5
+ template = mock('Template')
6
+ template.stub!(:lookup_context).and_return(ActionView::LookupContext.new([]))
7
+ template.lookup_context.stub!(:exists?).and_return(true)
8
+ @table = Tableficate::Table.new(template, NobelPrizeWinner.joins(:nobel_prizes).limit(1), {}, {})
9
+ @table.column(:year)
10
+ end
11
+
12
+ it 'should append "[start]" to field_name' do
13
+ Tableficate::Filter::InputStart.new(@table, :year).field_name == "#{@table.as}[filter][year][start]"
14
+ end
15
+
16
+ it 'should append "_start" to the name' do
17
+ Tableficate::Filter::InputStart.new(@table, :year).name == :year_start
18
+ end
19
+
20
+ it 'should provide a field value when given params or a blank value' do
21
+ Tableficate::Filter::InputStart.new(@table, :year).field_value({filter: {year: {start: '2011'}}}).should == '2011'
22
+ Tableficate::Filter::InputStart.new(@table, :year).field_value({}).should == ''
23
+ end
24
+ end
25
+
26
+ describe Tableficate::Filter::InputStop do
27
+ before(:each) do
28
+ template = mock('Template')
29
+ template.stub!(:lookup_context).and_return(ActionView::LookupContext.new([]))
30
+ template.lookup_context.stub!(:exists?).and_return(true)
31
+ @table = Tableficate::Table.new(template, NobelPrizeWinner.joins(:nobel_prizes).limit(1), {}, {})
32
+ @table.column(:year)
33
+ end
34
+
35
+ it 'should append "[stop]" to field_name' do
36
+ Tableficate::Filter::InputStop.new(@table, :year).field_name == "#{@table.as}[filter][year][stop]"
37
+ end
38
+
39
+ it 'should append "_stop" to the name' do
40
+ Tableficate::Filter::InputStop.new(@table, :year).name == :year_stop
41
+ end
42
+
43
+ it 'should provide a field value when given params or a blank value' do
44
+ Tableficate::Filter::InputStop.new(@table, :year).field_value({filter: {year: {stop: '2011'}}}).should == '2011'
45
+ Tableficate::Filter::InputStop.new(@table, :year).field_value({}).should == ''
46
+ end
47
+ end
48
+
49
+ describe Tableficate::Filter::InputRange do
50
+ before(:each) do
51
+ template = mock('Template')
52
+ template.stub!(:lookup_context).and_return(ActionView::LookupContext.new([]))
53
+ @table = Tableficate::Table.new(template, NobelPrizeWinner.joins(:nobel_prizes).limit(1), {}, {})
54
+ @table.column(:year)
55
+ end
56
+
57
+ it 'should create a start and stop input' do
58
+ @table.template.lookup_context.stub!(:exists?).and_return(true)
59
+
60
+ filter = Tableficate::Filter::InputRange.new(@table, :year)
61
+
62
+ filter.start.is_a?(Tableficate::Filter::InputStart).should be true
63
+ filter.stop.is_a?(Tableficate::Filter::InputStop).should be true
64
+ end
65
+
66
+ it 'should use the :start and :stop option hashes for the individual filters and default any values not passed to the range options provided' do
67
+ @table.template.lookup_context.stub!(:exists?).and_return(true)
68
+
69
+ filter = Tableficate::Filter::InputRange.new(@table, :year, label: 'Nobel Prize Won In')
70
+ filter.start.label.should == 'Nobel Prize Won In'
71
+ filter.stop.label.should == 'Nobel Prize Won In'
72
+
73
+ filter = Tableficate::Filter::InputRange.new(@table, :year, label: 'Nobel Prize Won In', start: {label: 'Nobel Prize Won Between'}, stop: {label: 'and'})
74
+ filter.start.label.should == 'Nobel Prize Won Between'
75
+ filter.stop.label.should == 'and'
76
+ end
77
+
78
+ it 'should look for a partial based on the input range type and use it if found' do
79
+ @table.template.lookup_context.stub(:exists?).and_return(true)
80
+
81
+ Tableficate::Filter::InputRange.new(@table, :year, type: 'search').template.should == 'filters/input_range_search'
82
+ end
83
+
84
+ it 'should look for a partial based on the input range type and use the default if it is not found' do
85
+ @table.template.lookup_context.should_receive(:exists?) do |*args|
86
+ (args.first == 'tableficate/filters/input_range' or args.first == 'tableficate/filters/input')
87
+ end.at_least(:once)
88
+
89
+ Tableficate::Filter::InputRange.new(@table, :year, type: 'search').template.should == 'filters/input_range'
90
+ end
91
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tableficate::Filter::Input do
4
+ before(:each) do
5
+ template = mock('Template')
6
+ template.stub!(:lookup_context).and_return(ActionView::LookupContext.new([]))
7
+ @table = Tableficate::Table.new(template, NobelPrizeWinner.joins(:nobel_prizes).limit(1), {}, {})
8
+ @table.column(:first_name)
9
+ end
10
+
11
+ it 'should look for a partial based on the input type and use it if found' do
12
+ @table.template.lookup_context.stub(:exists?).and_return(true)
13
+
14
+ Tableficate::Filter::Input.new(@table, :first_name, type: 'search').template.should == 'filters/input_search'
15
+ end
16
+
17
+ it 'should look for a partial based on the input type and use the default if it is not found' do
18
+ @table.template.lookup_context.should_receive(:exists?) do |*args|
19
+ (args.first == 'tableficate/filters/input')
20
+ end
21
+
22
+ Tableficate::Filter::Input.new(@table, :first_name, type: 'search').template.should == 'filters/input'
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'genspec'
3
+
4
+ describe 'tableficate:table' do
5
+ with_args :foo do
6
+ it 'should generate app/tables/foo.rb' do
7
+ subject.should generate('app/tables/foo.rb') { |content|
8
+ content.should =~ /class Foo < Tableficate\:\:Base/
9
+ }
10
+ end
11
+ end
12
+
13
+ with_args :foo, :bars do
14
+ it 'should generate app/tables/foo.rb' do
15
+ subject.should generate('app/tables/foo.rb') { |content|
16
+ content.should =~ /scope \:bars/
17
+ }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ require 'genspec'
3
+
4
+ describe 'tableficate:theme' do
5
+ with_args :foo do
6
+ it 'should generate app/views/tableficate/foo/ with all files' do
7
+ subject.should generate('app/views/tableficate/foo')
8
+ subject.should generate('app/views/tableficate/foo/_table.html.erb')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,164 @@
1
+ require 'spec_helper'
2
+ include Tableficate::Helper
3
+
4
+ describe Tableficate::Helper, type: :request do
5
+ describe 'tableficate_select_tag' do
6
+ it 'allows for a collection to accept a string' do
7
+ visit '/filters/select_from_string'
8
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[text()='Peace']")
9
+ end
10
+
11
+ it 'selects an option from the params when the collection is a string' do
12
+ visit '/filters/select_from_string?nobel_prize_winners[filter][category]=Peace'
13
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[text()='Peace'][@selected='selected']")
14
+ end
15
+
16
+ it 'should display a multi select box from a string' do
17
+ visit '/filters/multi_select_from_string'
18
+
19
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
20
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[text()='#{category}']")
21
+ page.should have_no_xpath("//select[@id='nobel_prize_winners_filter_category']/option[text()='#{category}'][@selected='selected']")
22
+ end
23
+ end
24
+
25
+ it 'should display a multi select box and select an option' do
26
+ visit '/filters/multi_select_from_string?nobel_prize_winners[filter][category][]=Peace&nobel_prize_winners[filter][category][]=Literature'
27
+
28
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
29
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[text()='#{category}']")
30
+ if category == 'Peace' or category == 'Literature'
31
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[text()='#{category}'][@selected='selected']")
32
+ else
33
+ page.should have_no_xpath("//select[@id='nobel_prize_winners_filter_category']/option[text()='#{category}'][@selected='selected']")
34
+ end
35
+ end
36
+ end
37
+
38
+ it 'should display a select box' do
39
+ visit '/filters/select_tag'
40
+
41
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
42
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}']")
43
+ page.should have_no_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}'][@selected='selected']")
44
+ end
45
+ end
46
+
47
+ it 'should display a select box and select an option' do
48
+ visit '/filters/select_tag?nobel_prize_winners[filter][category]=Peace'
49
+
50
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
51
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}']")
52
+ if category == 'Peace'
53
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}'][@selected='selected']")
54
+ else
55
+ page.should have_no_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}'][@selected='selected']")
56
+ end
57
+ end
58
+ end
59
+
60
+ it 'should display a multi select box' do
61
+ visit '/filters/multi_select_tag'
62
+
63
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
64
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}']")
65
+ page.should have_no_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}'][@selected='selected']")
66
+ end
67
+ end
68
+
69
+ it 'should display a select box and select an option' do
70
+ visit '/filters/multi_select_tag?nobel_prize_winners[filter][category][]=Peace&nobel_prize_winners[filter][category][]=Literature'
71
+
72
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
73
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}']")
74
+ if category == 'Peace' or category == 'Literature'
75
+ page.should have_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}'][@selected='selected']")
76
+ else
77
+ page.should have_no_xpath("//select[@id='nobel_prize_winners_filter_category']/option[@value='#{category}'][@selected='selected']")
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'tableficate_radio_tags' do
84
+ it 'takes a block for custom output' do
85
+ visit '/filters/radio_tags?theme=custom_radio_block'
86
+
87
+ page.html.should match /<label for="nobel_prize_winners_filter_category_Chemistry">Chemistry<\/label>\s*<input id="nobel_prize_winners_filter_category_Chemistry" name="nobel_prize_winners\[filter\]\[category\]" type="radio" value="Chemistry">/
88
+ end
89
+
90
+ it 'should display a group of radio tags with no selection and nothing filtered' do
91
+ visit '/filters/radio_tags'
92
+
93
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
94
+ id = "nobel_prize_winners_filter_category_#{category.gsub(/ /, '_')}"
95
+
96
+ page.should have_xpath("//input[@type='radio'][@id='#{id}'][@value='#{category}']")
97
+
98
+ page.has_no_checked_field?(id).should be true
99
+ end
100
+
101
+ page.should have_xpath('//table/tbody/tr', count: NobelPrizeWinner.joins(:nobel_prizes).size)
102
+ end
103
+
104
+ it 'should display a group of radio tags with a selection and the table filtered' do
105
+ selected_category = 'Peace'
106
+ visit "/filters/radio_tags?nobel_prize_winners[filter][category]=#{selected_category}"
107
+
108
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
109
+ id = "nobel_prize_winners_filter_category_#{category.gsub(/ /, '_')}"
110
+
111
+ page.should have_xpath("//input[@type='radio'][@id='#{id}'][@value='#{category}']")
112
+
113
+ if category == selected_category
114
+ page.has_checked_field?(id).should be true
115
+ else
116
+ page.has_no_checked_field?(id).should be true
117
+ end
118
+ end
119
+
120
+ page.should have_xpath('//table/tbody/tr', count: NobelPrizeWinner.joins(:nobel_prizes).where('nobel_prizes.category = ?', selected_category).size)
121
+ end
122
+ end
123
+
124
+ describe 'tableficate_check_box_tags' do
125
+ it 'takes a block for custom output' do
126
+ visit '/filters/check_box_tags?theme=custom_check_box_block'
127
+
128
+ page.html.should match /<label for="nobel_prize_winners_filter_category_Chemistry">Chemistry<\/label>\s*<input id="nobel_prize_winners_filter_category_Chemistry" name="nobel_prize_winners\[filter\]\[category\]\[\]" type="checkbox" value="Chemistry">/
129
+ end
130
+
131
+ it 'should display a group of check box tags with no selection and nothing filtered' do
132
+ visit '/filters/check_box_tags'
133
+
134
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
135
+ id = "nobel_prize_winners_filter_category_#{category.gsub(/ /, '_')}"
136
+
137
+ page.should have_xpath("//input[@type='checkbox'][@id='#{id}'][@value='#{category}']")
138
+
139
+ page.has_no_checked_field?(id).should be true
140
+ end
141
+
142
+ page.should have_xpath('//table/tbody/tr', count: NobelPrizeWinner.joins(:nobel_prizes).size)
143
+ end
144
+
145
+ it 'should display a group of check box tags with a selection and the table filtered' do
146
+ selected_category = 'Peace'
147
+ visit "/filters/check_box_tags?nobel_prize_winners[filter][category]=#{selected_category}"
148
+
149
+ ['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine'].each do |category|
150
+ id = "nobel_prize_winners_filter_category_#{category.gsub(/ /, '_')}"
151
+
152
+ page.should have_xpath("//input[@type='checkbox'][@id='#{id}'][@value='#{category}']")
153
+
154
+ if category == selected_category
155
+ page.has_checked_field?(id).should be true
156
+ else
157
+ page.has_no_checked_field?(id).should be true
158
+ end
159
+ end
160
+
161
+ page.should have_xpath('//table/tbody/tr', count: NobelPrizeWinner.joins(:nobel_prizes).where('nobel_prizes.category = ?', selected_category).size)
162
+ end
163
+ end
164
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
1
  ENV["RAILS_ENV"] ||= 'test'
2
2
 
3
3
  require File.expand_path("../test_app/config/environment", __FILE__)
4
+
5
+ require 'capybara/rspec'
6
+ require 'capybara/rails'