validates_timeliness 2.3.2 → 3.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +12 -4
- data/LICENSE +1 -1
- data/README.rdoc +138 -280
- data/Rakefile +30 -16
- data/lib/generators/validates_timeliness/install_generator.rb +17 -0
- data/lib/generators/validates_timeliness/templates/en.yml +16 -0
- data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +22 -0
- data/lib/validates_timeliness.rb +46 -52
- data/lib/validates_timeliness/attribute_methods.rb +51 -0
- data/lib/validates_timeliness/conversion.rb +69 -0
- data/lib/validates_timeliness/extensions.rb +14 -0
- data/lib/validates_timeliness/extensions/date_time_select.rb +45 -0
- data/lib/validates_timeliness/extensions/multiparameter_handler.rb +31 -0
- data/lib/validates_timeliness/helper_methods.rb +41 -0
- data/lib/validates_timeliness/orms/active_record.rb +14 -0
- data/lib/validates_timeliness/parser.rb +389 -17
- data/lib/validates_timeliness/validator.rb +37 -200
- data/lib/validates_timeliness/version.rb +1 -1
- data/spec/model_helpers.rb +27 -0
- data/spec/spec_helper.rb +74 -43
- data/spec/test_model.rb +56 -0
- data/spec/validates_timeliness/attribute_methods_spec.rb +36 -0
- data/spec/validates_timeliness/conversion_spec.rb +204 -0
- data/spec/validates_timeliness/extensions/date_time_select_spec.rb +178 -0
- data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +21 -0
- data/spec/validates_timeliness/helper_methods_spec.rb +36 -0
- data/spec/{formats_spec.rb → validates_timeliness/parser_spec.rb} +105 -71
- data/spec/validates_timeliness/validator/after_spec.rb +59 -0
- data/spec/validates_timeliness/validator/before_spec.rb +59 -0
- data/spec/validates_timeliness/validator/is_at_spec.rb +63 -0
- data/spec/validates_timeliness/validator/on_or_after_spec.rb +59 -0
- data/spec/validates_timeliness/validator/on_or_before_spec.rb +59 -0
- data/spec/validates_timeliness/validator_spec.rb +172 -0
- data/validates_timeliness.gemspec +30 -0
- metadata +42 -40
- data/TODO +0 -8
- data/lib/validates_timeliness/action_view/instance_tag.rb +0 -52
- data/lib/validates_timeliness/active_record/attribute_methods.rb +0 -77
- data/lib/validates_timeliness/active_record/multiparameter_attributes.rb +0 -69
- data/lib/validates_timeliness/formats.rb +0 -368
- data/lib/validates_timeliness/locale/en.new.yml +0 -18
- data/lib/validates_timeliness/locale/en.old.yml +0 -18
- data/lib/validates_timeliness/matcher.rb +0 -1
- data/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb +0 -162
- data/lib/validates_timeliness/validation_methods.rb +0 -46
- data/spec/action_view/instance_tag_spec.rb +0 -194
- data/spec/active_record/attribute_methods_spec.rb +0 -157
- data/spec/active_record/multiparameter_attributes_spec.rb +0 -118
- data/spec/ginger_scenarios.rb +0 -19
- data/spec/parser_spec.rb +0 -65
- data/spec/resources/application.rb +0 -2
- data/spec/resources/person.rb +0 -3
- data/spec/resources/schema.rb +0 -10
- data/spec/resources/sqlite_patch.rb +0 -19
- data/spec/spec/rails/matchers/validate_timeliness_spec.rb +0 -245
- data/spec/time_travel/MIT-LICENSE +0 -20
- data/spec/time_travel/time_extensions.rb +0 -33
- data/spec/time_travel/time_travel.rb +0 -12
- data/spec/validator_spec.rb +0 -723
data/CHANGELOG
CHANGED
@@ -1,7 +1,15 @@
|
|
1
|
-
=
|
2
|
-
-
|
3
|
-
-
|
4
|
-
-
|
1
|
+
= 3.0.0.beta
|
2
|
+
- Rails 3 and ActiveModel compatibility
|
3
|
+
- Uses ActiveModel::EachValidator as validator base class.
|
4
|
+
- Configuration settings stored in ValidatesTimeliness module only. ValidatesTimeliness.setup block to configure.
|
5
|
+
- Plugin parser is off by default.
|
6
|
+
- 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.
|
7
|
+
- Add validation helpers to classes using extend_orms config setting. e.g. conf.extend_orms = [ :active_record ]
|
8
|
+
- 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.
|
9
|
+
- Provides :timeliness option key for validates class method. Be sure to pass :type option as well e.g. :type => :date.
|
10
|
+
- Allows validation methods to be called on record instances as per ActiveModel API
|
11
|
+
- Performs parsing (optional) and raw value caching (before_type_cast) on validated attributes only. It used to be all date, time and datetime attributes.
|
12
|
+
- Fix meridian bug where time parsed with hour of 0 or greater than 12 was accepted.
|
5
13
|
|
6
14
|
= 2.3.1 [2010-03-19]
|
7
15
|
- Fixed bug where custom attribute writer method for date/times were being overriden
|
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,62 +1,84 @@
|
|
1
|
-
=
|
1
|
+
= ValidatesTimeliness
|
2
2
|
|
3
3
|
* Source: http://github.com/adzap/validates_timeliness
|
4
4
|
* Bugs: http://github.com/adzap/validates_timeliness/issues
|
5
5
|
|
6
|
-
==
|
6
|
+
== Description
|
7
7
|
|
8
|
-
Validate dates, times and datetimes for Rails
|
9
|
-
automatic timezone handling. Allows you to add custom formats or remove defaults
|
10
|
-
easily. This allows you to control what you think should be a valid date or
|
11
|
-
time string.
|
8
|
+
Validate dates, times and datetimes for Rails 3.x and ActiveModel.
|
12
9
|
|
10
|
+
If you a looking for the old version for Rails 2.x go here[http://github.com/adzap/validates_timeliness/tree/v2.3].
|
13
11
|
|
14
|
-
== FEATURES:
|
15
12
|
|
16
|
-
|
13
|
+
== Features
|
17
14
|
|
18
|
-
*
|
15
|
+
* Adds ActiveModel validation for dates, times and datetimes
|
19
16
|
|
20
|
-
*
|
17
|
+
* Should work with any ORM using ActiveModel
|
21
18
|
|
22
|
-
*
|
23
|
-
_before_type_cast modifier, which was lost in Rails 2.1.
|
19
|
+
* Supports timezone handling
|
24
20
|
|
25
|
-
* Supports
|
21
|
+
* Supports I18n for the error messages
|
26
22
|
|
27
|
-
*
|
23
|
+
* Adds before_type_cast method on validated attributes, if ORM supports it.
|
28
24
|
|
29
|
-
* Rspec matcher for testing model validation of dates and times
|
30
25
|
|
26
|
+
== Installation
|
31
27
|
|
32
|
-
|
28
|
+
As plugin (from master)
|
33
29
|
|
34
|
-
|
30
|
+
rails plugin install git://github.com/adzap/validates_timeliness.git
|
35
31
|
|
36
|
-
|
32
|
+
As gem (not working as yet)
|
37
33
|
|
38
|
-
# in
|
34
|
+
# in Gemfile
|
35
|
+
gem 'validates_timeliness'
|
39
36
|
|
40
|
-
|
37
|
+
# Run bundler
|
38
|
+
$ bundle install
|
41
39
|
|
42
|
-
|
40
|
+
Then run
|
41
|
+
|
42
|
+
$ rails generate validates_timeliness:install
|
43
|
+
|
44
|
+
This creates configuration initializer and locale files. In the initializer, you there are a number of config options to customize the plugin.
|
45
|
+
|
46
|
+
ValidatesTimeliness.setup do |config|
|
47
|
+
|
48
|
+
# Add plugin to supported ORMs (only :active_record for now)
|
49
|
+
# config.extend_orms = [ :active_record ]
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
By default the plugin extends ActiveRecord if present. Currently ActiveRecord is the only ORM included for extension. If you wish to extend
|
54
|
+
another ORM then look at the {wiki page}[http://github.com/adzap/validates_timeliness/wiki/ORM-Support] for more information.
|
55
|
+
|
56
|
+
To extend other ORMs is pretty straight forward. It's matter of hooking into a couple of methods, being the attribute method generation and
|
57
|
+
timezone handling of validated attributes. However, the plugin must support the ActiveModel validations system. If you extend an ORM
|
58
|
+
successfully, please send me a pull request to add the shim to the plugin or let me know where to find it.
|
43
59
|
|
44
|
-
./script/plugin install git://github.com/adzap/validates_timeliness.git -r v2.3
|
45
60
|
|
46
|
-
==
|
61
|
+
== Usage:
|
47
62
|
|
48
63
|
To validate a model with a date, time or datetime attribute you just use the
|
49
64
|
validation method
|
50
65
|
|
51
66
|
class Person < ActiveRecord::Base
|
52
|
-
validates_date :date_of_birth
|
53
|
-
|
67
|
+
validates_date :date_of_birth, :on_or_before => lambda { Date.today }
|
68
|
+
# or
|
69
|
+
validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.today }, :type => date}
|
54
70
|
end
|
55
71
|
|
72
|
+
# or even on a specific record, per ActiveModel API.
|
73
|
+
|
74
|
+
@person.validates_date :date_of_birth, :on_or_before => lambda { Date.today }
|
75
|
+
|
76
|
+
|
56
77
|
The list of validation methods available are as follows:
|
57
78
|
validates_date - validate value as date
|
58
79
|
validates_time - validate value as time only i.e. '12:20pm'
|
59
80
|
validates_datetime - validate value as a full date and time
|
81
|
+
validates - use the :timeliness key and set the type in the hash.
|
60
82
|
|
61
83
|
The validation methods take the usual options plus some specific ones to restrict
|
62
84
|
the valid range of dates or times allowed
|
@@ -67,7 +89,8 @@ Temporal options (or restrictions):
|
|
67
89
|
:on_or_before - Attribute must be equal to or before this value to be valid
|
68
90
|
:after - Attribute must be after this value to be valid
|
69
91
|
:on_or_after - Attribute must be equal to or after this value to be valid
|
70
|
-
:between - Attribute must be between the values to be valid.
|
92
|
+
:between - Attribute must be between the values to be valid. Range or Array of 2 values.
|
93
|
+
Uses :on_or_after and :on_of_before for error messages on lower and upper bounds respectively.
|
71
94
|
|
72
95
|
Regular validation options:
|
73
96
|
:allow_nil - Allow a nil value to be valid
|
@@ -76,327 +99,162 @@ Regular validation options:
|
|
76
99
|
:unless - Execute validation when :unless evaluates false
|
77
100
|
|
78
101
|
Special options:
|
79
|
-
:with_time - Validate a date attribute value combined with a time value against any temporal restrictions
|
80
|
-
:with_date - Validate a time attribute value combined with a date value against any temporal restrictions
|
81
102
|
:ignore_usec - Ignores microsecond value on datetime restrictions
|
82
|
-
:format - Limit validation to a single format for special cases.
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
:invalid_time_message
|
87
|
-
:invalid_datetime_message
|
88
|
-
:is_at_message
|
89
|
-
:before_message
|
90
|
-
:on_or_before_message
|
91
|
-
:after_message
|
92
|
-
:on_or_after_message
|
93
|
-
:between_message
|
94
|
-
|
95
|
-
The temporal restrictions, with_date and with_time can take 4 different value types:
|
96
|
-
* String value
|
103
|
+
:format - Limit validation to a single format for special cases. Requires plugin parser.
|
104
|
+
|
105
|
+
The temporal restrictions can take 4 different value types:
|
106
|
+
|
97
107
|
* Date, Time, or DateTime object value
|
98
|
-
* Proc or lambda object which may take an optional parameter being the record object
|
99
|
-
* A symbol matching
|
108
|
+
* Proc or lambda object which may take an optional parameter, being the record object
|
109
|
+
* A symbol matching a method name in the model
|
110
|
+
* String value
|
100
111
|
|
101
112
|
When an attribute value is compared to temporal restrictions, they are compared as
|
102
113
|
the same type as the validation method type. So using validates_date means all
|
103
|
-
values are compared as dates.
|
104
|
-
options which effectively force the value to validated as a datetime against the
|
105
|
-
temporal options.
|
106
|
-
|
107
|
-
== EXAMPLES:
|
108
|
-
|
109
|
-
validates_date :date_of_birth :before => lambda { 18.years.ago },
|
110
|
-
:before_message => "must be at least 18 years old"
|
111
|
-
|
112
|
-
validates_time :breakfast_time, :on_or_after => '6:00am',
|
113
|
-
:on_or_after_message => 'must be after opening time',
|
114
|
-
:before => :second_breakfast_time,
|
115
|
-
:allow_nil => true
|
116
|
-
|
117
|
-
validates_datetime :appointment_date, :before => lambda { 1.week.from_now }
|
118
|
-
|
119
|
-
validates_date :entry_date, :with_time => '17:00', :on_or_before => :competition_closing
|
120
|
-
|
121
|
-
|
122
|
-
=== DATE/TIME FORMATS:
|
123
|
-
|
124
|
-
So what formats does the plugin allow. Well there are default formats which can
|
125
|
-
be added to easily using the plugins format rules. Also formats can be easily
|
126
|
-
removed without hacking the plugin at all.
|
127
|
-
|
128
|
-
Below are the default formats. If you think they are easy to read then you will
|
129
|
-
be happy to know that is exactly the format you can use to define your own if
|
130
|
-
you want. No complex regular expressions or duck punching (monkey patching) the
|
131
|
-
plugin is needed.
|
132
|
-
|
133
|
-
==== Time formats:
|
134
|
-
hh:nn:ss
|
135
|
-
hh-nn-ss
|
136
|
-
h:nn
|
137
|
-
h.nn
|
138
|
-
h nn
|
139
|
-
h-nn
|
140
|
-
h:nn_ampm
|
141
|
-
h.nn_ampm
|
142
|
-
h nn_ampm
|
143
|
-
h-nn_ampm
|
144
|
-
h_ampm
|
145
|
-
|
146
|
-
NOTE: Any time format without a meridian token (the 'ampm' token) is considered in 24 hour time.
|
147
|
-
|
148
|
-
==== Date formats:
|
149
|
-
yyyy/mm/dd
|
150
|
-
yyyy-mm-dd
|
151
|
-
yyyy.mm.dd
|
152
|
-
m/d/yy OR d/m/yy
|
153
|
-
m\d\yy OR d\m\yy
|
154
|
-
d-m-yy
|
155
|
-
d.m.yy
|
156
|
-
d mmm yy
|
157
|
-
|
158
|
-
NOTE: To use non-US date formats see US/EURO FORMATS section
|
159
|
-
|
160
|
-
==== Datetime formats:
|
161
|
-
m/d/yy h:nn:ss OR d/m/yy hh:nn:ss
|
162
|
-
m/d/yy h:nn OR d/m/yy h:nn
|
163
|
-
m/d/yy h:nn_ampm OR d/m/yy h:nn_ampm
|
164
|
-
yyyy-mm-dd hh:nn:ss
|
165
|
-
yyyy-mm-dd h:nn
|
166
|
-
ddd mmm d hh:nn:ss zo yyyy # Ruby time string
|
167
|
-
yyyy-mm-ddThh:nn:ssZ # ISO 8601 without zone offset
|
168
|
-
yyyy-mm-ddThh:nn:sszo # ISO 8601 with zone offset
|
169
|
-
|
170
|
-
NOTE: To use non-US date formats see US/EURO FORMATS section
|
114
|
+
values are compared as dates.
|
171
115
|
|
172
|
-
Here is what each format token means:
|
173
116
|
|
174
|
-
|
175
|
-
y = year
|
176
|
-
m = month
|
177
|
-
d = day
|
178
|
-
h = hour
|
179
|
-
n = minute
|
180
|
-
s = second
|
181
|
-
u = micro-seconds
|
182
|
-
ampm = meridian (am or pm) with or without dots (e.g. am, a.m, or a.m.)
|
183
|
-
_ = optional space
|
184
|
-
tz = Timezone abbreviation (e.g. UTC, GMT, PST, EST)
|
185
|
-
zo = Timezone offset (e.g. +10:00, -08:00, +1000)
|
117
|
+
== Configuration
|
186
118
|
|
187
|
-
|
188
|
-
x = 1 or 2 digits for unit (e.g. 'h' means an hour can be '9' or '09')
|
189
|
-
xx = 2 digits exactly for unit (e.g. 'hh' means an hour can only be '09')
|
119
|
+
=== Error Messages
|
190
120
|
|
191
|
-
|
192
|
-
yy = 2 or 4 digit year
|
193
|
-
yyyy = exactly 4 digit year
|
194
|
-
mmm = month long name (e.g. 'Jul' or 'July')
|
195
|
-
ddd = Day name of 3 to 9 letters (e.g. Wed or Wednesday)
|
196
|
-
u = microseconds matches 1 to 3 digits
|
121
|
+
Using the I18n system to define new defaults:
|
197
122
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
123
|
+
en:
|
124
|
+
errors:
|
125
|
+
messages:
|
126
|
+
invalid_date: "is not a valid date"
|
127
|
+
invalid_time: "is not a valid time"
|
128
|
+
invalid_datetime: "is not a valid datetime"
|
129
|
+
is_at: "must be at %{restriction}"
|
130
|
+
before: "must be before %{restriction}"
|
131
|
+
on_or_before: "must be on or before %{restriction}"
|
132
|
+
after: "must be after %{restriction}"
|
133
|
+
on_or_after: "must be on or after %{restriction}"
|
202
134
|
|
203
|
-
|
135
|
+
The %{restriction} signifies where the interpolation value for the restriction will be inserted.
|
204
136
|
|
205
|
-
|
137
|
+
You can also use validation options for custom error messages. The following option keys are available:
|
206
138
|
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
139
|
+
:invalid_date_message
|
140
|
+
:invalid_time_message
|
141
|
+
:invalid_datetime_message
|
142
|
+
:is_at_message
|
143
|
+
:before_message
|
144
|
+
:on_or_before_message
|
145
|
+
:after_message
|
146
|
+
:on_or_after_message
|
212
147
|
|
213
|
-
|
214
|
-
initializer or environment.rb
|
148
|
+
Note: There is no :between_message option. The between error message should be defined using the :on_or_before and :on_or_after messages.
|
215
149
|
|
216
|
-
|
150
|
+
It is highly recommended you use the I18n system for error messages.
|
217
151
|
|
218
|
-
Now '01/02/2000' will be parsed as 1st February 2000, instead of 2nd January 2000.
|
219
152
|
|
220
|
-
===
|
153
|
+
=== Plugin Parser
|
221
154
|
|
222
|
-
|
223
|
-
|
155
|
+
The plugin comes with a customisable date and time parser. You can add or remove valid formats
|
156
|
+
for dates, times, and datetimes. It is also more strict than the Ruby parser, which means it
|
157
|
+
won't accept day of the month if it's not a valid number for that month.
|
224
158
|
|
225
|
-
|
159
|
+
By default the parser is switched off. To switch it on:
|
226
160
|
|
227
|
-
|
161
|
+
# in the setup block
|
162
|
+
config.use_plugin_parser = true
|
228
163
|
|
229
|
-
|
230
|
-
Ahh, then add it yourself. Again stick this in an initializer file
|
164
|
+
See the wiki[http://github.com/adzap/validates_timeliness/wiki/Plugin-Parser] for all the details about the parser.
|
231
165
|
|
232
|
-
ValidatesTimeliness::Formats.add_formats(:time, "d o'clock")
|
233
166
|
|
234
|
-
|
167
|
+
=== Restriction Option Shorthand
|
235
168
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
datetime for an example of an embedded regular expression.
|
169
|
+
It is common to restrict an attribute to being on or before the current time or current day.
|
170
|
+
To specify this you need to use a lambda as an option value e.g. <tt>lambda { Time.now }</tt>.
|
171
|
+
This can be tedious noise amongst your validations for something so common. To combat this the
|
172
|
+
plugin allows you to use shorthand symbols for often used relative times or dates.
|
241
173
|
|
242
|
-
|
243
|
-
with an existing format, will mean your format is ignored. If you need to make
|
244
|
-
your new format higher precedence than an existing format, you can include the
|
245
|
-
before option like so
|
174
|
+
Just provide the symbol as the option value like so:
|
246
175
|
|
247
|
-
|
176
|
+
validates_date :birth_date, :on_or_before => :today
|
248
177
|
|
249
|
-
|
250
|
-
|
178
|
+
The :today symbol is evaluated as <tt>lambda { Date.today }</tt>. The :now and :today
|
179
|
+
symbols are pre-configured. Configure your own like so:
|
251
180
|
|
181
|
+
# in the setup block
|
182
|
+
config.restriction_shorthand_symbols.update(
|
183
|
+
:yesterday => lambda { 1.day.ago }
|
184
|
+
)
|
252
185
|
|
253
|
-
=== AMBIGUOUS YEAR THRESHOLD
|
254
186
|
|
255
|
-
|
256
|
-
in the last century at or above 30. You can customize this however
|
187
|
+
=== Default Timezone
|
257
188
|
|
258
|
-
|
189
|
+
The plugin needs to know the default timezone you are using when parsing or type casting values. If you are using
|
190
|
+
ActiveRecord then the default is automatically set to the same default zone as ActiveRecord. If you are using
|
191
|
+
another ORM you may need to change this setting.
|
259
192
|
|
260
|
-
|
193
|
+
# in the setup block
|
194
|
+
config.default_timezone = :utc
|
261
195
|
|
262
|
-
|
263
|
-
year of 20 is considered 1920
|
196
|
+
By default it will be UTC if ActiveRecord is not loaded.
|
264
197
|
|
265
198
|
|
266
|
-
===
|
199
|
+
=== Dummy Date For Time Types
|
267
200
|
|
268
201
|
Given that Ruby has no support for a time-only type, all time type columns are evaluated
|
269
202
|
as a regular Time class objects with a dummy date value set. Rails defines the dummy date as
|
270
203
|
2000-01-01. So a time of '12:30' is evaluated as a Time value of '2000-01-01 12:30'. If you
|
271
204
|
need to customize this for some reason you can do so as follows
|
272
205
|
|
273
|
-
|
206
|
+
# in the setup block
|
207
|
+
config.dummy_date_for_time_type = [2009, 1, 1]
|
274
208
|
|
275
209
|
The value should be an array of 3 values being year, month and day in that order.
|
276
210
|
|
277
211
|
|
278
|
-
===
|
212
|
+
=== Temporal Restriction Errors
|
279
213
|
|
280
214
|
When using the validation temporal restrictions there are times when the restriction
|
281
|
-
value itself may be invalid.
|
282
|
-
'
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
To turn them off:
|
215
|
+
option value itself may be invalid. This will add an error to the model such as
|
216
|
+
'Error occurred validating birth_date for :before restriction'. These can be annoying
|
217
|
+
in development or production as you most likely just want to skip the option if no
|
218
|
+
valid value was returned. By default these errors are displayed in Rails test mode.
|
287
219
|
|
288
|
-
|
220
|
+
To turn them on/off:
|
289
221
|
|
290
|
-
|
291
|
-
|
292
|
-
model methods and you only care when they return a value, then they should
|
293
|
-
return nil in all other situations. Restrictions are skipped if they are nil.
|
222
|
+
# in the setup block
|
223
|
+
config.ignore_restriction_errors = true
|
294
224
|
|
295
225
|
|
296
|
-
===
|
226
|
+
=== Display Invalid Values in Select Helpers
|
297
227
|
|
298
|
-
The plugin
|
228
|
+
The plugin offers an extension for ActionView to allowing invalid
|
299
229
|
date and time values to be redisplayed to the user as feedback, instead of
|
300
230
|
a blank field which happens by default in Rails. Though the date helpers make this a
|
301
|
-
pretty rare
|
231
|
+
pretty rare occurrence, given the select dropdowns for each date/time component, but
|
302
232
|
it may be something of interest.
|
303
233
|
|
304
234
|
To activate it, put this in an initializer:
|
305
235
|
|
306
|
-
|
307
|
-
|
308
|
-
This will be removed from v3 as it adds too little to maintain.
|
309
|
-
|
310
|
-
=== OTHER CUSTOMISATION:
|
311
|
-
|
312
|
-
The error messages for each temporal restrictions can also be globally overridden by
|
313
|
-
updating the default AR error messages like so
|
314
|
-
|
315
|
-
For Rails 2.0/2.1:
|
316
|
-
|
317
|
-
ActiveRecord::Errors.default_error_messages.update(
|
318
|
-
:invalid_date => "is not a valid date",
|
319
|
-
:invalid_time => "is not a valid time",
|
320
|
-
:invalid_datetime => "is not a valid datetime",
|
321
|
-
:is_at => "must be at %s",
|
322
|
-
:before => "must be before %s",
|
323
|
-
:on_or_before => "must be on or before %s",
|
324
|
-
:after => "must be after %s",
|
325
|
-
:on_or_after => "must be on or after %s",
|
326
|
-
:between => "must be between %s and %s"
|
327
|
-
)
|
328
|
-
|
329
|
-
Where %s is the interpolation value for the restriction.
|
330
|
-
|
331
|
-
Rails 2.2+ using the I18n system to define new defaults:
|
332
|
-
|
333
|
-
en:
|
334
|
-
activerecord:
|
335
|
-
errors:
|
336
|
-
messages:
|
337
|
-
invalid_date: "is not a valid date"
|
338
|
-
invalid_time: "is not a valid time"
|
339
|
-
invalid_datetime: "is not a valid datetime"
|
340
|
-
is_at: "must be at {{restriction}}"
|
341
|
-
before: "must be before {{restriction}}"
|
342
|
-
on_or_before: "must be on or before {{restriction}}"
|
343
|
-
after: "must be after {{restriction}}"
|
344
|
-
on_or_after: "must be on or after {{restriction}}"
|
345
|
-
between: "must be between {{earliest}} and {{latest}}"
|
346
|
-
|
347
|
-
The {{restriction}} signifies where the interpolation value for the restriction
|
348
|
-
will be inserted.
|
349
|
-
|
350
|
-
And for something a little more specific you can override the format of the interpolation
|
351
|
-
values inserted in the error messages for temporal restrictions like so
|
352
|
-
|
353
|
-
For Rails 2.0/2.1:
|
354
|
-
|
355
|
-
ValidatesTimeliness::Validator.error_value_formats.update(
|
356
|
-
:time => '%H:%M:%S',
|
357
|
-
:date => '%Y-%m-%d',
|
358
|
-
:datetime => '%Y-%m-%d %H:%M:%S'
|
359
|
-
)
|
360
|
-
|
361
|
-
Rails 2.2+ using the I18n system to define new defaults:
|
362
|
-
|
363
|
-
validates_timeliness:
|
364
|
-
error_value_formats:
|
365
|
-
date: '%Y-%m-%d'
|
366
|
-
time: '%H:%M:%S'
|
367
|
-
datetime: '%Y-%m-%d %H:%M:%S'
|
368
|
-
|
369
|
-
Those are Ruby strftime formats not the plugin formats.
|
236
|
+
# in the setup block
|
237
|
+
config.enable_date_time_select_extension!
|
370
238
|
|
371
239
|
|
372
|
-
===
|
240
|
+
=== Strict Parsing for Select Helpers
|
373
241
|
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
options are then verified and reported if they fail.
|
379
|
-
|
380
|
-
First require it in your spec_helper.rb
|
381
|
-
|
382
|
-
require 'validates_timeliness/matcher'
|
383
|
-
|
384
|
-
Use it like so:
|
385
|
-
|
386
|
-
@person.should validate_date(:birth_date, :before => Time.now, :before_message => 'should be before today')
|
242
|
+
When using date/time select helpers, the component values are handled by ActiveRecord using
|
243
|
+
the Time class to instantiate them into a time value. But this mean that some invalid dates,
|
244
|
+
such as 31st June, are shifted forward and treated as valid. To handle these cases in a strict
|
245
|
+
way you can enable the plugin handler to treat them as invalid dates.
|
387
246
|
|
247
|
+
To activate it, put this in an initializer:
|
388
248
|
|
389
|
-
|
249
|
+
# in the setup block
|
250
|
+
config.enable_multiparameter_extension!
|
390
251
|
|
391
|
-
== CREDITS:
|
392
252
|
|
393
|
-
|
253
|
+
== Credits
|
394
254
|
|
395
|
-
*
|
396
|
-
For his validates_date_time plugin which I have used up until this plugin and
|
397
|
-
which influenced some of the design and I borrowed a little of code from it.
|
255
|
+
* Adam Meehan (adam.meehan@gmail.com, http://github.com/adzap)
|
398
256
|
|
399
257
|
|
400
|
-
==
|
258
|
+
== License
|
401
259
|
|
402
260
|
Copyright (c) 2008-2010 Adam Meehan, released under the MIT license
|