jc-validates_timeliness 3.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 (50) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +14 -0
  3. data/CHANGELOG.rdoc +183 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +301 -0
  6. data/Rakefile +30 -0
  7. data/gemfiles/mongoid_2_1.gemfile +16 -0
  8. data/gemfiles/mongoid_2_2.gemfile +16 -0
  9. data/gemfiles/mongoid_2_3.gemfile +16 -0
  10. data/gemfiles/mongoid_2_4.gemfile +16 -0
  11. data/gemfiles/rails_3_0.gemfile +15 -0
  12. data/gemfiles/rails_3_1.gemfile +15 -0
  13. data/gemfiles/rails_3_2.gemfile +15 -0
  14. data/init.rb +1 -0
  15. data/lib/generators/validates_timeliness/install_generator.rb +16 -0
  16. data/lib/generators/validates_timeliness/templates/en.yml +16 -0
  17. data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +40 -0
  18. data/lib/validates_timeliness.rb +70 -0
  19. data/lib/validates_timeliness/attribute_methods.rb +92 -0
  20. data/lib/validates_timeliness/conversion.rb +70 -0
  21. data/lib/validates_timeliness/extensions.rb +14 -0
  22. data/lib/validates_timeliness/extensions/date_time_select.rb +61 -0
  23. data/lib/validates_timeliness/extensions/multiparameter_handler.rb +80 -0
  24. data/lib/validates_timeliness/helper_methods.rb +23 -0
  25. data/lib/validates_timeliness/orm/active_record.rb +53 -0
  26. data/lib/validates_timeliness/orm/mongoid.rb +63 -0
  27. data/lib/validates_timeliness/railtie.rb +15 -0
  28. data/lib/validates_timeliness/validator.rb +117 -0
  29. data/lib/validates_timeliness/version.rb +3 -0
  30. data/spec/spec_helper.rb +100 -0
  31. data/spec/support/config_helper.rb +36 -0
  32. data/spec/support/model_helpers.rb +27 -0
  33. data/spec/support/tag_matcher.rb +35 -0
  34. data/spec/support/test_model.rb +60 -0
  35. data/spec/validates_timeliness/attribute_methods_spec.rb +86 -0
  36. data/spec/validates_timeliness/conversion_spec.rb +234 -0
  37. data/spec/validates_timeliness/extensions/date_time_select_spec.rb +163 -0
  38. data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +44 -0
  39. data/spec/validates_timeliness/helper_methods_spec.rb +30 -0
  40. data/spec/validates_timeliness/orm/active_record_spec.rb +244 -0
  41. data/spec/validates_timeliness/orm/mongoid_spec.rb +189 -0
  42. data/spec/validates_timeliness/validator/after_spec.rb +57 -0
  43. data/spec/validates_timeliness/validator/before_spec.rb +57 -0
  44. data/spec/validates_timeliness/validator/is_at_spec.rb +61 -0
  45. data/spec/validates_timeliness/validator/on_or_after_spec.rb +57 -0
  46. data/spec/validates_timeliness/validator/on_or_before_spec.rb +57 -0
  47. data/spec/validates_timeliness/validator_spec.rb +246 -0
  48. data/spec/validates_timeliness_spec.rb +43 -0
  49. data/validates_timeliness.gemspec +20 -0
  50. metadata +128 -0
