validates_timeliness 6.0.0.beta1 → 6.0.0.beta2

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