validates_timeliness 5.0.0.beta2 → 6.0.0.beta2
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 +4 -4
- data/.github/dependabot.yml +7 -0
- data/.github/workflows/ci.yml +41 -0
- data/CHANGELOG.rdoc +3 -1
- data/LICENSE +1 -1
- data/README.md +321 -0
- data/Rakefile +0 -10
- data/gemfiles/{rails_5_2.gemfile → rails_6_0.gemfile} +2 -2
- data/gemfiles/{rails_5_0.gemfile → rails_6_1.gemfile} +2 -2
- data/gemfiles/{rails_5_1.gemfile → rails_edge.gemfile} +2 -2
- data/lib/validates_timeliness/extensions/date_time_select.rb +1 -1
- data/lib/validates_timeliness/validator.rb +3 -3
- data/lib/validates_timeliness/version.rb +1 -1
- data/spec/support/test_model.rb +2 -2
- data/validates_timeliness.gemspec +4 -3
- metadata +30 -9
- data/.travis.yml +0 -20
- data/README.rdoc +0 -299
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc002013bf4a78cba246d4a99133e7cf50a4728125f6472621f2b8a563ea0857
|
4
|
+
data.tar.gz: fd59680e5636166eeff6848fb3a2560878c583a5fc60dbea9aba9d52fa53e0de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b80b655d4df7e324fc42bdd2b3006e7c5cf8a0fb4fd6d0d27155a94a4ce4e9b93e7ffa1e9eb0f79fb7635a6a0bc75d34112d25a425cd553b0be2a641fb93b626
|
7
|
+
data.tar.gz: 1ad906a88d910f0dae8ef4f0d09431776b43c9db85ab587b8f35ea18fd8cdb75a6dcbea666b745b7cc17dbc998f6e3274429ecc1e8fb5c47c5beabfb4ac42d23
|
@@ -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/CHANGELOG.rdoc
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
= [
|
1
|
+
= 5.0.0 [2021-04-03]
|
2
2
|
* Fix DateTimeSelect extension support (AquisTech)
|
3
3
|
* Relaxed Timeliness dependency version which allows for >= 0.4.0 with
|
4
4
|
threadsafety fix for use_us_formats and use_euro_formats for hot switching
|
5
5
|
in a request.
|
6
6
|
* Add initializer to ensure Timeliness v0.4+ ambiguous date config is set
|
7
7
|
correctly when using `use_euro_formats` or `remove_use_formats'.
|
8
|
+
* Add Ruby 3 compatibility
|
9
|
+
* Add Rails 6.1 compatibility
|
8
10
|
|
9
11
|
Breaking Changes
|
10
12
|
* Update Multiparameter extension to use ActiveRecord type classes with multiparameter handling
|
data/LICENSE
CHANGED
data/README.md
ADDED
@@ -0,0 +1,321 @@
|
|
1
|
+
# ValidatesTimeliness [](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,14 +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.rdoc_dir = 'rdoc'
|
23
|
-
rdoc.title = 'ValidatesTimeliness'
|
24
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
25
|
-
rdoc.rdoc_files.include('README')
|
26
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
27
|
-
end
|
28
|
-
|
29
19
|
desc 'Default: run specs.'
|
30
20
|
task :default => :spec
|
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "rails", "~>
|
5
|
+
gem "rails", "~> 6.0.0"
|
6
6
|
gem "rspec"
|
7
7
|
gem "rspec-rails", "~> 3.7"
|
8
|
+
gem "sqlite3"
|
8
9
|
gem "timecop"
|
9
10
|
gem "byebug"
|
10
11
|
gem "appraisal"
|
11
|
-
gem "sqlite3", "~> 1.3.6"
|
12
12
|
gem "nokogiri", "~> 1.8"
|
13
13
|
|
14
14
|
gemspec path: "../"
|
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "rails", "~>
|
5
|
+
gem "rails", "~> 6.1.0"
|
6
6
|
gem "rspec"
|
7
7
|
gem "rspec-rails", "~> 3.7"
|
8
|
+
gem "sqlite3"
|
8
9
|
gem "timecop"
|
9
10
|
gem "byebug"
|
10
11
|
gem "appraisal"
|
11
|
-
gem "sqlite3", "~> 1.3.6"
|
12
12
|
gem "nokogiri", "~> 1.8"
|
13
13
|
|
14
14
|
gemspec path: "../"
|
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "rails", "
|
5
|
+
gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
|
6
6
|
gem "rspec"
|
7
7
|
gem "rspec-rails", "~> 3.7"
|
8
|
+
gem "sqlite3"
|
8
9
|
gem "timecop"
|
9
10
|
gem "byebug"
|
10
11
|
gem "appraisal"
|
11
|
-
gem "sqlite3", "~> 1.3.6"
|
12
12
|
gem "nokogiri", "~> 1.8"
|
13
13
|
|
14
14
|
gemspec path: "../"
|
@@ -85,12 +85,12 @@ module ValidatesTimeliness
|
|
85
85
|
|
86
86
|
def add_error(record, attr_name, message, value=nil)
|
87
87
|
value = format_error_value(value) if value
|
88
|
-
message_options = { :
|
89
|
-
record.errors.add(attr_name, message, message_options)
|
88
|
+
message_options = { message: options.fetch(:"#{message}_message", options[:message]), restriction: value }
|
89
|
+
record.errors.add(attr_name, message, **message_options)
|
90
90
|
end
|
91
91
|
|
92
92
|
def format_error_value(value)
|
93
|
-
format = I18n.t(@type, :
|
93
|
+
format = I18n.t(@type, default: DEFAULT_ERROR_VALUE_FORMATS[@type], scope: 'validates_timeliness.error_value_formats')
|
94
94
|
value.strftime(format)
|
95
95
|
end
|
96
96
|
|
data/spec/support/test_model.rb
CHANGED
@@ -15,11 +15,11 @@ module TestModel
|
|
15
15
|
self.model_attributes[name] = type
|
16
16
|
end
|
17
17
|
|
18
|
-
def define_method_attribute=(attr_name)
|
18
|
+
def define_method_attribute=(attr_name, owner: nil)
|
19
19
|
generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=self.class.type_cast('#{attr_name}', new_value); end", __FILE__, __LINE__)
|
20
20
|
end
|
21
21
|
|
22
|
-
def define_method_attribute(attr_name)
|
22
|
+
def define_method_attribute(attr_name, owner: nil)
|
23
23
|
generated_attribute_methods.module_eval("def #{attr_name}; @attributes['#{attr_name}']; end", __FILE__, __LINE__)
|
24
24
|
end
|
25
25
|
|
@@ -13,9 +13,10 @@ 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
|
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.
|
18
|
+
s.extra_rdoc_files = ["README.md", "CHANGELOG.rdoc", "LICENSE"]
|
19
19
|
|
20
|
-
s.add_runtime_dependency(
|
20
|
+
s.add_runtime_dependency("activemodel", [">= 6.0.0", "< 7"])
|
21
|
+
s.add_runtime_dependency("timeliness", [">= 0.3.10", "< 1"])
|
21
22
|
end
|
metadata
CHANGED
@@ -1,15 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_timeliness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
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:
|
11
|
+
date: 2021-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.0.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '7'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 6.0.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7'
|
13
33
|
- !ruby/object:Gem::Dependency
|
14
34
|
name: timeliness
|
15
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -36,18 +56,19 @@ email: adam.meehan@gmail.com
|
|
36
56
|
executables: []
|
37
57
|
extensions: []
|
38
58
|
extra_rdoc_files:
|
39
|
-
- README.
|
59
|
+
- README.md
|
40
60
|
- CHANGELOG.rdoc
|
41
61
|
- LICENSE
|
42
62
|
files:
|
43
|
-
- ".
|
63
|
+
- ".github/dependabot.yml"
|
64
|
+
- ".github/workflows/ci.yml"
|
44
65
|
- CHANGELOG.rdoc
|
45
66
|
- LICENSE
|
46
|
-
- README.
|
67
|
+
- README.md
|
47
68
|
- Rakefile
|
48
|
-
- gemfiles/
|
49
|
-
- gemfiles/
|
50
|
-
- gemfiles/
|
69
|
+
- gemfiles/rails_6_0.gemfile
|
70
|
+
- gemfiles/rails_6_1.gemfile
|
71
|
+
- gemfiles/rails_edge.gemfile
|
51
72
|
- init.rb
|
52
73
|
- lib/generators/validates_timeliness/install_generator.rb
|
53
74
|
- lib/generators/validates_timeliness/templates/en.yml
|
@@ -103,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
124
|
- !ruby/object:Gem::Version
|
104
125
|
version: 1.3.1
|
105
126
|
requirements: []
|
106
|
-
rubygems_version: 3.0.3
|
127
|
+
rubygems_version: 3.0.3.1
|
107
128
|
signing_key:
|
108
129
|
specification_version: 4
|
109
130
|
summary: Date and time validation plugin for Rails which allows custom formats
|
data/.travis.yml
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
before_install: gem install bundler
|
3
|
-
cache: bundler
|
4
|
-
|
5
|
-
gemfile:
|
6
|
-
- gemfiles/rails_5_0.gemfile
|
7
|
-
- gemfiles/rails_5_1.gemfile
|
8
|
-
- gemfiles/rails_5_2.gemfile
|
9
|
-
|
10
|
-
rvm:
|
11
|
-
- "2.5.3"
|
12
|
-
|
13
|
-
script: 'bundle exec rspec'
|
14
|
-
|
15
|
-
notifications:
|
16
|
-
email:
|
17
|
-
recipients:
|
18
|
-
- adam.meehan@gmail.com
|
19
|
-
on_failure: change
|
20
|
-
on_success: never
|
data/README.rdoc
DELETED
@@ -1,299 +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: http://github.com/adzap/validates_timeliness
|
4
|
-
* Issues: http://github.com/adzap/validates_timeliness/issues
|
5
|
-
|
6
|
-
== Description
|
7
|
-
|
8
|
-
Complete validation of dates, times and datetimes for Rails 5.x and ActiveModel.
|
9
|
-
|
10
|
-
If you a looking for the old version for Rails 4.x go here [https://github.com/adzap/validates_timeliness/tree/4-0-stable].
|
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. For multi-language support try {timeliness-i18n gem}[https://github.com/pedrofurtado/timeliness-i18n].
|
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 'validates_timeliness', '~> 5.0.0.beta1'
|
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).
|
141
|
-
config.extend_orms = [ :active_record ]
|
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
|
-
To see the generous people who have contributed code, take a look at the {contributors list}[http://github.com/adzap/validates_timeliness/contributors].
|
290
|
-
|
291
|
-
|
292
|
-
== Maintainers
|
293
|
-
|
294
|
-
* {Adam Meehan}[http://github.com/adzap]
|
295
|
-
|
296
|
-
|
297
|
-
== License
|
298
|
-
|
299
|
-
Copyright (c) 2008 Adam Meehan, released under the MIT license
|