jc-validates_timeliness 3.1.0 → 3.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7bd9fd235f62881b5d9268a0557c312dea1e71da
4
- data.tar.gz: 4acf1e727c71b48f63f8fe756f93a0fb0ee65763
3
+ metadata.gz: 9736741a15eddc444e4bfc7a478554867e3f2227
4
+ data.tar.gz: 371107752fe5b1cf010877128b4e6d2260655e3a
5
5
  SHA512:
6
- metadata.gz: 2999e43fbe2481299924f3c567aa4778f39688fc49d846138a60fa55535f33b667d6df2f02a24df739b9f9b9135a59bb6219547341bce6892babc411a5294f86
7
- data.tar.gz: 8437ea8c718ec239d07a897b5355c27663b70c5625d70ac084d3917642ef169b8dd198cda1059ca887353c6a86d797332e3f29bcae657d2785aa3ab283777178
6
+ metadata.gz: a5c4923d5059e777cd18b5a9ed1a65638e1c0582aa179ceb4c9a5c3702ca325293256b273b3f7e3e1c0dbc1f2f5f7b401f8f9a034f747ad7bb1dd609915c8a40
7
+ data.tar.gz: 397b906340162ee0e671ef85af4d9a7eb7708a02a1dc6cd1666cec8227462564fa283c9047d36dba36b9e5e1a99afb8b30bdaa87293d915a1b01bc5826d79a06
@@ -0,0 +1,311 @@
1
+ # ValidatesTimeliness
2
+ [![Gem Version](https://badge.fury.io/rb/jc-validates_timeliness.svg)](http://badge.fury.io/rb/jc-validates_timeliness)
3
+ [![Build status](https://travis-ci.org/johncarney/validates_timeliness.svg?branch=master)](https://travis-ci.org/johncarney/validates_timeliness)
4
+ [![Coverage Status](https://coveralls.io/repos/johncarney/validates_timeliness/badge.png?branch=master)](https://coveralls.io/r/johncarney/validates_timeliness?branch=master)
5
+
6
+ ## Description
7
+
8
+ Complete validation of dates, times and datetimes for Rails 3.x and ActiveModel.
9
+
10
+ If you a looking for the old version for Rails 2.x go
11
+ here: [http://github.com/adzap/validates_timeliness/tree/v2.3](http://github.com/adzap/validates_timeliness/tree/v2.3).
12
+
13
+
14
+ ## Features
15
+
16
+ * Adds validation for dates, times and datetimes to ActiveModel
17
+ * Handles timezones and type casting of values for you
18
+ * Only Rails date/time validation plugin offering complete validation (See
19
+ ORM/ODM support)
20
+ * Uses extensible date/time parser (Using
21
+ [timeliness gem][timeliness]. See Plugin Parser)
22
+ * Adds extensions to fix Rails date/time select issues (See Extensions)
23
+ * Supports I18n for the error messages
24
+ * Supports all the Rubies (that any sane person would be using in production).
25
+
26
+ ## Installation
27
+
28
+ # in Gemfile
29
+ gem 'jc-validates_timeliness', require: 'validates_timeliness'
30
+
31
+ # Run bundler
32
+ $ bundle install
33
+
34
+ Then run
35
+
36
+ $ rails generate validates_timeliness:install
37
+
38
+ This creates configuration initializer and locale files. In the initializer, there are a number of config
39
+ options to customize the plugin.
40
+
41
+ NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
42
+
43
+
44
+ ## Examples
45
+
46
+ validates_datetime :occurred_at
47
+
48
+ validates_date :date_of_birth, :before => lambda { 18.years.ago },
49
+ :before_message => "must be at least 18 years old"
50
+
51
+ validates_datetime :finish_time, :after => :start_time # Method symbol
52
+
53
+ validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
54
+
55
+ validates_time :booked_at, :between => ['9:00am', '5:00pm'] # On or after 9:00AM and on or before 5:00PM
56
+ validates_time :booked_at, :between => '9:00am'..'5:00pm' # The same as previous example
57
+ validates_time :booked_at, :between => '9:00am'...'5:00pm' # On or after 9:00AM and strictly before 5:00PM
58
+
59
+ validates_time :breakfast_time, :on_or_after => '6:00am',
60
+ :on_or_after_message => 'must be after opening time',
61
+ :before => :lunchtime,
62
+ :before_message => 'must be before lunch time'
63
+
64
+
65
+ ## Usage
66
+
67
+ To validate a model with a date, time or datetime attribute you just use the
68
+ validation method
69
+
70
+ class Person < ActiveRecord::Base
71
+ validates_date :date_of_birth, :on_or_before => lambda { Date.current }
72
+ # or
73
+ validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
74
+ end
75
+
76
+ or even on a specific record, per ActiveModel API.
77
+
78
+ @person.validates_date :date_of_birth, :on_or_before => lambda { Date.current }
79
+
80
+ The list of validation methods available are as follows:
81
+
82
+ validates_date - validate value as date
83
+ validates_time - validate value as time only i.e. '12:20pm'
84
+ validates_datetime - validate value as a full date and time
85
+ validates - use the :timeliness key and set the type in the hash.
86
+
87
+ The validation methods take the usual options plus some specific ones to restrict
88
+ the valid range of dates or times allowed
89
+
90
+ Temporal options (or restrictions):
91
+
92
+ :is_at - Attribute must be equal to value to be valid
93
+ :before - Attribute must be before this value to be valid
94
+ :on_or_before - Attribute must be equal to or before this value to be valid
95
+ :after - Attribute must be after this value to be valid
96
+ :on_or_after - Attribute must be equal to or after this value to be valid
97
+ :between - Attribute must be between the values to be valid. Range or Array of 2 values.
98
+
99
+ Regular validation options:
100
+
101
+ :allow_nil - Allow a nil value to be valid
102
+ :allow_blank - Allows a nil or empty string value to be valid
103
+ :if - Execute validation when :if evaluates true
104
+ :unless - Execute validation when :unless evaluates false
105
+ :on - Specify validation context e.g :save, :create or :update. Default is :save.
106
+
107
+ Special options:
108
+
109
+ :ignore_usec - Ignores microsecond value on datetime restrictions
110
+ :format - Limit validation to a single format for special cases. Requires plugin parser.
111
+
112
+ The temporal restrictions can take 4 different value types:
113
+
114
+ * Date, Time, or DateTime object value
115
+ * Proc or lambda object which may take an optional parameter, being the record object
116
+ * A symbol matching a method name in the model
117
+ * String value
118
+
119
+ When an attribute value is compared to temporal restrictions, they are compared as
120
+ the same type as the validation method type. So using validates_date means all
121
+ values are compared as dates.
122
+
123
+
124
+ ## Configuration
125
+
126
+ ### ORM/ODM Support
127
+
128
+ The plugin adds date/time validation to ActiveModel for any ORM/ODM that supports the ActiveModel validations component.
129
+ However, there is an issue with most ORM/ODMs which does not allow 100% date/time validation by default. Specifically, when you
130
+ assign an invalid date/time value to an attribute, most ORM/ODMs will only store a nil value for the attribute. This causes an
131
+ issue for date/time validation, since we need to know that a value was assigned but was invalid. To fix this, we need to cache
132
+ the original invalid value to know that the attribute is not just nil.
133
+
134
+ Each ORM/ODM requires a specific shim to fix it. The plugin includes a shim for ActiveRecord and Mongoid. You can activate them
135
+ like so
136
+
137
+ ValidatesTimeliness.setup do |config|
138
+
139
+ # Extend ORM/ODMs for full support (:active_record, :mongoid).
140
+ config.extend_orms = [ :mongoid ]
141
+
142
+ end
143
+
144
+ By default the plugin extends ActiveRecord if loaded. If you wish to extend
145
+ another ORM then look at the [wiki page][orm-support] for more information.
146
+
147
+ It is not required that you use a shim, but you will not catch errors when the
148
+ attribute value is invalid and evaluated to nil.
149
+
150
+ ### Error Messages
151
+
152
+ Using the I18n system to define new defaults:
153
+
154
+ en:
155
+ errors:
156
+ messages:
157
+ invalid_date: "is not a valid date"
158
+ invalid_time: "is not a valid time"
159
+ invalid_datetime: "is not a valid datetime"
160
+ is_at: "must be at %{restriction}"
161
+ before: "must be before %{restriction}"
162
+ on_or_before: "must be on or before %{restriction}"
163
+ after: "must be after %{restriction}"
164
+ on_or_after: "must be on or after %{restriction}"
165
+
166
+ The `%{restriction}` signifies where the interpolation value for the
167
+ restriction will be inserted.
168
+
169
+ You can also use validation options for custom error messages. The following
170
+ option keys are available:
171
+
172
+ :invalid_date_message
173
+ :invalid_time_message
174
+ :invalid_datetime_message
175
+ :is_at_message
176
+ :before_message
177
+ :on_or_before_message
178
+ :after_message
179
+ :on_or_after_message
180
+
181
+ Note: There is no :between_message option. The between error message should be
182
+ defined using the :on_or_after and :on_or_before (:before in case when
183
+ :between argument is a Range with excluded high value, see Examples) messages.
184
+
185
+ It is highly recommended you use the I18n system for error messages.
186
+
187
+ ### Plugin Parser
188
+
189
+ The plugin uses the [timeliness gem][timeliness] as a fast, configurable and
190
+ extensible date and time parser. You can add or remove valid formats for
191
+ dates, times, and datetimes. It is also more strict than the Ruby parser,
192
+ which means it won't accept day of the month if it's not a valid number for
193
+ the month.
194
+
195
+ By default the parser is disabled. To enable it:
196
+
197
+ # in the setup block
198
+ config.use_plugin_parser = true
199
+
200
+ Enabling the parser will mean that strings assigned to attributes validated
201
+ with the plugin will be parsed using the gem. See the [wiki][plugin-parser]
202
+ for more details about the parser configuration.
203
+
204
+
205
+ ### Restriction Shorthand
206
+
207
+ It is common to restrict an attribute to being on or before the current time
208
+ or current day. To specify this you need to use a lambda as an option value
209
+ e.g. `lambda { Time.current }. This can be tedious noise amongst your
210
+ validations for something so common. To combat this the plugin allows you to
211
+ use shorthand symbols for often used relative times or dates.
212
+
213
+ Just provide the symbol as the option value like so:
214
+
215
+ validates_date :birth_date, :on_or_before => :today
216
+
217
+ The :today symbol is evaluated as `lambda { Date.today }`. The :now and :today
218
+ symbols are pre-configured. Configure your own like so:
219
+
220
+ # in the setup block
221
+ config.restriction_shorthand_symbols.update(
222
+ :yesterday => lambda { 1.day.ago }
223
+ )
224
+
225
+ ### Default Timezone
226
+
227
+ The plugin needs to know the default timezone you are using when parsing or
228
+ type casting values. If you are using ActiveRecord then the default is
229
+ automatically set to the same default zone as ActiveRecord. If you are using
230
+ another ORM you may need to change this setting.
231
+
232
+ # in the setup block
233
+ config.default_timezone = :utc
234
+
235
+ By default it will be UTC if ActiveRecord is not loaded.
236
+
237
+ ### Dummy Date For Time Types
238
+
239
+ Given that Ruby has no support for a time-only type, all time type columns are
240
+ evaluated as a regular Time class objects with a dummy date value set. Rails
241
+ defines the dummy date as 2000-01-01. So a time of '12:30' is evaluated as a
242
+ Time value of '2000-01-01 12:30'. If you need to customize this for some
243
+ reason you can do so as follows
244
+
245
+ # in the setup block
246
+ config.dummy_date_for_time_type = [2009, 1, 1]
247
+
248
+ The value should be an array of 3 values being year, month and day in that order.
249
+
250
+ ### Temporal Restriction Errors
251
+
252
+ When using the validation temporal restrictions there are times when the
253
+ restriction option value itself may be invalid. This will add an error to the
254
+ model such as 'Error occurred validating birth_date for :before restriction'.
255
+ These can be annoying in development or production as you most likely just
256
+ want to skip the option if no valid value was returned. By default these
257
+ errors are displayed in Rails test mode.
258
+
259
+ To turn them on/off:
260
+
261
+ # in the setup block
262
+ config.ignore_restriction_errors = true
263
+
264
+ ## Extensions
265
+
266
+ ### Strict Parsing for Select Helpers
267
+
268
+ When using date/time select helpers, the component values are handled by
269
+ ActiveRecord using the Time class to instantiate them into a time value. This
270
+ means that some invalid dates, such as 31st June, are shifted forward and
271
+ treated as valid. To handle these cases in a strict way, you can enable the
272
+ plugin extension to treat them as invalid dates.
273
+
274
+ To activate it, uncomment this line in the initializer:
275
+
276
+ # in the setup block
277
+ config.enable_multiparameter_extension!
278
+
279
+ ### Display Invalid Values in Select Helpers
280
+
281
+ The plugin offers an extension for ActionView to allowing invalid date and time values to be
282
+ redisplayed to the user as feedback, instead of a blank field which happens by default in
283
+ Rails. Though the date helpers make this a pretty rare occurrence, given the select dropdowns
284
+ for each date/time component, but it may be something of interest.
285
+
286
+ To activate it, uncomment this line in the initializer:
287
+
288
+ # in the setup block
289
+ config.enable_date_time_select_extension!
290
+
291
+ ## Contributors
292
+
293
+ This is a fork of the original validates_timeliness gem by
294
+ [Adam Meehan][adzap].
295
+
296
+ To see the generous people who have contributed code, take a look at the
297
+ [contributors list](http://github.com/johncarney/validates_timeliness/contributors).
298
+
299
+ ## Maintainers
300
+
301
+ * [John Carney][jc]
302
+
303
+ ## License
304
+
305
+ Copyright (c) 2008 Adam Meehan, released under the MIT license
306
+
307
+ [jc]: http://github.com/johncarney
308
+ [adzap]: http://github.com/adzap
309
+ [timeliness]: http://github.com/adzap/timeliness
310
+ [orm-support]: http://github.com/adzap/validates_timeliness/wiki/ORM-Support
311
+ [plugin-parser]: http://github.com/adzap/validates_timeliness/wiki/Plugin-Parser
@@ -0,0 +1 @@
1
+ require "validates_timeliness"
@@ -1,3 +1,3 @@
1
1
  module ValidatesTimeliness
2
- VERSION = '3.1.0'
2
+ VERSION = '3.1.1'
3
3
  end
@@ -1,6 +1,13 @@
1
1
  require 'rspec'
2
2
  require 'rspec/collection_matchers'
3
3
 
4
+ # Coveralls
5
+ require "coveralls"
6
+
7
+ Coveralls.wear! do
8
+ add_filter 'spec'
9
+ end
10
+
4
11
  require 'active_model'
5
12
  require 'active_model/validations'
6
13
  require 'active_record'
@@ -58,6 +65,8 @@ class PersonWithShim < Person
58
65
  include TestModelShim
59
66
  end
60
67
 
68
+ I18n.enforce_available_locales = false
69
+
61
70
  ActiveRecord::Base.default_timezone = :utc
62
71
  ActiveRecord::Base.time_zone_aware_attributes = true
63
72
  ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
@@ -5,7 +5,6 @@ module TestModel
5
5
  include ActiveModel::AttributeMethods
6
6
 
7
7
  included do
8
- attribute_method_suffix ""
9
8
  attribute_method_suffix "="
10
9
  cattr_accessor :model_attributes
11
10
  end
@@ -12,9 +12,14 @@ Gem::Specification.new do |s|
12
12
  s.homepage = %q{http://github.com/johncarney/validates_timeliness}
13
13
 
14
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/*']
15
+ s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb Appraisals .travis.yml .coveralls.yml } - Dir['gemfiles/*']
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.extra_rdoc_files = ["README.rdoc", "CHANGELOG.rdoc", "LICENSE"]
17
+ s.extra_rdoc_files = ["CHANGELOG.rdoc", "LICENSE"]
18
18
 
19
19
  s.add_runtime_dependency(%q<timeliness>, ["~> 0.3.7"])
20
+
21
+ s.add_development_dependency "coveralls"
22
+ s.add_development_dependency "rspec", "~> 3.0"
23
+ s.add_development_dependency "rspec-rails", "~> 3.0"
24
+ s.add_development_dependency "rspec-collection_matchers"
20
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jc-validates_timeliness
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-16 00:00:00.000000000 Z
12
+ date: 2014-07-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: timeliness
@@ -25,32 +25,80 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: 0.3.7
28
+ - !ruby/object:Gem::Dependency
29
+ name: coveralls
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec-rails
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec-collection_matchers
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
28
84
  description: Adds validation methods to ActiveModel for validating dates and times.
29
85
  Works with multiple ORMS.
30
86
  email: adam.meehan@gmail.com
31
87
  executables: []
32
88
  extensions: []
33
89
  extra_rdoc_files:
34
- - README.rdoc
35
90
  - CHANGELOG.rdoc
36
91
  - LICENSE
37
92
  files:
38
- - ".travis.yml"
39
93
  - CHANGELOG.rdoc
40
94
  - LICENSE
41
- - README.rdoc
95
+ - README.md
42
96
  - 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
97
  - init.rb
51
98
  - lib/generators/validates_timeliness/install_generator.rb
52
99
  - lib/generators/validates_timeliness/templates/en.yml
53
100
  - lib/generators/validates_timeliness/templates/validates_timeliness.rb
101
+ - lib/jc-validates_timeliness.rb
54
102
  - lib/validates_timeliness.rb
55
103
  - lib/validates_timeliness/attribute_methods.rb
56
104
  - lib/validates_timeliness/conversion.rb
@@ -1,14 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
-
4
- rvm:
5
- - 1.9.3
6
-
7
- script: 'bundle exec rake'
8
-
9
- notifications:
10
- email:
11
- recipients:
12
- - john+timeliness@carney.id.au
13
- on_failure: change
14
- on_success: never
@@ -1,301 +0,0 @@
1
- = ValidatesTimeliness
2
-
3
- * Source: http://github.com/johncarney/validates_timeliness
4
- * Issues: http://github.com/johncarney/validates_timeliness/issues
5
-
6
- == Description
7
-
8
- Complete validation of dates, times and datetimes for Rails 3.x and ActiveModel.
9
-
10
- If you a looking for the old version for Rails 2.x go here[http://github.com/adzap/validates_timeliness/tree/v2.3].
11
-
12
-
13
- == Features
14
-
15
- * Adds validation for dates, times and datetimes to ActiveModel
16
-
17
- * Handles timezones and type casting of values for you
18
-
19
- * Only Rails date/time validation plugin offering complete validation (See ORM/ODM support)
20
-
21
- * Uses extensible date/time parser (Using {timeliness gem}[http://github.com/adzap/timeliness]. See Plugin Parser)
22
-
23
- * Adds extensions to fix Rails date/time select issues (See Extensions)
24
-
25
- * Supports I18n for the error messages
26
-
27
- * Supports all the Rubies (that any sane person would be using in production).
28
-
29
-
30
- == Installation
31
-
32
- # in Gemfile
33
- gem 'jc-validates_timeliness', '~> 3.1'
34
-
35
- # Run bundler
36
- $ bundle install
37
-
38
- Then run
39
-
40
- $ rails generate validates_timeliness:install
41
-
42
- This creates configuration initializer and locale files. In the initializer, there are a number of config
43
- options to customize the plugin.
44
-
45
- NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
46
-
47
-
48
- == Examples
49
-
50
- validates_datetime :occurred_at
51
-
52
- validates_date :date_of_birth, :before => lambda { 18.years.ago },
53
- :before_message => "must be at least 18 years old"
54
-
55
- validates_datetime :finish_time, :after => :start_time # Method symbol
56
-
57
- validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
58
-
59
- validates_time :booked_at, :between => ['9:00am', '5:00pm'] # On or after 9:00AM and on or before 5:00PM
60
- validates_time :booked_at, :between => '9:00am'..'5:00pm' # The same as previous example
61
- validates_time :booked_at, :between => '9:00am'...'5:00pm' # On or after 9:00AM and strictly before 5:00PM
62
-
63
- validates_time :breakfast_time, :on_or_after => '6:00am',
64
- :on_or_after_message => 'must be after opening time',
65
- :before => :lunchtime,
66
- :before_message => 'must be before lunch time'
67
-
68
-
69
- == Usage
70
-
71
- To validate a model with a date, time or datetime attribute you just use the
72
- validation method
73
-
74
- class Person < ActiveRecord::Base
75
- validates_date :date_of_birth, :on_or_before => lambda { Date.current }
76
- # or
77
- validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
78
- end
79
-
80
- or even on a specific record, per ActiveModel API.
81
-
82
- @person.validates_date :date_of_birth, :on_or_before => lambda { Date.current }
83
-
84
-
85
- The list of validation methods available are as follows:
86
- validates_date - validate value as date
87
- validates_time - validate value as time only i.e. '12:20pm'
88
- validates_datetime - validate value as a full date and time
89
- validates - use the :timeliness key and set the type in the hash.
90
-
91
- The validation methods take the usual options plus some specific ones to restrict
92
- the valid range of dates or times allowed
93
-
94
- Temporal options (or restrictions):
95
- :is_at - Attribute must be equal to value to be valid
96
- :before - Attribute must be before this value to be valid
97
- :on_or_before - Attribute must be equal to or before this value to be valid
98
- :after - Attribute must be after this value to be valid
99
- :on_or_after - Attribute must be equal to or after this value to be valid
100
- :between - Attribute must be between the values to be valid. Range or Array of 2 values.
101
-
102
- Regular validation options:
103
- :allow_nil - Allow a nil value to be valid
104
- :allow_blank - Allows a nil or empty string value to be valid
105
- :if - Execute validation when :if evaluates true
106
- :unless - Execute validation when :unless evaluates false
107
- :on - Specify validation context e.g :save, :create or :update. Default is :save.
108
-
109
- Special options:
110
- :ignore_usec - Ignores microsecond value on datetime restrictions
111
- :format - Limit validation to a single format for special cases. Requires plugin parser.
112
-
113
- The temporal restrictions can take 4 different value types:
114
-
115
- * Date, Time, or DateTime object value
116
- * Proc or lambda object which may take an optional parameter, being the record object
117
- * A symbol matching a method name in the model
118
- * String value
119
-
120
- When an attribute value is compared to temporal restrictions, they are compared as
121
- the same type as the validation method type. So using validates_date means all
122
- values are compared as dates.
123
-
124
-
125
- == Configuration
126
-
127
- === ORM/ODM Support
128
-
129
- The plugin adds date/time validation to ActiveModel for any ORM/ODM that supports the ActiveModel validations component.
130
- However, there is an issue with most ORM/ODMs which does not allow 100% date/time validation by default. Specifically, when you
131
- assign an invalid date/time value to an attribute, most ORM/ODMs will only store a nil value for the attribute. This causes an
132
- issue for date/time validation, since we need to know that a value was assigned but was invalid. To fix this, we need to cache
133
- the original invalid value to know that the attribute is not just nil.
134
-
135
- Each ORM/ODM requires a specific shim to fix it. The plugin includes a shim for ActiveRecord and Mongoid. You can activate them
136
- like so
137
-
138
- ValidatesTimeliness.setup do |config|
139
-
140
- # Extend ORM/ODMs for full support (:active_record, :mongoid).
141
- config.extend_orms = [ :mongoid ]
142
-
143
- end
144
-
145
- By default the plugin extends ActiveRecord if loaded. If you wish to extend another ORM then look at the {wiki page}[http://github.com/adzap/validates_timeliness/wiki/ORM-Support] for more information.
146
-
147
- It is not required that you use a shim, but you will not catch errors when the attribute value is invalid and evaluated to nil.
148
-
149
-
150
- === Error Messages
151
-
152
- Using the I18n system to define new defaults:
153
-
154
- en:
155
- errors:
156
- messages:
157
- invalid_date: "is not a valid date"
158
- invalid_time: "is not a valid time"
159
- invalid_datetime: "is not a valid datetime"
160
- is_at: "must be at %{restriction}"
161
- before: "must be before %{restriction}"
162
- on_or_before: "must be on or before %{restriction}"
163
- after: "must be after %{restriction}"
164
- on_or_after: "must be on or after %{restriction}"
165
-
166
- The %{restriction} signifies where the interpolation value for the restriction will be inserted.
167
-
168
- You can also use validation options for custom error messages. The following option keys are available:
169
-
170
- :invalid_date_message
171
- :invalid_time_message
172
- :invalid_datetime_message
173
- :is_at_message
174
- :before_message
175
- :on_or_before_message
176
- :after_message
177
- :on_or_after_message
178
-
179
- Note: There is no :between_message option. The between error message should be defined using the :on_or_after and :on_or_before
180
- (:before in case when :between argument is a Range with excluded high value, see Examples) messages.
181
-
182
- It is highly recommended you use the I18n system for error messages.
183
-
184
-
185
- === Plugin Parser
186
-
187
- The plugin uses the {timeliness gem}[http://github.com/adzap/timeliness] as a fast, configurable and extensible date and time parser.
188
- You can add or remove valid formats for dates, times, and datetimes. It is also more strict than the
189
- Ruby parser, which means it won't accept day of the month if it's not a valid number for the month.
190
-
191
- By default the parser is disabled. To enable it:
192
-
193
- # in the setup block
194
- config.use_plugin_parser = true
195
-
196
- Enabling the parser will mean that strings assigned to attributes validated with the plugin will be parsed
197
- using the gem. See the wiki[http://github.com/adzap/validates_timeliness/wiki/Plugin-Parser] for more details about the parser configuration.
198
-
199
-
200
- === Restriction Shorthand
201
-
202
- It is common to restrict an attribute to being on or before the current time or current day.
203
- To specify this you need to use a lambda as an option value e.g. <tt>lambda { Time.current }</tt>.
204
- This can be tedious noise amongst your validations for something so common. To combat this the
205
- plugin allows you to use shorthand symbols for often used relative times or dates.
206
-
207
- Just provide the symbol as the option value like so:
208
-
209
- validates_date :birth_date, :on_or_before => :today
210
-
211
- The :today symbol is evaluated as <tt>lambda { Date.today }</tt>. The :now and :today
212
- symbols are pre-configured. Configure your own like so:
213
-
214
- # in the setup block
215
- config.restriction_shorthand_symbols.update(
216
- :yesterday => lambda { 1.day.ago }
217
- )
218
-
219
-
220
- === Default Timezone
221
-
222
- The plugin needs to know the default timezone you are using when parsing or type casting values. If you are using
223
- ActiveRecord then the default is automatically set to the same default zone as ActiveRecord. If you are using
224
- another ORM you may need to change this setting.
225
-
226
- # in the setup block
227
- config.default_timezone = :utc
228
-
229
- By default it will be UTC if ActiveRecord is not loaded.
230
-
231
-
232
- === Dummy Date For Time Types
233
-
234
- Given that Ruby has no support for a time-only type, all time type columns are evaluated
235
- as a regular Time class objects with a dummy date value set. Rails defines the dummy date as
236
- 2000-01-01. So a time of '12:30' is evaluated as a Time value of '2000-01-01 12:30'. If you
237
- need to customize this for some reason you can do so as follows
238
-
239
- # in the setup block
240
- config.dummy_date_for_time_type = [2009, 1, 1]
241
-
242
- The value should be an array of 3 values being year, month and day in that order.
243
-
244
-
245
- === Temporal Restriction Errors
246
-
247
- When using the validation temporal restrictions there are times when the restriction
248
- option value itself may be invalid. This will add an error to the model such as
249
- 'Error occurred validating birth_date for :before restriction'. These can be annoying
250
- in development or production as you most likely just want to skip the option if no
251
- valid value was returned. By default these errors are displayed in Rails test mode.
252
-
253
- To turn them on/off:
254
-
255
- # in the setup block
256
- config.ignore_restriction_errors = true
257
-
258
-
259
- == Extensions
260
-
261
- === Strict Parsing for Select Helpers
262
-
263
- When using date/time select helpers, the component values are handled by ActiveRecord using
264
- the Time class to instantiate them into a time value. This means that some invalid dates,
265
- such as 31st June, are shifted forward and treated as valid. To handle these cases in a strict
266
- way, you can enable the plugin extension to treat them as invalid dates.
267
-
268
- To activate it, uncomment this line in the initializer:
269
-
270
- # in the setup block
271
- config.enable_multiparameter_extension!
272
-
273
-
274
- === Display Invalid Values in Select Helpers
275
-
276
- The plugin offers an extension for ActionView to allowing invalid date and time values to be
277
- redisplayed to the user as feedback, instead of a blank field which happens by default in
278
- Rails. Though the date helpers make this a pretty rare occurrence, given the select dropdowns
279
- for each date/time component, but it may be something of interest.
280
-
281
- To activate it, uncomment this line in the initializer:
282
-
283
- # in the setup block
284
- config.enable_date_time_select_extension!
285
-
286
-
287
- == Contributors
288
-
289
- This is a fork of the original validates_timeliness gem by {Adam Meehan}[http://github.com/adzap].
290
-
291
- To see the generous people who have contributed code, take a look at the {contributors list}[http://github.com/johncarney/validates_timeliness/contributors].
292
-
293
-
294
- == Maintainers
295
-
296
- * {John Carney}[http://github.com/johncarney]
297
-
298
-
299
- == License
300
-
301
- Copyright (c) 2008 Adam Meehan, released under the MIT license
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 3.2.6"
6
- gem "rspec", "~> 2.8"
7
- gem "rspec-rails", "~> 2.8"
8
- gem "timecop"
9
- gem "rspec_tag_matchers"
10
- gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
11
- gem "debugger", :platforms=>[:ruby_19]
12
- gem "appraisal"
13
- gem "sqlite3"
14
- gem "mongoid", "~> 2.1.0"
15
-
16
- gemspec :path=>"../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 3.2.6"
6
- gem "rspec", "~> 2.8"
7
- gem "rspec-rails", "~> 2.8"
8
- gem "timecop"
9
- gem "rspec_tag_matchers"
10
- gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
11
- gem "debugger", :platforms=>[:ruby_19]
12
- gem "appraisal"
13
- gem "sqlite3"
14
- gem "mongoid", "~> 2.2.0"
15
-
16
- gemspec :path=>"../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 3.2.6"
6
- gem "rspec", "~> 2.8"
7
- gem "rspec-rails", "~> 2.8"
8
- gem "timecop"
9
- gem "rspec_tag_matchers"
10
- gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
11
- gem "debugger", :platforms=>[:ruby_19]
12
- gem "appraisal"
13
- gem "sqlite3"
14
- gem "mongoid", "~> 2.3.0"
15
-
16
- gemspec :path=>"../"
@@ -1,16 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rails", "~> 3.2.6"
6
- gem "rspec", "~> 2.8"
7
- gem "rspec-rails", "~> 2.8"
8
- gem "timecop"
9
- gem "rspec_tag_matchers"
10
- gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
11
- gem "debugger", :platforms=>[:ruby_19]
12
- gem "appraisal"
13
- gem "sqlite3"
14
- gem "mongoid", "~> 2.4.0"
15
-
16
- gemspec :path=>"../"
@@ -1,15 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rspec", "~> 2.8"
6
- gem "rspec-rails", "~> 2.8"
7
- gem "timecop"
8
- gem "rspec_tag_matchers"
9
- gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
10
- gem "debugger", :platforms=>[:ruby_19]
11
- gem "appraisal"
12
- gem "sqlite3"
13
- gem "rails", "~> 3.0.0"
14
-
15
- gemspec :path=>"../"
@@ -1,15 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rspec", "~> 2.8"
6
- gem "rspec-rails", "~> 2.8"
7
- gem "timecop"
8
- gem "rspec_tag_matchers"
9
- gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
10
- gem "debugger", :platforms=>[:ruby_19]
11
- gem "appraisal"
12
- gem "sqlite3"
13
- gem "rails", "~> 3.1.0"
14
-
15
- gemspec :path=>"../"
@@ -1,15 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "http://rubygems.org"
4
-
5
- gem "rspec", "~> 2.8"
6
- gem "rspec-rails", "~> 2.8"
7
- gem "timecop"
8
- gem "rspec_tag_matchers"
9
- gem "ruby-debug", :platforms=>[:ruby_18, :jruby]
10
- gem "debugger", :platforms=>[:ruby_19]
11
- gem "appraisal"
12
- gem "sqlite3"
13
- gem "rails", "~> 3.2.0"
14
-
15
- gemspec :path=>"../"