@@ -0,0 +1,189 @@
1
+ require 'spec_helper'
2
+
3
+ # Try loading mongoid and connecting. Otherwise, abort and skip spec.
4
+ begin
5
+
6
+ require 'mongoid'
7
+ require 'validates_timeliness/orm/mongoid'
8
+
9
+ Mongoid.configure do |config|
10
+ name = "validates_timeliness_test"
11
+ host = "localhost"
12
+ config.master = Mongo::Connection.new.db(name)
13
+ config.persist_in_safe_mode = false
14
+ end
15
+
16
+ describe ValidatesTimeliness, 'Mongoid' do
17
+
18
+ class Article
19
+ include Mongoid::Document
20
+ field :publish_date, :type => Date
21
+ field :publish_time, :type => Time
22
+ field :publish_datetime, :type => DateTime
23
+ validates_date :publish_date, :allow_nil => true
24
+ validates_time :publish_time, :allow_nil => true
25
+ validates_datetime :publish_datetime, :allow_nil => true
26
+ end
27
+
28
+ context "validation methods" do
29
+ let(:record) { Article.new }
30
+
31
+ it 'should be defined on the class' do
32
+ expect(Article).to respond_to(:validates_date)
33
+ expect(Article).to respond_to(:validates_time)
34
+ expect(Article).to respond_to(:validates_datetime)
35
+ end
36
+
37
+ it 'should be defined on the instance' do
38
+ expect(record).to respond_to(:validates_date)
39
+ expect(record).to respond_to(:validates_time)
40
+ expect(record).to respond_to(:validates_datetime)
41
+ end
42
+
43
+ it "should validate a valid value string" do
44
+ record.publish_date = '2012-01-01'
45
+
46
+ record.valid?
47
+ expect(record.errors[:publish_date]).to be_empty
48
+ end
49
+
50
+ it "should validate a invalid value string" do
51
+ begin
52
+ record.publish_date = 'not a date'
53
+ rescue
54
+ end
55
+
56
+ record.valid?
57
+ expect(record.errors[:publish_date]).not_to be_empty
58
+ end
59
+
60
+ it "should validate a nil value" do
61
+ record.publish_date = nil
62
+
63
+ record.valid?
64
+ expect(record.errors[:publish_date]).to be_empty
65
+ end
66
+ end
67
+
68
+ it 'should determine type for attribute' do
69
+ expect(Article.timeliness_attribute_type(:publish_date)).to eq(:date)
70
+ end
71
+
72
+ context "attribute write method" do
73
+ let(:record) { Article.new }
74
+
75
+ it 'should cache attribute raw value' do
76
+ record.publish_datetime = date_string = '2010-01-01'
77
+
78
+ expect(record._timeliness_raw_value_for('publish_datetime')).to eq(date_string)
79
+ end
80
+
81
+ context "with plugin parser" do
82
+ let(:record) { ArticleWithParser.new }
83
+
84
+ class ArticleWithParser
85
+ include Mongoid::Document
86
+ field :publish_date, :type => Date
87
+ field :publish_time, :type => Time
88
+ field :publish_datetime, :type => DateTime
89
+
90
+ ValidatesTimeliness.use_plugin_parser = true
91
+ validates_date :publish_date, :allow_nil => true
92
+ validates_time :publish_time, :allow_nil => true
93
+ validates_datetime :publish_datetime, :allow_nil => true
94
+ ValidatesTimeliness.use_plugin_parser = false
95
+ end
96
+
97
+ context "for a date column" do
98
+ it 'should parse a string value' do
99
+ expect(Timeliness::Parser).to receive(:parse)
100
+
101
+ record.publish_date = '2010-01-01'
102
+ end
103
+
104
+ it 'should parse a invalid string value as nil' do
105
+ expect(Timeliness::Parser).to receive(:parse)
106
+
107
+ record.publish_date = 'not valid'
108
+ end
109
+
110
+ it 'should store a Date value after parsing string' do
111
+ record.publish_date = '2010-01-01'
112
+
113
+ expect(record.publish_date).to be_kind_of(Date)
114
+ expect(record.publish_date).to eq Date.new(2010, 1, 1)
115
+ end
116
+ end
117
+
118
+ context "for a time column" do
119
+ it 'should parse a string value' do
120
+ expect(Timeliness::Parser).to receive(:parse)
121
+
122
+ record.publish_time = '12:30'
123
+ end
124
+
125
+ it 'should parse a invalid string value as nil' do
126
+ expect(Timeliness::Parser).to receive(:parse)
127
+
128
+ record.publish_time = 'not valid'
129
+ end
130
+
131
+ it 'should store a Time value after parsing string' do
132
+ record.publish_time = '12:30'
133
+
134
+ expect(record.publish_time).to be_kind_of(Time)
135
+ expect(record.publish_time).to eq Time.utc(2000, 1, 1, 12, 30)
136
+ end
137
+ end
138
+
139
+ context "for a datetime column" do
140
+ with_config(:default_timezone, 'Australia/Melbourne')
141
+
142
+ it 'should parse a string value' do
143
+ expect(Timeliness::Parser).to receive(:parse)
144
+
145
+ record.publish_datetime = '2010-01-01 12:00'
146
+ end
147
+
148
+ it 'should parse a invalid string value as nil' do
149
+ expect(Timeliness::Parser).to receive(:parse)
150
+
151
+ record.publish_datetime = 'not valid'
152
+ end
153
+
154
+ it 'should parse string into DateTime value' do
155
+ record.publish_datetime = '2010-01-01 12:00'
156
+
157
+ expect(record.publish_datetime).to be_kind_of(DateTime)
158
+ end
159
+
160
+ pending 'should parse string as current timezone' do
161
+ record.publish_datetime = '2010-06-01 12:00'
162
+
163
+ expect(record.publish_datetime.utc_offset).to eq Time.zone.utc_offset
164
+ end
165
+ end
166
+ end
167
+ end
168
+
169
+ context "cached value" do
170
+ it 'should be cleared on reload' do
171
+ record = Article.create!
172
+ record.publish_date = '2010-01-01'
173
+ record.reload
174
+ expect(record._timeliness_raw_value_for('publish_date')).to be_nil
175
+ end
176
+ end
177
+
178
+ context "before_type_cast method" do
179
+ it 'should not be defined if ORM does not support it' do
180
+ expect(Article.new).not_to respond_to(:publish_datetime_before_type_cast)
181
+ end
182
+ end
183
+ end
184
+
185
+ rescue LoadError
186
+ puts "Mongoid specs skipped. Mongoid not installed"
187
+ rescue StandardError => e
188
+ puts "Mongoid specs skipped. MongoDB connection failed with error: #{e.message}"
189
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator, ":after option" do
4
+ describe "for date type" do
5
+ before do
6
+ Person.validates_date :birth_date, :after => Date.new(2010, 1, 1)
7
+ end
8
+
9
+ it "should not be valid for same date value" do
10
+ invalid!(:birth_date, Date.new(2010, 1, 1), 'must be after 2010-01-01')
11
+ end
12
+
13
+ it "should not be valid for date before restriction" do
14
+ invalid!(:birth_date, Date.new(2009, 12, 31), 'must be after 2010-01-01')
15
+ end
16
+
17
+ it "should be valid for date after restriction" do
18
+ valid!(:birth_date, Date.new(2010, 1, 2))
19
+ end
20
+ end
21
+
22
+ describe "for time type" do
23
+ before do
24
+ Person.validates_time :birth_time, :after => Time.mktime(2000, 1, 1, 12, 0, 0)
25
+ end
26
+
27
+ it "should not be valid for same time as restriction" do
28
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be after 12:00:00')
29
+ end
30
+
31
+ it "should not be valid for time before restriction" do
32
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be after 12:00:00')
33
+ end
34
+
35
+ it "should be valid for time after restriction" do
36
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01))
37
+ end
38
+ end
39
+
40
+ describe "for datetime type" do
41
+ before do
42
+ Person.validates_datetime :birth_datetime, :after => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
43
+ end
44
+
45
+ it "should not be valid for same datetime as restriction" do
46
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0), 'must be after 2010-01-01 12:00:00')
47
+ end
48
+
49
+ it "should be valid for datetime is before restriction" do
50
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be after 2010-01-01 12:00:00')
51
+ end
52
+
53
+ it "should be valid for datetime is after restriction" do
54
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1))
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator, ":before option" do
4
+ describe "for date type" do
5
+ before do
6
+ Person.validates_date :birth_date, :before => Date.new(2010, 1, 1)
7
+ end
8
+
9
+ it "should not be valid for date after restriction" do
10
+ invalid!(:birth_date, Date.new(2010, 1, 2), 'must be before 2010-01-01')
11
+ end
12
+
13
+ it "should not be valid for same date value" do
14
+ invalid!(:birth_date, Date.new(2010, 1, 1), 'must be before 2010-01-01')
15
+ end
16
+
17
+ it "should be valid for date before restriction" do
18
+ valid!(:birth_date, Date.new(2009, 12, 31))
19
+ end
20
+ end
21
+
22
+ describe "for time type" do
23
+ before do
24
+ Person.validates_time :birth_time, :before => Time.mktime(2000, 1, 1, 12, 0, 0)
25
+ end
26
+
27
+ it "should not be valid for time after restriction" do
28
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be before 12:00:00')
29
+ end
30
+
31
+ it "should not be valid for same time as restriction" do
32
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be before 12:00:00')
33
+ end
34
+
35
+ it "should be valid for time before restriction" do
36
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59))
37
+ end
38
+ end
39
+
40
+ describe "for datetime type" do
41
+ before do
42
+ Person.validates_datetime :birth_datetime, :before => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
43
+ end
44
+
45
+ it "should not be valid for datetime after restriction" do
46
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be before 2010-01-01 12:00:00')
47
+ end
48
+
49
+ it "should not be valid for same datetime as restriction" do
50
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0), 'must be before 2010-01-01 12:00:00')
51
+ end
52
+
53
+ it "should be valid for datetime before restriction" do
54
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59))
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator, ":is_at option" do
4
+ before do
5
+ Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
6
+ end
7
+
8
+ describe "for date type" do
9
+ before do
10
+ Person.validates_date :birth_date, :is_at => Date.new(2010, 1, 1)
11
+ end
12
+
13
+ it "should not be valid for date before restriction" do
14
+ invalid!(:birth_date, Date.new(2009, 12, 31), 'must be at 2010-01-01')
15
+ end
16
+
17
+ it "should not be valid for date after restriction" do
18
+ invalid!(:birth_date, Date.new(2010, 1, 2), 'must be at 2010-01-01')
19
+ end
20
+
21
+ it "should be valid for same date value" do
22
+ valid!(:birth_date, Date.new(2010, 1, 1))
23
+ end
24
+ end
25
+
26
+ describe "for time type" do
27
+ before do
28
+ Person.validates_time :birth_time, :is_at => Time.mktime(2000, 1, 1, 12, 0, 0)
29
+ end
30
+
31
+ it "should not be valid for time before restriction" do
32
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be at 12:00:00')
33
+ end
34
+
35
+ it "should not be valid for time after restriction" do
36
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be at 12:00:00')
37
+ end
38
+
39
+ it "should be valid for same time as restriction" do
40
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0))
41
+ end
42
+ end
43
+
44
+ describe "for datetime type" do
45
+ before do
46
+ Person.validates_datetime :birth_datetime, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
47
+ end
48
+
49
+ it "should not be valid for datetime before restriction" do
50
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be at 2010-01-01 12:00:00')
51
+ end
52
+
53
+ it "should not be valid for datetime after restriction" do
54
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be at 2010-01-01 12:00:00')
55
+ end
56
+
57
+ it "should be valid for same datetime as restriction" do
58
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0))
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator, ":on_or_after option" do
4
+ describe "for date type" do
5
+ before do
6
+ Person.validates_date :birth_date, :on_or_after => Date.new(2010, 1, 1)
7
+ end
8
+
9
+ it "should not be valid for date before restriction" do
10
+ invalid!(:birth_date, Date.new(2009, 12, 31), 'must be on or after 2010-01-01')
11
+ end
12
+
13
+ it "should be valid for same date value" do
14
+ valid!(:birth_date, Date.new(2010, 1, 1))
15
+ end
16
+
17
+ it "should be valid for date after restriction" do
18
+ valid!(:birth_date, Date.new(2010, 1, 2))
19
+ end
20
+ end
21
+
22
+ describe "for time type" do
23
+ before do
24
+ Person.validates_time :birth_time, :on_or_after => Time.mktime(2000, 1, 1, 12, 0, 0)
25
+ end
26
+
27
+ it "should not be valid for time before restriction" do
28
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be on or after 12:00:00')
29
+ end
30
+
31
+ it "should be valid for time after restriction" do
32
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01))
33
+ end
34
+
35
+ it "should be valid for same time as restriction" do
36
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0))
37
+ end
38
+ end
39
+
40
+ describe "for datetime type" do
41
+ before do
42
+ Person.validates_datetime :birth_datetime, :on_or_after => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
43
+ end
44
+
45
+ it "should not be valid for datetime before restriction" do
46
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be on or after 2010-01-01 12:00:00')
47
+ end
48
+
49
+ it "should be valid for same datetime as restriction" do
50
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0))
51
+ end
52
+
53
+ it "should be valid for datetime after restriction" do
54
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1))
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator, ":on_or_before option" do
4
+ describe "for date type" do
5
+ before do
6
+ Person.validates_date :birth_date, :on_or_before => Date.new(2010, 1, 1)
7
+ end
8
+
9
+ it "should not be valid for date after restriction" do
10
+ invalid!(:birth_date, Date.new(2010, 1, 2), 'must be on or before 2010-01-01')
11
+ end
12
+
13
+ it "should be valid for date before restriction" do
14
+ valid!(:birth_date, Date.new(2009, 12, 31))
15
+ end
16
+
17
+ it "should be valid for same date value" do
18
+ valid!(:birth_date, Date.new(2010, 1, 1))
19
+ end
20
+ end
21
+
22
+ describe "for time type" do
23
+ before do
24
+ Person.validates_time :birth_time, :on_or_before => Time.mktime(2000, 1, 1, 12, 0, 0)
25
+ end
26
+
27
+ it "should not be valid for time after restriction" do
28
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be on or before 12:00:00')
29
+ end
30
+
31
+ it "should be valid for time before restriction" do
32
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59))
33
+ end
34
+
35
+ it "should be valid for same time as restriction" do
36
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0))
37
+ end
38
+ end
39
+
40
+ describe "for datetime type" do
41
+ before do
42
+ Person.validates_datetime :birth_datetime, :on_or_before => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
43
+ end
44
+
45
+ it "should not be valid for datetime after restriction" do
46
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be on or before 2010-01-01 12:00:00')
47
+ end
48
+
49
+ it "should be valid for same datetime as restriction" do
50
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0))
51
+ end
52
+
53
+ it "should not be valid for datetime before restriction" do
54
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59))
55
+ end
56
+ end
57
+ end