with_filters 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. data/.gitignore +8 -0
  2. data/.yardopts +1 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/README.md +63 -0
  6. data/Rakefile +1 -0
  7. data/app/views/with_filters/_filter_form.html.erb +14 -0
  8. data/app/views/with_filters/filter/_check_box.html.erb +2 -0
  9. data/app/views/with_filters/filter/_check_boxes.html.erb +5 -0
  10. data/app/views/with_filters/filter/_radio.html.erb +5 -0
  11. data/app/views/with_filters/filter/_select.html.erb +2 -0
  12. data/app/views/with_filters/filter/_select_range.html.erb +4 -0
  13. data/app/views/with_filters/filter/_text.html.erb +2 -0
  14. data/app/views/with_filters/filter/_text_range.html.erb +4 -0
  15. data/changelog.md +2 -0
  16. data/lib/generators/with_filters/theme/theme_generator.rb +43 -0
  17. data/lib/with_filters/action_view_extension.rb +110 -0
  18. data/lib/with_filters/active_record_extension.rb +26 -0
  19. data/lib/with_filters/active_record_model_extension.rb +163 -0
  20. data/lib/with_filters/engine.rb +5 -0
  21. data/lib/with_filters/hash_extraction.rb +31 -0
  22. data/lib/with_filters/models/action.rb +14 -0
  23. data/lib/with_filters/models/filter/base.rb +36 -0
  24. data/lib/with_filters/models/filter/base_range.rb +42 -0
  25. data/lib/with_filters/models/filter/check_box.rb +20 -0
  26. data/lib/with_filters/models/filter/choice.rb +23 -0
  27. data/lib/with_filters/models/filter/collection.rb +28 -0
  28. data/lib/with_filters/models/filter/filter.rb +59 -0
  29. data/lib/with_filters/models/filter/radio.rb +7 -0
  30. data/lib/with_filters/models/filter/select.rb +22 -0
  31. data/lib/with_filters/models/filter/select_range.rb +15 -0
  32. data/lib/with_filters/models/filter/text.rb +30 -0
  33. data/lib/with_filters/models/filter/text_range.rb +15 -0
  34. data/lib/with_filters/models/filter_form.rb +93 -0
  35. data/lib/with_filters/value_prep/boolean_prep.rb +10 -0
  36. data/lib/with_filters/value_prep/date_prep.rb +10 -0
  37. data/lib/with_filters/value_prep/date_time_prep.rb +51 -0
  38. data/lib/with_filters/value_prep/default_prep.rb +88 -0
  39. data/lib/with_filters/value_prep/value_prep.rb +28 -0
  40. data/lib/with_filters/version.rb +3 -0
  41. data/lib/with_filters.rb +32 -0
  42. data/spec/active_record_model_extension_spec.rb +435 -0
  43. data/spec/dummy/README.rdoc +261 -0
  44. data/spec/dummy/Rakefile +7 -0
  45. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  46. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  47. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  48. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  49. data/spec/dummy/app/mailers/.gitkeep +0 -0
  50. data/spec/dummy/app/models/.gitkeep +0 -0
  51. data/spec/dummy/app/models/date_time_tester.rb +2 -0
  52. data/spec/dummy/app/models/field_format_tester.rb +2 -0
  53. data/spec/dummy/app/models/nobel_prize.rb +3 -0
  54. data/spec/dummy/app/models/nobel_prize_winner.rb +3 -0
  55. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  56. data/spec/dummy/config/application.rb +56 -0
  57. data/spec/dummy/config/boot.rb +10 -0
  58. data/spec/dummy/config/database.yml +25 -0
  59. data/spec/dummy/config/environment.rb +5 -0
  60. data/spec/dummy/config/environments/development.rb +37 -0
  61. data/spec/dummy/config/environments/production.rb +67 -0
  62. data/spec/dummy/config/environments/test.rb +37 -0
  63. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  64. data/spec/dummy/config/initializers/inflections.rb +15 -0
  65. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  66. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  67. data/spec/dummy/config/initializers/session_store.rb +8 -0
  68. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  69. data/spec/dummy/config/locales/en.yml +5 -0
  70. data/spec/dummy/config/routes.rb +58 -0
  71. data/spec/dummy/config.ru +4 -0
  72. data/spec/dummy/db/migrate/20111227224959_additional_columns.rb +73 -0
  73. data/spec/dummy/db/migrate/20120127203225_create_nobel_prize_winners.rb +30 -0
  74. data/spec/dummy/db/migrate/20120203212237_create_nobel_prizes.rb +34 -0
  75. data/spec/dummy/db/migrate/20120209051208_modify_updated_at.rb +13 -0
  76. data/spec/dummy/db/migrate/20120210163052_change_created_at.rb +17 -0
  77. data/spec/dummy/db/migrate/20120214172946_fix_einstein.rb +9 -0
  78. data/spec/dummy/db/migrate/20120227200013_create_date_time_testers.rb +25 -0
  79. data/spec/dummy/db/migrate/20120309202722_create_field_format_testers.rb +22 -0
  80. data/spec/dummy/db/migrate/20120310195447_update_field_format_testers.rb +15 -0
  81. data/spec/dummy/db/schema.rb +51 -0
  82. data/spec/dummy/db/test.sqlite3 +0 -0
  83. data/spec/dummy/lib/assets/.gitkeep +0 -0
  84. data/spec/dummy/log/.gitkeep +0 -0
  85. data/spec/dummy/public/404.html +26 -0
  86. data/spec/dummy/public/422.html +26 -0
  87. data/spec/dummy/public/500.html +25 -0
  88. data/spec/dummy/public/favicon.ico +0 -0
  89. data/spec/dummy/script/rails +6 -0
  90. data/spec/generators/with_filters_theme_spec.rb +33 -0
  91. data/spec/hash_extraction_spec.rb +51 -0
  92. data/spec/helpers/action_view_extension_spec.rb +345 -0
  93. data/spec/models/action.rb +20 -0
  94. data/spec/models/filter/base_range_spec.rb +32 -0
  95. data/spec/models/filter/base_spec.rb +76 -0
  96. data/spec/models/filter/check_box_spec.rb +36 -0
  97. data/spec/models/filter/choice_spec.rb +24 -0
  98. data/spec/models/filter/collection_spec.rb +72 -0
  99. data/spec/models/filter/filter_spec.rb +35 -0
  100. data/spec/models/filter/select_spec.rb +12 -0
  101. data/spec/models/filter/text_spec.rb +16 -0
  102. data/spec/models/filter_form_spec.rb +212 -0
  103. data/spec/spec_helper.rb +12 -0
  104. data/spec/value_prep/boolean_prep_spec.rb +13 -0
  105. data/spec/value_prep/date_prep_spec.rb +28 -0
  106. data/spec/value_prep/date_time_prep_spec.rb +106 -0
  107. data/spec/value_prep/default_prep_spec.rb +43 -0
  108. data/with_filters.gemspec +27 -0
  109. metadata +280 -0
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::Filter::BaseStart do
4
+ subject {described_class.new(:year, :foo, '')}
5
+
6
+ its(:field_name) {should == 'foo[year][start]'}
7
+ end
8
+
9
+ describe WithFilters::Filter::BaseStop do
10
+ subject {described_class.new(:year, :foo, '')}
11
+
12
+ its(:field_name) {should == 'foo[year][stop]'}
13
+ end
14
+
15
+ describe WithFilters::Filter::BaseRange do
16
+ subject {described_class.new(:year, :foo, {})}
17
+
18
+ its(:start) {should be_an_instance_of(WithFilters::Filter::BaseStart)}
19
+ its(:stop) {should be_an_instance_of(WithFilters::Filter::BaseStop)}
20
+
21
+ context 'options' do
22
+ it 'uses the :start and :stop option hashes for the individual filters and default any values not passed to the range options provided' do
23
+ filter = described_class.new(:year, :foo, {}, label: 'Nobel Prize Won In')
24
+ filter.start.label.should == 'Nobel Prize Won In'
25
+ filter.stop.label.should == 'Nobel Prize Won In'
26
+
27
+ filter = described_class.new(:year, :foo, {}, label: 'Nobel Prize Won In', start: {label: 'Nobel Prize Won Between'}, stop: {label: 'and'})
28
+ filter.start.label.should == 'Nobel Prize Won Between'
29
+ filter.stop.label.should == 'and'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::Filter::Base do
4
+ describe '#initialize(name, namespace, value, options = {})' do
5
+ context 'defaults' do
6
+ subject {described_class.new(:first_name, :foo, 'Aaron')}
7
+
8
+ its(:to_partial_path) {should == File.join('with_filters', 'filter', 'base')}
9
+ its(:label) {should == 'First Name'}
10
+ its(:label_attrs) {should == {}}
11
+ its(:field_name) {should == 'foo[first_name]'}
12
+ its(:value) {should == 'Aaron'}
13
+ it 'has no collection' do
14
+ subject.collection.should be_nil
15
+ end
16
+ end
17
+
18
+ context 'options' do
19
+ context ':field_name' do
20
+ it 'manually sets the field name' do
21
+ field_name = 'fname'
22
+ described_class.new(:first_name, :foo, 'Aaron', field_name: field_name).field_name.should == field_name
23
+ end
24
+ end
25
+
26
+ context ':label' do
27
+ it 'uses the provided label' do
28
+ label = 'Given Name'
29
+ described_class.new(:first_name, :foo, 'Aaron', label: label).label.should == label
30
+ end
31
+ end
32
+
33
+ context ':label_attrs' do
34
+ it 'uses the provided label attrs' do
35
+ label_attrs = {
36
+ class: 'label_class'
37
+ }
38
+ described_class.new(:first_name, :foo, 'Aaron', label_attrs: label_attrs).label_attrs.should == label_attrs
39
+ end
40
+ end
41
+
42
+ context ':collection' do
43
+ it 'creates collection from the provided list' do
44
+ collection = described_class.new(:gender, :foo, 'Male', collection: ['Male', 'Female']).collection
45
+ collection.should be_a_kind_of(WithFilters::Filter::Collection)
46
+ collection.first.label.should == 'Male'
47
+ collection.last.label.should == 'Female'
48
+ end
49
+ end
50
+
51
+ context ':theme' do
52
+ it 'uses the provided theme to create a partial path' do
53
+ Dir.stub(:glob).and_return([File.join(Rails.root, 'app', 'views', 'with_filters', 'foo', 'filter', '_base.html.erb')])
54
+
55
+ filter = described_class.new(:first_name, :foo, 'Aaron', theme: 'foo')
56
+
57
+ filter.to_partial_path.should == File.join('with_filters', 'foo', 'filter', 'base')
58
+ end
59
+
60
+ it 'falls back to the original partials if the theme version can not be found' do
61
+ Dir.stub(:glob).and_return([])
62
+
63
+ filter = described_class.new(:first_name, :foo, 'Aaron', theme: 'foo')
64
+
65
+ filter.to_partial_path.should == File.join('with_filters', 'filter', 'base')
66
+ end
67
+ end
68
+
69
+ context 'everything else' do
70
+ it 'gets put in attrs' do
71
+ described_class.new(:first_name, :foo, 'Aaron', {label: 'Given Name', class: 'input_class'}).attrs.should == {class: 'input_class'}
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::Filter::CheckBox do
4
+ describe '#initialize(name, namespace, value, options = {})' do
5
+ context 'without collection' do
6
+ subject {described_class.new(:active, :foo, 'on')}
7
+
8
+ its(:to_partial_path) {should == 'with_filters/filter/check_box'}
9
+
10
+ context 'and value is "on"' do
11
+ its(:selected?) {should be true}
12
+ end
13
+
14
+ context 'and value is "off"' do
15
+ subject {described_class.new(:active, :foo, 'off')}
16
+ its(:selected?) {should be false}
17
+ end
18
+ end
19
+
20
+ context 'with collection' do
21
+ let(:collection) {['Chemistry', 'Literature', 'Peace', 'Physics', 'Physiology or Medicine']}
22
+ subject {described_class.new(:categories, :foo, ['Chemistry', 'Physics'], collection: collection)}
23
+
24
+ its(:to_partial_path) {should == 'with_filters/filter/check_boxes'}
25
+
26
+ context 'and there are values' do
27
+ its(:selected?) {should be true}
28
+ end
29
+
30
+ context 'and there are not values' do
31
+ subject {described_class.new(:categories, :foo, [], collection: collection)}
32
+ its(:selected?) {should be false}
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::Filter::Choice do
4
+ describe '#initialize(field_name, label, value, options = {})' do
5
+ context 'default' do
6
+ subject {described_class.new('field', 'One', 1)}
7
+
8
+ its(:field_name) {should == 'field[]'}
9
+ its(:label) {should == 'One'}
10
+ its(:value) {should be 1}
11
+ its(:selected?) {should be_false}
12
+ its(:attrs) {should == {id: 'field_1'}}
13
+ end
14
+
15
+ context 'options' do
16
+ context ':selected' do
17
+ subject {described_class.new('field', 'One', 1, selected: true)}
18
+
19
+ its(:selected?) {should be_true}
20
+ its(:attrs) {should == {id: 'field_1'}}
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::Filter::Collection do
4
+ describe '#initialize(field_name, choices, options = {})' do
5
+ context 'choices' do
6
+ context 'is an Array ([1,2,3])' do
7
+ let(:values) {[1,2,3]}
8
+ subject {described_class.new('field', values)}
9
+
10
+ it 'should create Choice objects' do
11
+ values.each_with_index do |v, i|
12
+ subject[i].label.should == v.to_s
13
+ subject[i].value.should == v
14
+ end
15
+ end
16
+ end
17
+
18
+ context 'is an Array of Arrays ([[:one, 1], [:two, 2], [:three, 3]])' do
19
+ let(:values) {[[:one, 1], [:two, 2], [:three, 3]]}
20
+ subject {described_class.new('field', values)}
21
+
22
+ it 'should create Choice objects' do
23
+ values.each_with_index do |a, i|
24
+ k, v = a
25
+ subject[i].label.should == k.to_s
26
+ subject[i].value.should == v
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'is an Array of Arrays with options ([[:one, 1], [:two, 2, {class: "foo"}], [:three, 3]])' do
32
+ let(:values) {[[:one, 1], [:two, 2, {class: 'foo'}], [:three, 3]]}
33
+ subject {described_class.new('field', values)}
34
+
35
+ it 'should create Choice objects' do
36
+ values.each_with_index do |a, i|
37
+ k, v = a
38
+ subject[i].label.should == k.to_s
39
+ subject[i].value.should == v
40
+ subject[i].attrs[:class].should == 'foo' if i == 1
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'is a Range (1..3)' do
46
+ let(:values) {1..3}
47
+ subject {described_class.new('field', values)}
48
+
49
+ it 'should create Choice objects' do
50
+ values.to_a.each_with_index do |v, i|
51
+ subject[i].label.should == v.to_s
52
+ subject[i].value.should == v
53
+ end
54
+ end
55
+ end
56
+
57
+ context 'is a Hash ({one: 1, two: 2, three: 3})' do
58
+ let(:values) {{one: 1, two: 2, three: 3}}
59
+ subject {described_class.new('field', values)}
60
+
61
+ it 'should create Choice objects' do
62
+ i = 0
63
+ values.each do |k, v|
64
+ subject[i].label.should == k.to_s
65
+ subject[i].value.should == v
66
+ i += 1
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::Filter do
4
+ describe '#create(name, namespace, value, options = {})' do
5
+ context 'options' do
6
+ context ':as' do
7
+ subject {described_class.create(:email, :foo, '', as: :email)}
8
+
9
+ it 'sets the input type' do
10
+ subject.attrs[:type].should == 'email'
11
+ end
12
+
13
+ it 'returns a filter based on the type' do
14
+ subject.should be_an_instance_of(WithFilters::Filter::Text)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ describe '#create_range(name, namespace, value, options = {})' do
21
+ context 'options' do
22
+ context ':as' do
23
+ subject {described_class.create_range(:year, :foo, {}, as: :number)}
24
+
25
+ it 'sets the input type' do
26
+ subject.attrs[:type].should == 'number'
27
+ end
28
+
29
+ it 'returns a filter based on the type' do
30
+ subject.should be_an_instance_of(WithFilters::Filter::TextRange)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::Filter::Select do
4
+ describe '#initialize(name, namespace, value, options = {})' do
5
+ context 'options' do
6
+ it 'allows :collection to accept a string' do
7
+ subject = described_class.new(:gender, :foo, 'Male', collection: '<option>Male</option><option>Female</option>')
8
+ subject.collection.should == '<option selected="selected">Male</option><option>Female</option>'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::Filter::Text do
4
+ it 'should look for a partial based on the input type and use it if found' do
5
+ described_class.new(:email, :foo, '').to_partial_path.should == File.join('with_filters', 'filter', 'text')
6
+ end
7
+
8
+ it 'should look for a partial based on the input type and use the default if it is not found' do
9
+ # there must be a better way to do this
10
+ Dir.stub(:glob).and_return([File.join('with_filters', 'filter', '_text_as_email.html.erb')])
11
+ described_class.new(:email, :foo, '', type: :email).to_partial_path.should == File.join('with_filters', 'filter', 'text_as_email')
12
+
13
+ Dir.stub(:glob).and_return([])
14
+ described_class.new(:email, :foo, '', type: :email).to_partial_path.should == File.join('with_filters', 'filter', 'text')
15
+ end
16
+ end
@@ -0,0 +1,212 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::FilterForm do
4
+ describe '#initialize(records, params = {}, options = {})' do
5
+ context 'defaults' do
6
+ subject {described_class.new(NobelPrizeWinner.with_filters)}
7
+
8
+ its(:attrs) {should == {novalidate: 'novalidate', method: 'get'}}
9
+ its(:to_partial_path) {should == File.join('with_filters', 'filter_form')}
10
+ its(:param_namespace) {should be :nobel_prize_winners}
11
+ its(:filters) {should be_empty}
12
+ its(:hidden_filters) {should be_empty}
13
+ its(:actions) {should be_empty}
14
+ end
15
+
16
+ context 'options' do
17
+ context ':attrs' do
18
+ it 'attrs should override the defaults' do
19
+ described_class.new(NobelPrizeWinner.with_filters, {}, method: 'post').attrs.should == {novalidate: 'novalidate', method: 'post'}
20
+ end
21
+ end
22
+
23
+ context ':theme' do
24
+ it 'passes the theme to the inputs' do
25
+ path = [Rails.root, 'app', 'views', 'with_filters', 'foo', 'filter']
26
+ Dir.stub(:glob).and_return([])
27
+ Dir.should_receive(:glob).
28
+ with(File.join(*path, '_text.*')).
29
+ and_return([File.join(*path, 'text')])
30
+
31
+ ff = described_class.new(NobelPrizeWinner.with_filters, {}, theme: 'foo')
32
+ ff.input(:first_name)
33
+
34
+ ff.filters.first.to_partial_path.should == File.join('with_filters', 'foo', 'filter', 'text')
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#hidden(name, options = {})' do
41
+ it 'adds a hidden filter' do
42
+ ff = described_class.new(NobelPrizeWinner.with_filters)
43
+ ff.hidden(:hidden)
44
+
45
+ ff.hidden_filters.length.should == 1
46
+ ff.hidden_filters.first.should be_a_kind_of(WithFilters::Filter::Text)
47
+ ff.hidden_filters.first.attrs[:type].should == 'hidden'
48
+ end
49
+ end
50
+
51
+ describe '#input(name, options = {})' do
52
+ it 'adds a filter' do
53
+ label = 'Given Name'
54
+ ff = described_class.new(NobelPrizeWinner.with_filters)
55
+ ff.input(:first_name, label: label)
56
+
57
+ ff.filters.length.should == 1
58
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::Base)
59
+ ff.filters.first.label.should == label
60
+ end
61
+
62
+ let(:ff) {described_class.new(FieldFormatTester.with_filters)}
63
+
64
+ context 'the type is :hidden' do
65
+ it 'adds to the hidden_filters list' do
66
+ ff.input(:foo, as: :hidden)
67
+
68
+ ff.filters.should be_empty
69
+ ff.hidden_filters[0].should be_a_kind_of(WithFilters::Filter::Text)
70
+ ff.hidden_filters[0].attrs[:type].should == 'hidden'
71
+ end
72
+ end
73
+
74
+ context 'the database field is an integer, float or decimal' do
75
+ it 'uses a number filter' do
76
+ ff.input(:integer_field)
77
+ ff.input(:float_field)
78
+ ff.input(:decimal_field)
79
+
80
+ ff.filters[0].should be_a_kind_of(WithFilters::Filter::Text)
81
+ ff.filters[0].attrs[:type].should == 'number'
82
+ ff.filters[1].should be_a_kind_of(WithFilters::Filter::Text)
83
+ ff.filters[1].attrs[:type].should == 'number'
84
+ ff.filters[2].should be_a_kind_of(WithFilters::Filter::Text)
85
+ ff.filters[2].attrs[:type].should == 'number'
86
+ end
87
+ end
88
+
89
+ context 'the database field is a date' do
90
+ it 'uses a date filter' do
91
+ ff.input(:date_field)
92
+
93
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::Text)
94
+ ff.filters.first.attrs[:type].should == 'date'
95
+ end
96
+ end
97
+
98
+ context 'the database field is a time' do
99
+ it 'uses a time filter' do
100
+ ff.input(:time_field)
101
+
102
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::Text)
103
+ ff.filters.first.attrs[:type].should == 'time'
104
+ end
105
+ end
106
+
107
+ context 'the database field is a datetime or timestamp' do
108
+ it 'uses a time filter' do
109
+ ff.input(:datetime_field)
110
+ ff.input(:timestamp_field)
111
+
112
+ ff.filters[0].should be_a_kind_of(WithFilters::Filter::Text)
113
+ ff.filters[0].attrs[:type].should == 'datetime'
114
+ ff.filters[1].should be_a_kind_of(WithFilters::Filter::Text)
115
+ ff.filters[1].attrs[:type].should == 'datetime'
116
+ end
117
+ end
118
+
119
+ context 'the database field is a boolean' do
120
+ it 'uses a checkbox filter' do
121
+ ff.input(:boolean_field)
122
+
123
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::CheckBox)
124
+ ff.filters.first.attrs[:type].should == 'checkbox'
125
+ end
126
+ end
127
+
128
+ context 'the database field is text' do
129
+ context 'and the name includes "email"' do
130
+ it 'uses an email filter' do
131
+ ff.input(:email_field)
132
+
133
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::Text)
134
+ ff.filters.first.attrs[:type].should == 'email'
135
+ end
136
+ end
137
+
138
+ context 'and the name includes "phone"' do
139
+ it 'uses a tel filter' do
140
+ ff.input(:phone_field)
141
+
142
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::Text)
143
+ ff.filters.first.attrs[:type].should == 'tel'
144
+ end
145
+ end
146
+
147
+ context 'and the name includes "url"' do
148
+ it 'uses a url filter' do
149
+ ff.input(:url_field)
150
+
151
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::Text)
152
+ ff.filters.first.attrs[:type].should == 'url'
153
+ end
154
+ end
155
+ end
156
+
157
+ context 'options' do
158
+ context ':collection' do
159
+ it 'defaults to a select filter' do
160
+ ff = described_class.new(FieldFormatTester.with_filters)
161
+ ff.input(:text_field)
162
+
163
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::Text)
164
+ ff.filters.first.attrs[:type].should == 'text'
165
+ end
166
+ end
167
+
168
+ context 'no :collection' do
169
+ it 'defaults to a select filter' do
170
+ ff = described_class.new(FieldFormatTester.with_filters)
171
+ ff.input(:text_field, collection: 1..10)
172
+
173
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::Select)
174
+ end
175
+ end
176
+ end
177
+ end
178
+
179
+ describe '#input_range(name, options = {})' do
180
+ it 'adds a ranged filter' do
181
+ label = 'Awarded'
182
+ ff = described_class.new(NobelPrizeWinner.with_filters)
183
+ ff.input_range(:year, label: label)
184
+
185
+ ff.filters.length.should == 1
186
+ ff.filters.first.should be_a_kind_of(WithFilters::Filter::BaseRange)
187
+ ff.filters.first.start.label.should == label
188
+ end
189
+ end
190
+
191
+ describe '#action(type, options = {})' do
192
+ context 'type' do
193
+ context ':submit' do
194
+ it 'adds an action' do
195
+ ff = described_class.new(NobelPrizeWinner.with_filters)
196
+ ff.action(:submit)
197
+
198
+ ff.actions.first.should be_a_kind_of(WithFilters::Action)
199
+ end
200
+ end
201
+
202
+ context ':reset' do
203
+ it 'adds an action' do
204
+ ff = described_class.new(NobelPrizeWinner.with_filters)
205
+ ff.action(:reset)
206
+
207
+ ff.actions.first.should be_a_kind_of(WithFilters::Action)
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,12 @@
1
+ # Configure Rails Environment
2
+ ENV['RAILS_ENV'] = 'test'
3
+
4
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
5
+
6
+ require 'rspec/rails'
7
+ require 'capybara/rspec'
8
+
9
+ Rails.backtrace_cleaner.remove_silencers!
10
+
11
+ # Load support files
12
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::ValuePrep::BooleanPrep do
4
+ describe '#value' do
5
+ it 'turns "on" into `true`' do
6
+ described_class.new('on').value.should be true
7
+ end
8
+
9
+ it 'turns "off" into `false`' do
10
+ described_class.new('off').value.should be false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::ValuePrep::DatePrep do
4
+ describe '#value' do
5
+ context 'value is a String' do
6
+ it 'returns a Date' do
7
+ described_class.new('19140325').value.should be_an_instance_of(Date)
8
+ end
9
+ end
10
+
11
+ context 'value is an Array' do
12
+ it 'returns an array of Date objects' do
13
+ described_class.new(['19140325', '19140326']).value.each do |v|
14
+ v.should be_an_instance_of(Date)
15
+ end
16
+ end
17
+ end
18
+
19
+ context 'value is a Hash' do
20
+ it 'returns a Hash of Date objects' do
21
+ value = described_class.new({start: '19140325', stop: '19140326'}).value
22
+
23
+ value[:start].should be_an_instance_of(Date)
24
+ value[:stop].should be_an_instance_of(Date)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe WithFilters::ValuePrep::DateTimePrep do
4
+ describe '#value' do
5
+ context 'is a String representing a datetime' do
6
+ context 'to a fraction of a second' do
7
+ it 'returns a value' do
8
+ datetime = '1914-03-25 12:34:56.123456'
9
+ described_class.new(datetime).value.should == Time.zone.parse(datetime).to_s(:db) + '.123456'
10
+ end
11
+ end
12
+
13
+ context 'to a second' do
14
+ it 'returns a value' do
15
+ datetime = '1914-03-25 12:34:56'
16
+ value = described_class.new(datetime).value
17
+
18
+ value[:start].should == Time.zone.parse(datetime).to_s(:db)
19
+ value[:stop].should == Time.zone.parse(datetime).advance(seconds: 1).to_s(:db)
20
+ end
21
+ end
22
+
23
+ context 'to a minute' do
24
+ it 'returns a value' do
25
+ datetime = '1914-03-25 12:34'
26
+ value = described_class.new(datetime).value
27
+
28
+ value[:start].should == Time.zone.parse(datetime).to_s(:db)
29
+ value[:stop].should == Time.zone.parse(datetime).advance(minutes: 1).to_s(:db)
30
+ end
31
+ end
32
+
33
+ context 'to an hour' do
34
+ it 'returns a value' do
35
+ datetime = '1914-03-25 12'
36
+ value = described_class.new(datetime).value
37
+
38
+ value[:start].should == Time.zone.parse(datetime).to_s(:db)
39
+ value[:stop].should == Time.zone.parse(datetime).advance(hours: 1).to_s(:db)
40
+ end
41
+ end
42
+
43
+ context 'to a day' do
44
+ it 'returns a value' do
45
+ date = '1914-03-25'
46
+ value = described_class.new(date).value
47
+
48
+ value[:start].should == Time.zone.parse(date).to_s(:db)
49
+ value[:stop].should == Time.zone.parse(date).advance(days: 1).to_s(:db)
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'is a Hash representing a datetime range' do
55
+ context 'to a fraction of a second' do
56
+ it 'returns a Hash of values' do
57
+ datetimes = {start: '1914-03-25 12:34:56.123456', stop: '1914-03-26 12:34:56.654321'}
58
+ value = described_class.new(datetimes).value
59
+
60
+ value[:start].should == Time.zone.parse(datetimes[:start]).to_s(:db) + '.123456'
61
+ value[:stop].should == Time.zone.parse(datetimes[:stop]).to_s(:db) + '.654321'
62
+ end
63
+ end
64
+
65
+ context 'to a second' do
66
+ it 'returns a Hash of values' do
67
+ datetimes = {start: '1914-03-25 12:34:56', stop: '1914-03-26 12:34:56'}
68
+ value = described_class.new(datetimes).value
69
+
70
+ value[:start].should == Time.zone.parse(datetimes[:start]).to_s(:db)
71
+ value[:stop].should == Time.zone.parse(datetimes[:stop]).advance(seconds: 1).to_s(:db)
72
+ end
73
+ end
74
+
75
+ context 'to a minute' do
76
+ it 'returns a Hash of values' do
77
+ datetimes = {start: '1914-03-25 12:34', stop: '1914-03-26 12:34'}
78
+ value = described_class.new(datetimes).value
79
+
80
+ value[:start].should == Time.zone.parse(datetimes[:start]).to_s(:db)
81
+ value[:stop].should == Time.zone.parse(datetimes[:stop]).advance(minutes: 1).to_s(:db)
82
+ end
83
+ end
84
+
85
+ context 'to an hour' do
86
+ it 'returns a Hash of values' do
87
+ datetimes = {start: '1914-03-25 12', stop: '1914-03-26 12'}
88
+ value = described_class.new(datetimes).value
89
+
90
+ value[:start].should == Time.zone.parse(datetimes[:start]).to_s(:db)
91
+ value[:stop].should == Time.zone.parse(datetimes[:stop]).advance(hours: 1).to_s(:db)
92
+ end
93
+ end
94
+
95
+ context 'to a day' do
96
+ it 'returns a Hash of values' do
97
+ dates = {start: '19140325', stop: '19140326'}
98
+ date_time_prep = described_class.new(dates)
99
+
100
+ date_time_prep.value[:start].should == Time.zone.parse(dates[:start]).to_s(:db)
101
+ date_time_prep.value[:stop].should == Time.zone.parse(dates[:stop]).advance(days: 1).to_s(:db)
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end