validates_timeliness 2.3.2 → 3.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/CHANGELOG +12 -4
  2. data/LICENSE +1 -1
  3. data/README.rdoc +138 -280
  4. data/Rakefile +30 -16
  5. data/lib/generators/validates_timeliness/install_generator.rb +17 -0
  6. data/lib/generators/validates_timeliness/templates/en.yml +16 -0
  7. data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +22 -0
  8. data/lib/validates_timeliness.rb +46 -52
  9. data/lib/validates_timeliness/attribute_methods.rb +51 -0
  10. data/lib/validates_timeliness/conversion.rb +69 -0
  11. data/lib/validates_timeliness/extensions.rb +14 -0
  12. data/lib/validates_timeliness/extensions/date_time_select.rb +45 -0
  13. data/lib/validates_timeliness/extensions/multiparameter_handler.rb +31 -0
  14. data/lib/validates_timeliness/helper_methods.rb +41 -0
  15. data/lib/validates_timeliness/orms/active_record.rb +14 -0
  16. data/lib/validates_timeliness/parser.rb +389 -17
  17. data/lib/validates_timeliness/validator.rb +37 -200
  18. data/lib/validates_timeliness/version.rb +1 -1
  19. data/spec/model_helpers.rb +27 -0
  20. data/spec/spec_helper.rb +74 -43
  21. data/spec/test_model.rb +56 -0
  22. data/spec/validates_timeliness/attribute_methods_spec.rb +36 -0
  23. data/spec/validates_timeliness/conversion_spec.rb +204 -0
  24. data/spec/validates_timeliness/extensions/date_time_select_spec.rb +178 -0
  25. data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +21 -0
  26. data/spec/validates_timeliness/helper_methods_spec.rb +36 -0
  27. data/spec/{formats_spec.rb → validates_timeliness/parser_spec.rb} +105 -71
  28. data/spec/validates_timeliness/validator/after_spec.rb +59 -0
  29. data/spec/validates_timeliness/validator/before_spec.rb +59 -0
  30. data/spec/validates_timeliness/validator/is_at_spec.rb +63 -0
  31. data/spec/validates_timeliness/validator/on_or_after_spec.rb +59 -0
  32. data/spec/validates_timeliness/validator/on_or_before_spec.rb +59 -0
  33. data/spec/validates_timeliness/validator_spec.rb +172 -0
  34. data/validates_timeliness.gemspec +30 -0
  35. metadata +42 -40
  36. data/TODO +0 -8
  37. data/lib/validates_timeliness/action_view/instance_tag.rb +0 -52
  38. data/lib/validates_timeliness/active_record/attribute_methods.rb +0 -77
  39. data/lib/validates_timeliness/active_record/multiparameter_attributes.rb +0 -69
  40. data/lib/validates_timeliness/formats.rb +0 -368
  41. data/lib/validates_timeliness/locale/en.new.yml +0 -18
  42. data/lib/validates_timeliness/locale/en.old.yml +0 -18
  43. data/lib/validates_timeliness/matcher.rb +0 -1
  44. data/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb +0 -162
  45. data/lib/validates_timeliness/validation_methods.rb +0 -46
  46. data/spec/action_view/instance_tag_spec.rb +0 -194
  47. data/spec/active_record/attribute_methods_spec.rb +0 -157
  48. data/spec/active_record/multiparameter_attributes_spec.rb +0 -118
  49. data/spec/ginger_scenarios.rb +0 -19
  50. data/spec/parser_spec.rb +0 -65
  51. data/spec/resources/application.rb +0 -2
  52. data/spec/resources/person.rb +0 -3
  53. data/spec/resources/schema.rb +0 -10
  54. data/spec/resources/sqlite_patch.rb +0 -19
  55. data/spec/spec/rails/matchers/validate_timeliness_spec.rb +0 -245
  56. data/spec/time_travel/MIT-LICENSE +0 -20
  57. data/spec/time_travel/time_extensions.rb +0 -33
  58. data/spec/time_travel/time_travel.rb +0 -12
  59. data/spec/validator_spec.rb +0 -723
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::AttributeMethods do
4
+ it 'should define _timeliness_raw_value_for instance method' do
5
+ Person.instance_methods.should include('_timeliness_raw_value_for')
6
+ end
7
+
8
+ context "attribute write method" do
9
+ class EmployeeCopy < ActiveRecord::Base
10
+ set_table_name 'employees'
11
+ validates_datetime :birth_datetime
12
+ end
13
+
14
+ it 'should cache attribute raw value' do
15
+ r = EmployeeCopy.new
16
+ r.birth_datetime = date_string = '2010-01-01'
17
+ r._timeliness_raw_value_for(:birth_datetime).should == date_string
18
+ end
19
+ end
20
+
21
+ context "before_type_cast method" do
22
+ it 'should be defined on class if ORM supports it' do
23
+ Employee.instance_methods(false).should include("birth_datetime_before_type_cast")
24
+ end
25
+
26
+ it 'should not be defined if ORM does not support it' do
27
+ Person.instance_methods(false).should_not include("birth_datetime_before_type_cast")
28
+ end
29
+
30
+ it 'should return original value' do
31
+ r = Employee.new
32
+ r.birth_datetime = date_string = '2010-01-01'
33
+ r.birth_datetime_before_type_cast.should == date_string
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,204 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Conversion do
4
+ include ValidatesTimeliness::Conversion
5
+
6
+ before do
7
+ Timecop.freeze(Time.mktime(2010, 1, 1, 0, 0, 0))
8
+ end
9
+
10
+ describe "#type_cast_value" do
11
+ let(:options) { Hash.new }
12
+
13
+ describe "for date type" do
14
+ it "should return same value for date value" do
15
+ type_cast_value(Date.new(2010, 1, 1), :date).should == Date.new(2010, 1, 1)
16
+ end
17
+
18
+ it "should return date part of time value" do
19
+ type_cast_value(Time.mktime(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
20
+ end
21
+
22
+ it "should return date part of datetime value" do
23
+ type_cast_value(DateTime.new(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
24
+ end
25
+ end
26
+
27
+ describe "for time type" do
28
+ it "should return same value for time value matching dummy date part" do
29
+ type_cast_value(Time.utc(2000, 1, 1, 0, 0, 0), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
30
+ end
31
+
32
+ it "should return dummy time value with same time part for time value with different date" do
33
+ type_cast_value(Time.utc(2010, 1, 1, 0, 0, 0), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
34
+ end
35
+
36
+ it "should return dummy time only for date value" do
37
+ type_cast_value(Date.new(2010, 1, 1), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
38
+ end
39
+
40
+ it "should return dummy date with time part for datetime value" do
41
+ type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :time).should == Time.utc(2000, 1, 1, 12, 34, 56)
42
+ end
43
+ end
44
+
45
+ describe "for datetime type" do
46
+ it "should return Date as Time value" do
47
+ type_cast_value(Date.new(2010, 1, 1), :datetime).should == Time.local_time(2010, 1, 1, 0, 0, 0)
48
+ end
49
+
50
+ it "should return same Time value" do
51
+ value = Time.utc(2010, 1, 1, 12, 34, 56)
52
+ type_cast_value(Time.utc(2010, 1, 1, 12, 34, 56), :datetime).should == value
53
+ end
54
+
55
+ it "should return as Time with same component values" do
56
+ type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
57
+ end
58
+
59
+ it "should return same Time in correct zone if timezone aware" do
60
+ @timezone_aware = true
61
+ value = Time.utc(2010, 1, 1, 12, 34, 56)
62
+ result = type_cast_value(value, :datetime)
63
+ result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
64
+ result.zone.should == 'EST'
65
+ end
66
+ end
67
+
68
+ describe "ignore_usec option" do
69
+ let(:options) { {:ignore_usec => true} }
70
+
71
+ it "should ignore usec on time values when evaluated" do
72
+ value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
73
+ type_cast_value(value, :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
74
+ end
75
+
76
+ it "should ignore usec and return time in correct zone if timezone aware" do
77
+ @timezone_aware = true
78
+ value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
79
+ result = type_cast_value(value, :datetime)
80
+ result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
81
+ result.zone.should == 'EST'
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "#dummy_time" do
87
+ it 'should return Time with dummy date values but same time components' do
88
+ dummy_time(Time.utc(2010, 11, 22, 12, 34, 56)).should == Time.utc(2000, 1, 1, 12, 34, 56)
89
+ end
90
+
91
+ it 'should return same value for Time which already has dummy date values' do
92
+ dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.utc(2000, 1, 1, 12, 34, 56)
93
+ end
94
+
95
+ it 'should return time component values shifted to current zone if timezone aware' do
96
+ @timezone_aware = true
97
+ dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.zone.local(2000, 1, 1, 23, 34, 56)
98
+ end
99
+
100
+ it 'should return base dummy time value for Date value' do
101
+ dummy_time(Date.new(2010, 11, 22)).should == Time.utc(2000, 1, 1, 0, 0, 0)
102
+ end
103
+
104
+ describe "with custom dummy date" do
105
+ before(:all) do
106
+ @@original_dummy_date = ValidatesTimeliness.dummy_date_for_time_type
107
+ ValidatesTimeliness.dummy_date_for_time_type = [2010, 1, 1]
108
+ end
109
+
110
+ it 'should return dummy time with custom dummy date' do
111
+ dummy_time(Time.utc(1999, 11, 22, 12, 34, 56)).should == Time.utc(2010, 1, 1, 12, 34, 56)
112
+ end
113
+
114
+ after(:all) do
115
+ ValidatesTimeliness.dummy_date_for_time_type = @@original_dummy_date
116
+ end
117
+ end
118
+ end
119
+
120
+ describe "#evaluate_option_value" do
121
+ let(:person) { Person.new }
122
+
123
+ it 'should return Date object as is' do
124
+ value = Date.new(2010,1,1)
125
+ evaluate_option_value(value, person).should == value
126
+ end
127
+
128
+ it 'should return Time object as is' do
129
+ value = Time.mktime(2010,1,1)
130
+ evaluate_option_value(value, person).should == value
131
+ end
132
+
133
+ it 'should return DateTime object as is' do
134
+ value = DateTime.new(2010,1,1,0,0,0)
135
+ evaluate_option_value(value, person).should == value
136
+ end
137
+
138
+ it 'should return Time value returned from proc with 0 arity' do
139
+ value = Time.mktime(2010,1,1)
140
+ evaluate_option_value(lambda { value }, person).should == value
141
+ end
142
+
143
+ it 'should return Time value returned by record attribute call in proc arity of 1' do
144
+ value = Time.mktime(2010,1,1)
145
+ person.birth_time = value
146
+ evaluate_option_value(lambda {|r| r.birth_time }, person).should == value
147
+ end
148
+
149
+ it 'should return Time value for attribute method symbol which returns Time' do
150
+ value = Time.mktime(2010,1,1)
151
+ person.birth_time = value
152
+ evaluate_option_value(:birth_time, person).should == value
153
+ end
154
+
155
+ it 'should return Time value is default zone from string time value' do
156
+ value = '2010-01-01 12:00:00'
157
+ evaluate_option_value(value, person).should == Time.utc(2010,1,1,12,0,0)
158
+ end
159
+
160
+ it 'should return Time value is current zone from string time value if timezone aware' do
161
+ @timezone_aware = true
162
+ value = '2010-01-01 12:00:00'
163
+ evaluate_option_value(value, person).should == Time.zone.local(2010,1,1,12,0,0)
164
+ end
165
+
166
+ it 'should return Time value in default zone from proc which returns string time' do
167
+ value = '2010-01-01 12:00:00'
168
+ evaluate_option_value(lambda { value }, person).should == Time.utc(2010,1,1,12,0,0)
169
+ end
170
+
171
+ it 'should return Time value in default zone for attribute method symbol which returns string time value' do
172
+ value = '2010-01-01 12:00:00'
173
+ person.birth_time = value
174
+ evaluate_option_value(:birth_time, person).should == Time.utc(2010,1,1,12,0,0)
175
+ end
176
+
177
+ it 'should return Time value in current zone attribute method symbol which returns string time value if timezone aware' do
178
+ @timezone_aware = true
179
+ value = '2010-01-01 12:00:00'
180
+ person.birth_time = value
181
+ evaluate_option_value(:birth_time, person).should == Time.zone.local(2010,1,1,12,0,0)
182
+ end
183
+
184
+ context "restriction shorthand" do
185
+ before do
186
+ Timecop.freeze(Time.mktime(2010, 1, 1, 0, 0, 0))
187
+ end
188
+
189
+ it 'should evaluate :now as current time' do
190
+ evaluate_option_value(:now, person).should == Time.now
191
+ end
192
+
193
+ it 'should evaluate :today as current time' do
194
+ evaluate_option_value(:today, person).should == Date.today
195
+ end
196
+
197
+ it 'should not use shorthand if symbol if is record method' do
198
+ time = 1.day.from_now
199
+ person.stub!(:now).and_return(time)
200
+ evaluate_option_value(:now, person).should == time
201
+ end
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,178 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Extensions::DateTimeSelect do
4
+ include ActionView::Helpers::DateHelper
5
+
6
+ attr_reader :employee, :params
7
+
8
+ before do
9
+ @employee = Employee.new
10
+ @params = {}
11
+ end
12
+
13
+ describe "datetime_select" do
14
+ it "should use param values when attribute is nil" do
15
+ params["employee"] = {
16
+ "birth_datetime(1i)" => 2009,
17
+ "birth_datetime(2i)" => 2,
18
+ "birth_datetime(3i)" => 29,
19
+ "birth_datetime(4i)" => 12,
20
+ "birth_datetime(5i)" => 13,
21
+ "birth_datetime(6i)" => 14,
22
+ }
23
+ employee.birth_datetime = nil
24
+ output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
25
+ output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009')
26
+ output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'February')
27
+ output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '29')
28
+ output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12')
29
+ output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13')
30
+ output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14')
31
+ end
32
+
33
+ it "should override object values and use params if present" do
34
+ params["employee"] = {
35
+ "birth_datetime(1i)" => 2009,
36
+ "birth_datetime(2i)" => 2,
37
+ "birth_datetime(3i)" => 29,
38
+ "birth_datetime(4i)" => 12,
39
+ "birth_datetime(5i)" => 13,
40
+ "birth_datetime(6i)" => 14,
41
+ }
42
+ employee.birth_datetime = "2010-01-01 15:16:17"
43
+ output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
44
+ output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009')
45
+ output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'February')
46
+ output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '29')
47
+ output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12')
48
+ output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13')
49
+ output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14')
50
+ end
51
+
52
+ it "should use attribute values from object if no params" do
53
+ employee.birth_datetime = "2009-01-02 12:13:14"
54
+ output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
55
+ output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009')
56
+ output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'January')
57
+ output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '2')
58
+ output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12')
59
+ output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13')
60
+ output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14')
61
+ end
62
+
63
+ it "should use attribute values if params does not contain attribute params" do
64
+ employee.birth_datetime = "2009-01-02 12:13:14"
65
+ params["employee"] = { }
66
+ output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
67
+ output.should have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]', '2009')
68
+ output.should have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]', 'January')
69
+ output.should have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]', '2')
70
+ output.should have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]', '12')
71
+ output.should have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]', '13')
72
+ output.should have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]', '14')
73
+ end
74
+
75
+ it "should not select values when attribute value is nil and has no param values" do
76
+ employee.birth_datetime = nil
77
+ output = datetime_select(:employee, :birth_datetime, :include_blank => true, :include_seconds => true)
78
+ output.should_not have_tag('select[id=employee_birth_datetime_1i] option[selected=selected]')
79
+ output.should_not have_tag('select[id=employee_birth_datetime_2i] option[selected=selected]')
80
+ output.should_not have_tag('select[id=employee_birth_datetime_3i] option[selected=selected]')
81
+ output.should_not have_tag('select[id=employee_birth_datetime_4i] option[selected=selected]')
82
+ output.should_not have_tag('select[id=employee_birth_datetime_5i] option[selected=selected]')
83
+ output.should_not have_tag('select[id=employee_birth_datetime_6i] option[selected=selected]')
84
+ end
85
+ end
86
+
87
+ describe "date_select" do
88
+ it "should use param values when attribute is nil" do
89
+ params["employee"] = {
90
+ "birth_date(1i)" => 2009,
91
+ "birth_date(2i)" => 2,
92
+ "birth_date(3i)" => 29,
93
+ }
94
+ employee.birth_date = nil
95
+ output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
96
+ output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009')
97
+ output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'February')
98
+ output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '29')
99
+ end
100
+
101
+ it "should override object values and use params if present" do
102
+ params["employee"] = {
103
+ "birth_date(1i)" => 2009,
104
+ "birth_date(2i)" => 2,
105
+ "birth_date(3i)" => 29,
106
+ }
107
+ employee.birth_date = "2009-03-01"
108
+ output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
109
+ output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009')
110
+ output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'February')
111
+ output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '29')
112
+ end
113
+
114
+ it "should select attribute values from object if no params" do
115
+ employee.birth_date = "2009-01-02"
116
+ output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
117
+ output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009')
118
+ output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'January')
119
+ output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '2')
120
+ end
121
+
122
+ it "should select attribute values if params does not contain attribute params" do
123
+ employee.birth_date = "2009-01-02"
124
+ params["employee"] = { }
125
+ output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
126
+ output.should have_tag('select[id=employee_birth_date_1i] option[selected=selected]', '2009')
127
+ output.should have_tag('select[id=employee_birth_date_2i] option[selected=selected]', 'January')
128
+ output.should have_tag('select[id=employee_birth_date_3i] option[selected=selected]', '2')
129
+ end
130
+
131
+ it "should not select values when attribute value is nil and has no param values" do
132
+ employee.birth_date = nil
133
+ output = date_select(:employee, :birth_date, :include_blank => true, :include_seconds => true)
134
+ output.should_not have_tag('select[id=employee_birth_date_1i] option[selected=selected]')
135
+ output.should_not have_tag('select[id=employee_birth_date_2i] option[selected=selected]')
136
+ output.should_not have_tag('select[id=employee_birth_date_3i] option[selected=selected]')
137
+ end
138
+ end
139
+
140
+ describe "time_select" do
141
+ before do
142
+ Timecop.freeze Time.mktime(2009,1,1)
143
+ end
144
+
145
+ it "should use param values when attribute is nil" do
146
+ params["employee"] = {
147
+ "birth_time(1i)" => 2000,
148
+ "birth_time(2i)" => 1,
149
+ "birth_time(3i)" => 1,
150
+ "birth_time(4i)" => 12,
151
+ "birth_time(5i)" => 13,
152
+ "birth_time(6i)" => 14,
153
+ }
154
+ employee.birth_time = nil
155
+ output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true)
156
+ output.should have_tag('select[id=employee_birth_time_4i] option[selected=selected]', '12')
157
+ output.should have_tag('select[id=employee_birth_time_5i] option[selected=selected]', '13')
158
+ output.should have_tag('select[id=employee_birth_time_6i] option[selected=selected]', '14')
159
+ end
160
+
161
+ it "should select attribute values from object if no params" do
162
+ employee.birth_time = "2000-01-01 12:13:14"
163
+ output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true)
164
+ output.should have_tag('select[id=employee_birth_time_4i] option[selected=selected]', '12')
165
+ output.should have_tag('select[id=employee_birth_time_5i] option[selected=selected]', '13')
166
+ output.should have_tag('select[id=employee_birth_time_6i] option[selected=selected]', '14')
167
+ end
168
+
169
+ it "should not select values when attribute value is nil and has no param values" do
170
+ employee.birth_time = nil
171
+ output = time_select(:employee, :birth_time, :include_blank => true, :include_seconds => true)
172
+ output.should_not have_tag('select[id=employee_birth_time_4i] option[selected=selected]')
173
+ output.should_not have_tag('select[id=employee_birth_time_5i] option[selected=selected]')
174
+ output.should_not have_tag('select[id=employee_birth_time_6i] option[selected=selected]')
175
+ end
176
+ end
177
+
178
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Extensions::MultiparameterHandler do
4
+ let(:employee) { Employee.new }
5
+
6
+ it 'should return string value for invalid dates' do
7
+ instantiate_time_object('birth_date', [2000, 2, 31]).should == '2000-02-31'
8
+ end
9
+
10
+ it 'should return string value for invalid datetimes' do
11
+ instantiate_time_object('birth_datetime', [2000, 2, 31, 12, 0, 0]).should == '2000-02-31 12:00:00'
12
+ end
13
+
14
+ it 'should return Time value for valid datetimes' do
15
+ instantiate_time_object('birth_datetime', [2000, 2, 28, 12, 0, 0]).should be_kind_of(Time)
16
+ end
17
+
18
+ def instantiate_time_object(name, values)
19
+ employee.send(:instantiate_time_object, name, values)
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::HelperMethods do
4
+ it 'should define class validation methods on extended classes' do
5
+ ActiveRecord::Base.should respond_to(:validates_date)
6
+ ActiveRecord::Base.should respond_to(:validates_time)
7
+ ActiveRecord::Base.should respond_to(:validates_datetime)
8
+ end
9
+
10
+ it 'should define instance validation methods on extended classes' do
11
+ ActiveRecord::Base.instance_methods.should include('validates_date')
12
+ ActiveRecord::Base.instance_methods.should include('validates_time')
13
+ ActiveRecord::Base.instance_methods.should include('validates_datetime')
14
+ end
15
+
16
+ it 'should validate instance when validation method called' do
17
+ r = Employee.new
18
+ r.validates_date :birth_date
19
+ r.errors[:birth_date].should_not be_empty
20
+ end
21
+
22
+ describe ".timeliness_validated_attributes" do
23
+ it 'should return attributes validated with plugin validator' do
24
+ Person.timeliness_validated_attributes = {}
25
+ Person.validates_date :birth_date
26
+ Person.validates_time :birth_time
27
+ Person.validates_datetime :birth_datetime
28
+
29
+ Person.timeliness_validated_attributes.should == {
30
+ "birth_date" => :date,
31
+ "birth_time" => :time,
32
+ "birth_datetime" => :datetime
33
+ }
34
+ end
35
+ end
36
+ end