jc-validates_timeliness 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,246 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator do
4
+ before do
5
+ Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
6
+ end
7
+
8
+ describe "Model.validates with :timeliness option" do
9
+ it 'should use plugin validator class' do
10
+ Person.validates :birth_date, :timeliness => {:is_at => Date.new(2010,1,1), :type => :date}
11
+ expect(Person.validators).to have(1).kind_of(ActiveModel::Validations::TimelinessValidator)
12
+ invalid!(:birth_date, Date.new(2010,1,2))
13
+ valid!(:birth_date, Date.new(2010,1,1))
14
+ end
15
+
16
+ it 'should use default to :datetime type' do
17
+ Person.validates :birth_datetime, :timeliness => {:is_at => Time.mktime(2010,1,1)}
18
+ expect(Person.validators.first.type).to eq(:datetime)
19
+ end
20
+
21
+ it 'should add attribute to timeliness attributes set' do
22
+ expect(PersonWithShim.timeliness_validated_attributes).not_to include(:birth_time)
23
+
24
+ PersonWithShim.validates :birth_time, :timeliness => {:is_at => "12:30"}
25
+
26
+ expect(PersonWithShim.timeliness_validated_attributes).to include(:birth_time)
27
+ end
28
+ end
29
+
30
+ it 'should not be valid for value which not valid date or time value' do
31
+ Person.validates_date :birth_date
32
+ invalid!(:birth_date, "Not a date", 'is not a valid date')
33
+ end
34
+
35
+ it 'should not be valid attribute is type cast to nil but raw value is non-nil invalid value' do
36
+ Person.validates_date :birth_date, :allow_nil => true
37
+ record = Person.new
38
+ allow(record).to receive(:birth_date).and_return(nil)
39
+ allow(record).to receive(:_timeliness_raw_value_for).and_return("Not a date")
40
+ expect(record).not_to be_valid
41
+ expect(record.errors[:birth_date].first).to eq('is not a valid date')
42
+ end
43
+
44
+ describe ":allow_nil option" do
45
+ it 'should not allow nil by default' do
46
+ Person.validates_date :birth_date
47
+ invalid!(:birth_date, [nil], 'is not a valid date')
48
+ valid!(:birth_date, Date.today)
49
+ end
50
+
51
+ it 'should allow nil when true' do
52
+ Person.validates_date :birth_date, :allow_nil => true
53
+ valid!(:birth_date, [nil])
54
+ end
55
+
56
+ context "with raw value cache" do
57
+ it "should not be valid with an invalid format" do
58
+ PersonWithShim.validates_date :birth_date, :allow_nil => true
59
+
60
+ p = PersonWithShim.new
61
+ p.birth_date = 'bogus'
62
+
63
+ expect(p).not_to be_valid
64
+ end
65
+ end
66
+ end
67
+
68
+ describe ":allow_blank option" do
69
+ it 'should not allow blank by default' do
70
+ Person.validates_date :birth_date
71
+ invalid!(:birth_date, '', 'is not a valid date')
72
+ valid!(:birth_date, Date.today)
73
+ end
74
+
75
+ it 'should allow blank when true' do
76
+ Person.validates_date :birth_date, :allow_blank => true
77
+ valid!(:birth_date, '')
78
+ end
79
+
80
+ context "with raw value cache" do
81
+ it "should not be valid with an invalid format" do
82
+ PersonWithShim.validates_date :birth_date, :allow_blank => true
83
+
84
+ p = PersonWithShim.new
85
+ p.birth_date = 'bogus'
86
+
87
+ expect(p).not_to be_valid
88
+ end
89
+ end
90
+ end
91
+
92
+ describe ":between option" do
93
+ describe "array value" do
94
+ it 'should be split option into :on_or_after and :on_or_before values' do
95
+ on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
96
+ Person.validates_date :birth_date, :between => [on_or_after, on_or_before]
97
+ expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
98
+ expect(Person.validators.first.options[:on_or_before]).to eq(on_or_before)
99
+ invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
100
+ invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
101
+ valid!(:birth_date, on_or_after)
102
+ valid!(:birth_date, on_or_before)
103
+ end
104
+ end
105
+
106
+ describe "range value" do
107
+ it 'should be split option into :on_or_after and :on_or_before values' do
108
+ on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
109
+ Person.validates_date :birth_date, :between => on_or_after..on_or_before
110
+ expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
111
+ expect(Person.validators.first.options[:on_or_before]).to eq(on_or_before)
112
+ invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
113
+ invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
114
+ valid!(:birth_date, on_or_after)
115
+ valid!(:birth_date, on_or_before)
116
+ end
117
+ end
118
+
119
+ describe "range with excluded end value" do
120
+ it 'should be split option into :on_or_after and :before values' do
121
+ on_or_after, before = Date.new(2010,1,1), Date.new(2010,1,3)
122
+ Person.validates_date :birth_date, :between => on_or_after...before
123
+ expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
124
+ expect(Person.validators.first.options[:before]).to eq(before)
125
+ invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
126
+ invalid!(:birth_date, before, "must be before 2010-01-03")
127
+ valid!(:birth_date, on_or_after)
128
+ valid!(:birth_date, before - 1)
129
+ end
130
+ end
131
+ end
132
+
133
+ describe ":ignore_usec option" do
134
+ it "should not be valid when usec values don't match and option is false" do
135
+ Person.validates_datetime :birth_datetime, :on_or_before => Time.utc(2010,1,2,3,4,5), :ignore_usec => false
136
+ invalid!(:birth_datetime, Time.utc(2010,1,2,3,4,5,10000))
137
+ end
138
+
139
+ it "should be valid when usec values dont't match and option is true" do
140
+ Person.validates_datetime :birth_datetime, :on_or_before => Time.utc(2010,1,2,3,4,5), :ignore_usec => true
141
+ valid!(:birth_datetime, Time.utc(2010,1,2,3,4,5,10000))
142
+ end
143
+ end
144
+
145
+ describe ":format option" do
146
+ class PersonWithFormatOption
147
+ include TestModel
148
+ include TestModelShim
149
+ attribute :birth_date, :date
150
+ attribute :birth_time, :time
151
+ attribute :birth_datetime, :datetime
152
+ validates_date :birth_date, :format => 'dd-mm-yyyy'
153
+ end
154
+
155
+ let(:person) { PersonWithFormatOption.new }
156
+
157
+ with_config(:use_plugin_parser, true)
158
+
159
+ it "should be valid when value matches format" do
160
+ person.birth_date = '11-12-1913'
161
+ person.valid?
162
+ expect(person.errors[:birth_date]).to be_empty
163
+ end
164
+
165
+ it "should not be valid when value does not match format" do
166
+ person.birth_date = '1913-12-11'
167
+ person.valid?
168
+ expect(person.errors[:birth_date]).to include('is not a valid date')
169
+ end
170
+ end
171
+
172
+ describe "restriction value errors" do
173
+ let(:person) { Person.new(:birth_date => Date.today) }
174
+
175
+ before do
176
+ Person.validates_time :birth_date, :is_at => lambda { raise }, :before => lambda { raise }
177
+ end
178
+
179
+ it "should be added when ignore_restriction_errors is false" do
180
+ with_config(:ignore_restriction_errors, false) do
181
+ person.valid?
182
+ expect(person.errors[:birth_date].first).to match("Error occurred validating birth_date")
183
+ end
184
+ end
185
+
186
+ it "should not be added when ignore_restriction_errors is true" do
187
+ with_config(:ignore_restriction_errors, true) do
188
+ person.valid?
189
+ expect(person.errors[:birth_date]).to be_empty
190
+ end
191
+ end
192
+
193
+ it 'should exit on first error' do
194
+ with_config(:ignore_restriction_errors, false) do
195
+ person.valid?
196
+ expect(person.errors[:birth_date]).to have(1).items
197
+ end
198
+ end
199
+ end
200
+
201
+ describe "#format_error_value" do
202
+ describe "default" do
203
+ it 'should format date error value as yyyy-mm-dd' do
204
+ validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
205
+ expect(validator.format_error_value(Date.new(2010,1,1))).to eq('2010-01-01')
206
+ end
207
+
208
+ it 'should format time error value as hh:nn:ss' do
209
+ validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_time], :type => :time)
210
+ expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('12:34:56')
211
+ end
212
+
213
+ it 'should format datetime error value as yyyy-mm-dd hh:nn:ss' do
214
+ validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_datetime], :type => :datetime)
215
+ expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('2010-01-01 12:34:56')
216
+ end
217
+ end
218
+
219
+ describe "with missing translation" do
220
+ before :all do
221
+ I18n.locale = :es
222
+ end
223
+
224
+ it 'should use the default format for the type' do
225
+ validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
226
+ expect(validator.format_error_value(Date.new(2010,1,1))).to eq('2010-01-01')
227
+ end
228
+
229
+ after :all do
230
+ I18n.locale = :en
231
+ end
232
+ end
233
+ end
234
+
235
+ context "custom error message" do
236
+ it 'should be used for invalid type' do
237
+ Person.validates_date :birth_date, :invalid_date_message => 'custom invalid message'
238
+ invalid!(:birth_date, 'asdf', 'custom invalid message')
239
+ end
240
+
241
+ it 'should be used for invalid restriction' do
242
+ Person.validates_date :birth_date, :before => Time.now, :before_message => 'custom before message'
243
+ invalid!(:birth_date, Time.now, 'custom before message')
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness do
4
+
5
+ it 'should alias use_euro_formats to remove_us_formats on Timeliness gem' do
6
+ expect(Timeliness).to respond_to(:remove_us_formats)
7
+ end
8
+
9
+ it 'should alias to date_for_time_type to dummy_date_for_time_type on Timeliness gem' do
10
+ expect(Timeliness).to respond_to(:dummy_date_for_time_type)
11
+ end
12
+
13
+ describe "config" do
14
+ it 'should delegate default_timezone to Timeliness gem' do
15
+ expect(Timeliness).to receive(:default_timezone=)
16
+ ValidatesTimeliness.default_timezone = :utc
17
+ end
18
+
19
+ it 'should delegate dummy_date_for_time_type to Timeliness gem' do
20
+ expect(Timeliness).to receive(:dummy_date_for_time_type)
21
+ expect(Timeliness).to receive(:dummy_date_for_time_type=)
22
+ array = ValidatesTimeliness.dummy_date_for_time_type
23
+ ValidatesTimeliness.dummy_date_for_time_type = array
24
+ end
25
+
26
+ context "parser" do
27
+ it 'should delegate add_formats to Timeliness gem' do
28
+ expect(Timeliness).to receive(:add_formats)
29
+ ValidatesTimeliness.parser.add_formats
30
+ end
31
+
32
+ it 'should delegate remove_formats to Timeliness gem' do
33
+ expect(Timeliness).to receive(:remove_formats)
34
+ ValidatesTimeliness.parser.remove_formats
35
+ end
36
+
37
+ it 'should delegate remove_us_formats to Timeliness gem' do
38
+ expect(Timeliness).to receive(:remove_us_formats)
39
+ ValidatesTimeliness.parser.remove_us_formats
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validates_timeliness/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jc-validates_timeliness"
7
+ s.version = ValidatesTimeliness::VERSION
8
+ s.authors = ["Adam Meehan", "John Carney"]
9
+ s.summary = %q{Date and time validation plugin for Rails which allows custom formats}
10
+ s.description = %q{Adds validation methods to ActiveModel for validating dates and times. Works with multiple ORMS.}
11
+ s.email = %q{adam.meehan@gmail.com}
12
+ s.homepage = %q{http://github.com/johncarney/validates_timeliness}
13
+
14
+ s.require_paths = ["lib"]
15
+ s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb Appraisals Travis.yml } - Dir['gemsfiles/*']
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
18
+
19
+ s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.7"])
20
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jc-validates_timeliness
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Meehan
8
+ - John Carney
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-07-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: timeliness
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 0.3.7
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 0.3.7
28
+ description: Adds validation methods to ActiveModel for validating dates and times.
29
+ Works with multiple ORMS.
30
+ email: adam.meehan@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files:
34
+ - README.rdoc
35
+ - CHANGELOG.rdoc
36
+ - LICENSE
37
+ files:
38
+ - ".travis.yml"
39
+ - CHANGELOG.rdoc
40
+ - LICENSE
41
+ - README.rdoc
42
+ - Rakefile
43
+ - gemfiles/mongoid_2_1.gemfile
44
+ - gemfiles/mongoid_2_2.gemfile
45
+ - gemfiles/mongoid_2_3.gemfile
46
+ - gemfiles/mongoid_2_4.gemfile
47
+ - gemfiles/rails_3_0.gemfile
48
+ - gemfiles/rails_3_1.gemfile
49
+ - gemfiles/rails_3_2.gemfile
50
+ - init.rb
51
+ - lib/generators/validates_timeliness/install_generator.rb
52
+ - lib/generators/validates_timeliness/templates/en.yml
53
+ - lib/generators/validates_timeliness/templates/validates_timeliness.rb
54
+ - lib/validates_timeliness.rb
55
+ - lib/validates_timeliness/attribute_methods.rb
56
+ - lib/validates_timeliness/conversion.rb
57
+ - lib/validates_timeliness/extensions.rb
58
+ - lib/validates_timeliness/extensions/date_time_select.rb
59
+ - lib/validates_timeliness/extensions/multiparameter_handler.rb
60
+ - lib/validates_timeliness/helper_methods.rb
61
+ - lib/validates_timeliness/orm/active_record.rb
62
+ - lib/validates_timeliness/orm/mongoid.rb
63
+ - lib/validates_timeliness/railtie.rb
64
+ - lib/validates_timeliness/validator.rb
65
+ - lib/validates_timeliness/version.rb
66
+ - spec/spec_helper.rb
67
+ - spec/support/config_helper.rb
68
+ - spec/support/model_helpers.rb
69
+ - spec/support/tag_matcher.rb
70
+ - spec/support/test_model.rb
71
+ - spec/validates_timeliness/attribute_methods_spec.rb
72
+ - spec/validates_timeliness/conversion_spec.rb
73
+ - spec/validates_timeliness/extensions/date_time_select_spec.rb
74
+ - spec/validates_timeliness/extensions/multiparameter_handler_spec.rb
75
+ - spec/validates_timeliness/helper_methods_spec.rb
76
+ - spec/validates_timeliness/orm/active_record_spec.rb
77
+ - spec/validates_timeliness/orm/mongoid_spec.rb
78
+ - spec/validates_timeliness/validator/after_spec.rb
79
+ - spec/validates_timeliness/validator/before_spec.rb
80
+ - spec/validates_timeliness/validator/is_at_spec.rb
81
+ - spec/validates_timeliness/validator/on_or_after_spec.rb
82
+ - spec/validates_timeliness/validator/on_or_before_spec.rb
83
+ - spec/validates_timeliness/validator_spec.rb
84
+ - spec/validates_timeliness_spec.rb
85
+ - validates_timeliness.gemspec
86
+ homepage: http://github.com/johncarney/validates_timeliness
87
+ licenses: []
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Date and time validation plugin for Rails which allows custom formats
109
+ test_files:
110
+ - spec/spec_helper.rb
111
+ - spec/support/config_helper.rb
112
+ - spec/support/model_helpers.rb
113
+ - spec/support/tag_matcher.rb
114
+ - spec/support/test_model.rb
115
+ - spec/validates_timeliness/attribute_methods_spec.rb
116
+ - spec/validates_timeliness/conversion_spec.rb
117
+ - spec/validates_timeliness/extensions/date_time_select_spec.rb
118
+ - spec/validates_timeliness/extensions/multiparameter_handler_spec.rb
119
+ - spec/validates_timeliness/helper_methods_spec.rb
120
+ - spec/validates_timeliness/orm/active_record_spec.rb
121
+ - spec/validates_timeliness/orm/mongoid_spec.rb
122
+ - spec/validates_timeliness/validator/after_spec.rb
123
+ - spec/validates_timeliness/validator/before_spec.rb
124
+ - spec/validates_timeliness/validator/is_at_spec.rb
125
+ - spec/validates_timeliness/validator/on_or_after_spec.rb
126
+ - spec/validates_timeliness/validator/on_or_before_spec.rb
127
+ - spec/validates_timeliness/validator_spec.rb
128
+ - spec/validates_timeliness_spec.rb