sp-validates_timeliness 3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.rdoc +183 -0
- data/LICENSE +20 -0
- data/README.md +323 -0
- data/Rakefile +30 -0
- data/init.rb +1 -0
- data/lib/generators/validates_timeliness/install_generator.rb +16 -0
- data/lib/generators/validates_timeliness/templates/en.yml +16 -0
- data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +40 -0
- data/lib/jc-validates_timeliness.rb +1 -0
- data/lib/validates_timeliness.rb +70 -0
- data/lib/validates_timeliness/attribute_methods.rb +95 -0
- data/lib/validates_timeliness/conversion.rb +70 -0
- data/lib/validates_timeliness/extensions.rb +14 -0
- data/lib/validates_timeliness/extensions/date_time_select.rb +61 -0
- data/lib/validates_timeliness/extensions/multiparameter_handler.rb +80 -0
- data/lib/validates_timeliness/helper_methods.rb +23 -0
- data/lib/validates_timeliness/orm/active_record.rb +53 -0
- data/lib/validates_timeliness/orm/mongoid.rb +63 -0
- data/lib/validates_timeliness/railtie.rb +15 -0
- data/lib/validates_timeliness/validator.rb +117 -0
- data/lib/validates_timeliness/version.rb +3 -0
- data/spec/spec_helper.rb +109 -0
- data/spec/support/config_helper.rb +36 -0
- data/spec/support/model_helpers.rb +27 -0
- data/spec/support/tag_matcher.rb +35 -0
- data/spec/support/test_model.rb +59 -0
- data/spec/validates_timeliness/attribute_methods_spec.rb +86 -0
- data/spec/validates_timeliness/conversion_spec.rb +234 -0
- data/spec/validates_timeliness/extensions/date_time_select_spec.rb +163 -0
- data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +44 -0
- data/spec/validates_timeliness/helper_methods_spec.rb +30 -0
- data/spec/validates_timeliness/orm/active_record_spec.rb +244 -0
- data/spec/validates_timeliness/orm/mongoid_spec.rb +189 -0
- data/spec/validates_timeliness/validator/after_spec.rb +57 -0
- data/spec/validates_timeliness/validator/before_spec.rb +57 -0
- data/spec/validates_timeliness/validator/is_at_spec.rb +61 -0
- data/spec/validates_timeliness/validator/on_or_after_spec.rb +57 -0
- data/spec/validates_timeliness/validator/on_or_before_spec.rb +57 -0
- data/spec/validates_timeliness/validator_spec.rb +246 -0
- data/spec/validates_timeliness_spec.rb +43 -0
- data/validates_timeliness.gemspec +25 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 14a6e4850a171da43f31e296fb95f1c27607b74e
|
4
|
+
data.tar.gz: 41fd9a72a1b6e16783623ca1b0708a42e23c6480
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 82bb60ec36a5d2c127eb95cf162c7c8f274979a6576e3929ac4200a0bb102ca6e68e76267991b58b366cdc74c9e0bd35f75d8e4ce04650f1a6fe90e7586ba912
|
7
|
+
data.tar.gz: 47e1fed2ffe7d4f12f807f859806a169a3275ee2a4b5ea5a6a51667b44f338556a1c856997024654ec220f53bcb959d28393df621a69b4394aee4b35642f0c30
|
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
= 3.0.14 [2012-08-23]
|
2
|
+
* Fix for using validates :timeliness => {} form to correctly add attributes to timeliness validated attributes.
|
3
|
+
|
4
|
+
= 3.0.13 [2012-08-21]
|
5
|
+
* Fix ActiveRecord issues with using plugin parser by using old way of caching values.
|
6
|
+
* Allow any ActiveRecord non-column attribute to be validated
|
7
|
+
|
8
|
+
= 3.0.12 [2012-06-23]
|
9
|
+
* Fix load order issue when relying on Railtie to load ActiveRecord extension
|
10
|
+
|
11
|
+
= 3.0.11 [2012-04-01]
|
12
|
+
* Change dependency on Timeliness version due to a broken release
|
13
|
+
|
14
|
+
= 3.0.10 [2012-03-26]
|
15
|
+
* Fix for ActiveRecord shim and validation with :allow_blank => true in AR 3.1+. Fixes issue#52.
|
16
|
+
|
17
|
+
= 3.0.9 [2012-03-26]
|
18
|
+
* ActiveRecord 3.1+ suport
|
19
|
+
* Fixes for multiparameter extension with empty date values (thanks @mogox, @Sharagoz)
|
20
|
+
|
21
|
+
= 3.0.8 [2011-12-24]
|
22
|
+
* Remove deprecated InstanceMethods module when using AS::Concern (carlosantoniodasilva)
|
23
|
+
* Update Mongoid shim for v2.3 compatability.
|
24
|
+
|
25
|
+
= 3.0.7 [2011-09-21]
|
26
|
+
* Fix ActiveRecord before_type_cast extension for non-dirty attributes.
|
27
|
+
* Don't override AR before_type_cast for >= 3.1.0 which now has it's own implementation for date/time attributes.
|
28
|
+
* Fix DateTimeSelect extension to convert params to integers (#45)
|
29
|
+
* Add #change method to DateTimeSelect extension (@trusche, #45)
|
30
|
+
* Cleanup Mongoid shim.
|
31
|
+
|
32
|
+
= 3.0.6 [2011-05-09]
|
33
|
+
* Fix for AR type conversion for date columns when using plugin parser.
|
34
|
+
* Add timeliness_type_cast_code for ORM specific type casting after parsing.
|
35
|
+
|
36
|
+
= 3.0.5 [2011-01-29]
|
37
|
+
* Fix for Conversion#parse when given nil value (closes issue #34)
|
38
|
+
|
39
|
+
= 3.0.4 [2011-01-22]
|
40
|
+
* Fix :between option which was being ignored (ebeigarts)
|
41
|
+
* Use class_attribute to remove deprecated class_inheritable_accessor
|
42
|
+
* Namespace copied validator class to ActiveModel::Validations::Timeliness for :timeliness option
|
43
|
+
|
44
|
+
= 3.0.3 [2010-12-11]
|
45
|
+
* Fix validation of values which don't respond to to_date or to_time (renatoelias)
|
46
|
+
|
47
|
+
= 3.0.2 [2010-12-04]
|
48
|
+
* Fix AR multiparameter extension for Date columns
|
49
|
+
* Update to Timeliness 0.3.2 for zone abbreviation and offset support
|
50
|
+
|
51
|
+
= 3.0.1 [2010-11-02]
|
52
|
+
* Generate timeliness write methods in an included module to allow overriding in model class (josevalim)
|
53
|
+
|
54
|
+
= 3.0.0 [2010-10-18]
|
55
|
+
* Rails 3 and ActiveModel compatibility
|
56
|
+
* Uses ActiveModel::EachValidator as validator base class.
|
57
|
+
* Configuration settings stored in ValidatesTimeliness module only. ValidatesTimeliness.setup block to configure.
|
58
|
+
* Parser extracted to the Timeliness gem http://github.com/adzap/timeliness
|
59
|
+
* Parser is disabled by default. See initializer for enabling it.
|
60
|
+
* Removed RSpec matcher. Encouraged poor specs by copy-pasting from spec to model, or worse, the other way round.
|
61
|
+
* Method override for parsing and before type cast values is on validated attributes only. Old version handled all date/datetime columns, validates or not. Too intrusive.
|
62
|
+
* Add validation helpers to classes using extend_orms config setting. e.g. conf.extend_orms = [ :active_record ]
|
63
|
+
* Changed :between option so it is split into :on_or_after and :on_or_before option values. The error message for either failing check will be used instead of a between error message.
|
64
|
+
* Provides :timeliness option key for validates class method. Be sure to pass :type option as well e.g. :type => :date.
|
65
|
+
* Allows validation methods to be called on record instances as per ActiveModel API.
|
66
|
+
* Performs parsing (optional) and raw value caching (before_type_cast) on validated attributes only. It used to be all date, time and datetime attributes.
|
67
|
+
|
68
|
+
= 2.3.1 [2010-03-19]
|
69
|
+
* Fixed bug where custom attribute writer method for date/times were being overriden
|
70
|
+
|
71
|
+
= 2.3.0 [2010-02-04]
|
72
|
+
* Backwards incompatible change to :equal_to option. Fixed error message clash with :equal_to option which exists in Rails already. Option is now :is_at.
|
73
|
+
* Fixed I18n support so it returns missing translation message instead of error
|
74
|
+
* Fixed attribute method bug. Write method was bypassed when method was first generated and used Rails default parser.
|
75
|
+
* Fixed date/time selects when using enable_datetime_select_extension! when some values empty
|
76
|
+
* Fixed ISO8601 datetime format which is now split into two formats
|
77
|
+
* Changed I18n error value format to fallback to global default if missing in locale
|
78
|
+
* Refactored date/time select invalid value extension to use param values. Functionality will be extracted from plugin for v3.
|
79
|
+
|
80
|
+
= 2.2.2 [2009-09-19]
|
81
|
+
* Fixed dummy_time using make_time to respect timezone. Fixes 1.9.1 bug.
|
82
|
+
|
83
|
+
= 2.2.1 [2009-09-12]
|
84
|
+
* Fixed dummy date part for time types in Validator.type_cast_value
|
85
|
+
* No more core extensions! Removed dummy_time methods.
|
86
|
+
|
87
|
+
= 2.2.0 [2009-09-12]
|
88
|
+
* Ruby 1.9 support!
|
89
|
+
* Customise dummy date values for time types. See DUMMY DATE FOR TIME TYPES.
|
90
|
+
* Fixed matcher conflict with Shoulda. Load plugin matcher manually now see matcher section in README
|
91
|
+
* Fixed :ignore_usec when used with :with_time or :with_date
|
92
|
+
* Some clean up and refactoring
|
93
|
+
|
94
|
+
= 2.1.0 [2009-06-20]
|
95
|
+
* Added ambiguous year threshold setting in Formats class to customize the threshold for 2 digit years (See README)
|
96
|
+
* Fixed interpolation values in custom error message for Rails 2.2+
|
97
|
+
* Fixed custom I18n local override of en locale
|
98
|
+
* Dramatically simplified ActiveRecord monkey patching and hackery
|
99
|
+
|
100
|
+
= 2.0.0 [2009-04-12]
|
101
|
+
* Error value formats are now specified in the i18n locale file instead of updating plugin hash. See OTHER CUSTOMISATION section in README.
|
102
|
+
* Date/time select helper extension is disabled by default. To enable see DISPLAY INVALID VALUES IN DATE HELPERS section in README to enable.
|
103
|
+
* Added :format option to limit validation to a single format if desired
|
104
|
+
* Matcher now supports :equal_to option
|
105
|
+
* Formats.parse can take :include_offset option to include offset value from string in seconds, if string contains an offset. Offset not used in rest of plugin yet.
|
106
|
+
* Refactored to remove as much plugin code from ActiveRecord as possible.
|
107
|
+
|
108
|
+
= 1.1.7 [2009-03-26]
|
109
|
+
* Minor change to multiparameter attributes which I had not properly implemented for chaining
|
110
|
+
|
111
|
+
= 1.1.6 [2009-03-19]
|
112
|
+
* Rail 2.3 support
|
113
|
+
* Added :with_date and :with_time options. They allow an attribute to be combined with another attribute or value to make a datetime value for validation against the temporal restrictions
|
114
|
+
* Added :equal_to option
|
115
|
+
* Option key validation
|
116
|
+
* Better behaviour with other plugins using alias_method_chain on read_attribute and define_attribute_methods
|
117
|
+
* Added option to enable datetime_select extension for future use to optionally enable. Enabled by default until version 2.
|
118
|
+
* Added :ignore_usec option for datetime restrictions to be compared without microsecond
|
119
|
+
* some refactoring
|
120
|
+
|
121
|
+
= 1.1.5 [2009-01-21]
|
122
|
+
* Fixed regex for 'yy' format token which wasn't greedy enough for date formats ending with year when a datetime string parsed as date with a 4 digit year
|
123
|
+
|
124
|
+
= 1.1.4 [2009-01-13]
|
125
|
+
* Make months names respect i18n in Formats
|
126
|
+
|
127
|
+
= 1.1.3 [2009-01-13]
|
128
|
+
* Fixed bug where time and date attributes still being parsed on read using Rails default parser [reported by Brad (pvjq)]
|
129
|
+
|
130
|
+
= 1.1.2 [2009-01-12]
|
131
|
+
* Fixed bugs
|
132
|
+
* matcher failing for custom error message without interpolation keys using I18n
|
133
|
+
* validator custom error messages not being extracted
|
134
|
+
|
135
|
+
= 1.1.1 [2009-01-03]
|
136
|
+
* Fixed bug in matcher for options local variable
|
137
|
+
|
138
|
+
= 1.1.0 [2009-01-01]
|
139
|
+
* Added between option
|
140
|
+
|
141
|
+
= 1.0.0 [2008-12-06]
|
142
|
+
* Gemified!
|
143
|
+
* Refactor of plugin into a Data Mapper style validator class which makes for a cleaner implementation and possible future Merb\Data Mapper support
|
144
|
+
* Added Rails 2.2 i18n support. Plugin error messages can specified in locale files. See README.
|
145
|
+
* ignore_datetime_restriction_errors setting has been moved from AR to ValidatesTimeliness::Validator.ignore_restriction_errors
|
146
|
+
* date_time_error_value_formats setting has been moved from AR to ValidatesTimeliness::Validator.error_value_formats
|
147
|
+
* Namespaced modules and specs
|
148
|
+
* Clean up of specs
|
149
|
+
* fixed a few bugs
|
150
|
+
* accessor methods not generating properly due method name stored as symbol in generated_attributes which fails on lookup
|
151
|
+
* force value assigned to time/datetime attributes to time objects
|
152
|
+
|
153
|
+
= 0.1.0 [2008-12-06]
|
154
|
+
* Tagged plugin as version 0.1.0
|
155
|
+
|
156
|
+
= 2008-11-13
|
157
|
+
* allow uppercase meridian to be valid [reported by Alex (http://alex.digns.com/)]
|
158
|
+
|
159
|
+
= 2008-10-28
|
160
|
+
* fixed bug when dirty attributes not reflecting change when attribute changed from time value to nil [reported by Brad (pvjq)]
|
161
|
+
* fixes for Rails 2.2 compatibility. Will refactor in to Rails version specific branches in the future.
|
162
|
+
|
163
|
+
= 2008-09-24
|
164
|
+
* refactored attribute write method definitions
|
165
|
+
|
166
|
+
= 2008-08-25
|
167
|
+
* fixed bug for non-timezone write method not updating changed attributes hash [reported by Sylvestre Mergulhão]
|
168
|
+
|
169
|
+
= 2008-08-22
|
170
|
+
* fixed bug with attribute cache not clearing on write for date and time columns [reported by Sylvestre Mergulhão]
|
171
|
+
* parse method returns Date object for date column assigned string as per normal Rails behaviour
|
172
|
+
* parse method returns same object type when assigned Date or Time object as per normal Rails behaviour
|
173
|
+
|
174
|
+
= 2008-08-07
|
175
|
+
* modified matcher option value parsing to allow same value types as validation method
|
176
|
+
* fixed matcher message
|
177
|
+
|
178
|
+
= 2008-08-02
|
179
|
+
* refactored validation
|
180
|
+
* refactored matcher
|
181
|
+
|
182
|
+
= 2008-07-30
|
183
|
+
* removed setting values to nil when validation fails to preserve before_type_cast value
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008-2010 Adam Meehan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,323 @@
|
|
1
|
+
# ValidatesTimeliness
|
2
|
+
[![Gem Version][gem-badge]][gem]
|
3
|
+
[![Build status][build-badge]][build]
|
4
|
+
[![Coverage Status][coverage-badge]][coverage]
|
5
|
+
|
6
|
+
## Description
|
7
|
+
|
8
|
+
Complete validation of dates, times and datetimes for Rails 3.x and
|
9
|
+
ActiveModel.
|
10
|
+
|
11
|
+
This is a Rails 4-compatible fork of the
|
12
|
+
[original validates_timeliness gem][original] by [Adam Meehan][adzap].
|
13
|
+
|
14
|
+
This is a Rails 4.2-compatible fork of the
|
15
|
+
[original validates_timeliness gem][original] by [John Carney][johncarney].
|
16
|
+
|
17
|
+
## Features
|
18
|
+
|
19
|
+
* Adds validation for dates, times and datetimes to ActiveModel
|
20
|
+
* Handles timezones and type casting of values for you
|
21
|
+
* Only Rails date/time validation plugin offering complete validation (See
|
22
|
+
ORM/ODM support)
|
23
|
+
* Uses extensible date/time parser (Using
|
24
|
+
[timeliness gem][timeliness]. See Plugin Parser)
|
25
|
+
* Adds extensions to fix Rails date/time select issues (See Extensions)
|
26
|
+
* Supports I18n for the error messages
|
27
|
+
* Supports all the Rubies (that any sane person would be using in production).
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
# in Gemfile
|
32
|
+
gem 'jc-validates_timeliness'
|
33
|
+
|
34
|
+
# Run bundler
|
35
|
+
$ bundle install
|
36
|
+
|
37
|
+
Then run
|
38
|
+
|
39
|
+
$ rails generate validates_timeliness:install
|
40
|
+
|
41
|
+
This creates configuration initializer and locale files. In the initializer,
|
42
|
+
there are a number of config options to customize the plugin.
|
43
|
+
|
44
|
+
NOTE: You may wish to enable the plugin parser and the extensions to start.
|
45
|
+
Please read those sections first.
|
46
|
+
|
47
|
+
## Examples
|
48
|
+
|
49
|
+
validates_datetime :occurred_at
|
50
|
+
|
51
|
+
validates_date :date_of_birth, :before => lambda { 18.years.ago },
|
52
|
+
:before_message => "must be at least 18 years old"
|
53
|
+
|
54
|
+
validates_datetime :finish_time, :after => :start_time # Method symbol
|
55
|
+
|
56
|
+
validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
|
57
|
+
|
58
|
+
validates_time :booked_at, :between => ['9:00am', '5:00pm'] # On or after 9:00AM and on or before 5:00PM
|
59
|
+
validates_time :booked_at, :between => '9:00am'..'5:00pm' # The same as previous example
|
60
|
+
validates_time :booked_at, :between => '9:00am'...'5:00pm' # On or after 9:00AM and strictly before 5:00PM
|
61
|
+
|
62
|
+
validates_time :breakfast_time, :on_or_after => '6:00am',
|
63
|
+
:on_or_after_message => 'must be after opening time',
|
64
|
+
:before => :lunchtime,
|
65
|
+
:before_message => 'must be before lunch time'
|
66
|
+
|
67
|
+
## Usage
|
68
|
+
|
69
|
+
To validate a model with a date, time or datetime attribute you just use the
|
70
|
+
validation method
|
71
|
+
|
72
|
+
class Person < ActiveRecord::Base
|
73
|
+
validates_date :date_of_birth, :on_or_before => lambda { Date.current }
|
74
|
+
# or
|
75
|
+
validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
|
76
|
+
end
|
77
|
+
|
78
|
+
or even on a specific record, per ActiveModel API.
|
79
|
+
|
80
|
+
@person.validates_date :date_of_birth, :on_or_before => lambda { Date.current }
|
81
|
+
|
82
|
+
The list of validation methods available are as follows:
|
83
|
+
|
84
|
+
validates_date - validate value as date
|
85
|
+
validates_time - validate value as time only i.e. '12:20pm'
|
86
|
+
validates_datetime - validate value as a full date and time
|
87
|
+
validates - use the :timeliness key and set the type in the hash.
|
88
|
+
|
89
|
+
The validation methods take the usual options plus some specific ones to
|
90
|
+
restrict the valid range of dates or times allowed
|
91
|
+
|
92
|
+
Temporal options (or restrictions):
|
93
|
+
|
94
|
+
:is_at - Attribute must be equal to value to be valid
|
95
|
+
:before - Attribute must be before this value to be valid
|
96
|
+
:on_or_before - Attribute must be equal to or before this value to be valid
|
97
|
+
:after - Attribute must be after this value to be valid
|
98
|
+
:on_or_after - Attribute must be equal to or after this value to be valid
|
99
|
+
:between - Attribute must be between the values to be valid. Range or Array of 2 values.
|
100
|
+
|
101
|
+
Regular validation options:
|
102
|
+
|
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
|
+
|
111
|
+
:ignore_usec - Ignores microsecond value on datetime restrictions
|
112
|
+
:format - Limit validation to a single format for special cases. Requires plugin parser.
|
113
|
+
|
114
|
+
The temporal restrictions can take 4 different value types:
|
115
|
+
|
116
|
+
* Date, Time, or DateTime object value
|
117
|
+
* Proc or lambda object which may take an optional parameter, being the record
|
118
|
+
object
|
119
|
+
* A symbol matching a method name in the model
|
120
|
+
* String value
|
121
|
+
|
122
|
+
When an attribute value is compared to temporal restrictions, they are
|
123
|
+
compared as the same type as the validation method type. So using
|
124
|
+
validates_date means all values are compared as dates.
|
125
|
+
|
126
|
+
## Configuration
|
127
|
+
|
128
|
+
### ORM/ODM Support
|
129
|
+
|
130
|
+
The plugin adds date/time validation to ActiveModel for any ORM/ODM that
|
131
|
+
supports the ActiveModel validations component. However, there is an issue
|
132
|
+
with most ORM/ODMs which does not allow 100% date/time validation by default.
|
133
|
+
Specifically, when you assign an invalid date/time value to an attribute, most
|
134
|
+
ORM/ODMs will only store a nil value for the attribute. This causes an issue
|
135
|
+
for date/time validation, since we need to know that a value was assigned but
|
136
|
+
was invalid. To fix this, we need to cache the original invalid value to know
|
137
|
+
that the attribute is not just nil.
|
138
|
+
|
139
|
+
Each ORM/ODM requires a specific shim to fix it. The plugin includes a shim
|
140
|
+
for ActiveRecord and Mongoid. You can activate them like so
|
141
|
+
|
142
|
+
ValidatesTimeliness.setup do |config|
|
143
|
+
|
144
|
+
# Extend ORM/ODMs for full support (:active_record, :mongoid).
|
145
|
+
config.extend_orms = [ :mongoid ]
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
By default the plugin extends ActiveRecord if loaded. If you wish to extend
|
150
|
+
another ORM then look at the [wiki page][orm-support] for more information.
|
151
|
+
|
152
|
+
It is not required that you use a shim, but you will not catch errors when the
|
153
|
+
attribute value is invalid and evaluated to nil.
|
154
|
+
|
155
|
+
### Error Messages
|
156
|
+
|
157
|
+
Using the I18n system to define new defaults:
|
158
|
+
|
159
|
+
en:
|
160
|
+
errors:
|
161
|
+
messages:
|
162
|
+
invalid_date: "is not a valid date"
|
163
|
+
invalid_time: "is not a valid time"
|
164
|
+
invalid_datetime: "is not a valid datetime"
|
165
|
+
is_at: "must be at %{restriction}"
|
166
|
+
before: "must be before %{restriction}"
|
167
|
+
on_or_before: "must be on or before %{restriction}"
|
168
|
+
after: "must be after %{restriction}"
|
169
|
+
on_or_after: "must be on or after %{restriction}"
|
170
|
+
|
171
|
+
The `%{restriction}` signifies where the interpolation value for the
|
172
|
+
restriction will be inserted.
|
173
|
+
|
174
|
+
You can also use validation options for custom error messages. The following
|
175
|
+
option keys are available:
|
176
|
+
|
177
|
+
:invalid_date_message
|
178
|
+
:invalid_time_message
|
179
|
+
:invalid_datetime_message
|
180
|
+
:is_at_message
|
181
|
+
:before_message
|
182
|
+
:on_or_before_message
|
183
|
+
:after_message
|
184
|
+
:on_or_after_message
|
185
|
+
|
186
|
+
Note: There is no :between_message option. The between error message should be
|
187
|
+
defined using the :on_or_after and :on_or_before (:before in case when
|
188
|
+
:between argument is a Range with excluded high value, see Examples) messages.
|
189
|
+
|
190
|
+
It is highly recommended you use the I18n system for error messages.
|
191
|
+
|
192
|
+
### Plugin Parser
|
193
|
+
|
194
|
+
The plugin uses the [timeliness gem][timeliness] as a fast, configurable and
|
195
|
+
extensible date and time parser. You can add or remove valid formats for
|
196
|
+
dates, times, and datetimes. It is also more strict than the Ruby parser,
|
197
|
+
which means it won't accept day of the month if it's not a valid number for
|
198
|
+
the month.
|
199
|
+
|
200
|
+
By default the parser is disabled. To enable it:
|
201
|
+
|
202
|
+
# in the setup block
|
203
|
+
config.use_plugin_parser = true
|
204
|
+
|
205
|
+
Enabling the parser will mean that strings assigned to attributes validated
|
206
|
+
with the plugin will be parsed using the gem. See the [wiki][plugin-parser]
|
207
|
+
for more details about the parser configuration.
|
208
|
+
|
209
|
+
|
210
|
+
### Restriction Shorthand
|
211
|
+
|
212
|
+
It is common to restrict an attribute to being on or before the current time
|
213
|
+
or current day. To specify this you need to use a lambda as an option value
|
214
|
+
e.g. `lambda { Time.current }. This can be tedious noise amongst your
|
215
|
+
validations for something so common. To combat this the plugin allows you to
|
216
|
+
use shorthand symbols for often used relative times or dates.
|
217
|
+
|
218
|
+
Just provide the symbol as the option value like so:
|
219
|
+
|
220
|
+
validates_date :birth_date, :on_or_before => :today
|
221
|
+
|
222
|
+
The :today symbol is evaluated as `lambda { Date.today }`. The :now and :today
|
223
|
+
symbols are pre-configured. Configure your own like so:
|
224
|
+
|
225
|
+
# in the setup block
|
226
|
+
config.restriction_shorthand_symbols.update(
|
227
|
+
:yesterday => lambda { 1.day.ago }
|
228
|
+
)
|
229
|
+
|
230
|
+
### Default Timezone
|
231
|
+
|
232
|
+
The plugin needs to know the default timezone you are using when parsing or
|
233
|
+
type casting values. If you are using ActiveRecord then the default is
|
234
|
+
automatically set to the same default zone as ActiveRecord. If you are using
|
235
|
+
another ORM you may need to change this setting.
|
236
|
+
|
237
|
+
# in the setup block
|
238
|
+
config.default_timezone = :utc
|
239
|
+
|
240
|
+
By default it will be UTC if ActiveRecord is not loaded.
|
241
|
+
|
242
|
+
### Dummy Date For Time Types
|
243
|
+
|
244
|
+
Given that Ruby has no support for a time-only type, all time type columns are
|
245
|
+
evaluated as a regular Time class objects with a dummy date value set. Rails
|
246
|
+
defines the dummy date as 2000-01-01. So a time of '12:30' is evaluated as a
|
247
|
+
Time value of '2000-01-01 12:30'. If you need to customize this for some
|
248
|
+
reason you can do so as follows
|
249
|
+
|
250
|
+
# in the setup block
|
251
|
+
config.dummy_date_for_time_type = [2009, 1, 1]
|
252
|
+
|
253
|
+
The value should be an array of 3 values being year, month and day in that
|
254
|
+
order.
|
255
|
+
|
256
|
+
### Temporal Restriction Errors
|
257
|
+
|
258
|
+
When using the validation temporal restrictions there are times when the
|
259
|
+
restriction option value itself may be invalid. This will add an error to the
|
260
|
+
model such as 'Error occurred validating birth_date for :before restriction'.
|
261
|
+
These can be annoying in development or production as you most likely just
|
262
|
+
want to skip the option if no valid value was returned. By default these
|
263
|
+
errors are displayed in Rails test mode.
|
264
|
+
|
265
|
+
To turn them on/off:
|
266
|
+
|
267
|
+
# in the setup block
|
268
|
+
config.ignore_restriction_errors = true
|
269
|
+
|
270
|
+
## Extensions
|
271
|
+
|
272
|
+
### Strict Parsing for Select Helpers
|
273
|
+
|
274
|
+
When using date/time select helpers, the component values are handled by
|
275
|
+
ActiveRecord using the Time class to instantiate them into a time value. This
|
276
|
+
means that some invalid dates, such as 31st June, are shifted forward and
|
277
|
+
treated as valid. To handle these cases in a strict way, you can enable the
|
278
|
+
plugin extension to treat them as invalid dates.
|
279
|
+
|
280
|
+
To activate it, uncomment this line in the initializer:
|
281
|
+
|
282
|
+
# in the setup block
|
283
|
+
config.enable_multiparameter_extension!
|
284
|
+
|
285
|
+
### Display Invalid Values in Select Helpers
|
286
|
+
|
287
|
+
The plugin offers an extension for ActionView to allowing invalid date and
|
288
|
+
time values to be redisplayed to the user as feedback, instead of a blank
|
289
|
+
field which happens by default in Rails. Though the date helpers make this a
|
290
|
+
pretty rare occurrence, given the select dropdowns for each date/time
|
291
|
+
component, but it may be something of interest.
|
292
|
+
|
293
|
+
To activate it, uncomment this line in the initializer:
|
294
|
+
|
295
|
+
# in the setup block
|
296
|
+
config.enable_date_time_select_extension!
|
297
|
+
|
298
|
+
## Contributors
|
299
|
+
|
300
|
+
To see the generous people who have contributed code, take a look at the
|
301
|
+
[contributors list][contributors].
|
302
|
+
|
303
|
+
## Maintainers
|
304
|
+
|
305
|
+
* [John Carney][jc]
|
306
|
+
|
307
|
+
## License
|
308
|
+
|
309
|
+
Copyright (c) 2008 Adam Meehan, released under the MIT license
|
310
|
+
|
311
|
+
[jc]: http://github.com/johncarney
|
312
|
+
[adzap]: http://github.com/adzap
|
313
|
+
[timeliness]: http://github.com/adzap/timeliness
|
314
|
+
[orm-support]: http://github.com/adzap/validates_timeliness/wiki/ORM-Support
|
315
|
+
[plugin-parser]: http://github.com/adzap/validates_timeliness/wiki/Plugin-Parser
|
316
|
+
[original]: http://github.com/adzap/validates_timeliness
|
317
|
+
[contributors]: http://github.com/johncarney/validates_timeliness/contributors
|
318
|
+
[gem-badge]: https://badge.fury.io/rb/jc-validates_timeliness.svg
|
319
|
+
[gem]: http://badge.fury.io/rb/jc-validates_timeliness
|
320
|
+
[build-badge]: https://travis-ci.org/johncarney/validates_timeliness.svg?branch=master
|
321
|
+
[build]: https://travis-ci.org/johncarney/validates_timeliness
|
322
|
+
[coverage-badge]: https://coveralls.io/repos/johncarney/validates_timeliness/badge.png?branch=master
|
323
|
+
[coverage]: https://coveralls.io/r/johncarney/validates_timeliness?branch=master
|