mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
Compare commits
198 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b874e786b6 | ||
|
|
b356363791 | ||
|
|
e9eb812c9e | ||
|
|
353f154330 | ||
|
|
ccfad007d1 | ||
|
|
f3c0dd6144 | ||
|
|
6349990243 | ||
|
|
220b58ab14 | ||
|
|
f98e503e25 | ||
|
|
3e1a9fc107 | ||
|
|
99d742e3d0 | ||
|
|
77f338d97c | ||
|
|
d255bbfccf | ||
|
|
4df1974524 | ||
|
|
82c0e1bcd3 | ||
|
|
f1a0016bf7 | ||
|
|
59d9957ab6 | ||
|
|
694c3b0ce3 | ||
|
|
a859827af4 | ||
|
|
f8b9f72693 | ||
|
|
7f1ed79a89 | ||
|
|
f3c119e191 | ||
|
|
3bfc7b748f | ||
|
|
40369efdff | ||
|
|
f41016af93 | ||
|
|
5258256d5e | ||
|
|
9cfbb2a458 | ||
|
|
78baa7a3cc | ||
|
|
7d6967da90 | ||
|
|
0c38b6abd1 | ||
|
|
a6712de5ff | ||
|
|
c580c3e682 | ||
|
|
9c5db44500 | ||
|
|
f109443fb7 | ||
|
|
96bf4bf184 | ||
|
|
90be6a6db5 | ||
|
|
4d5d82ff20 | ||
|
|
b11893eac0 | ||
|
|
76e159b350 | ||
|
|
c762b6d4f8 | ||
|
|
91f9f65bc0 | ||
|
|
6db8b7d908 | ||
|
|
d3c5101f92 | ||
|
|
899e96b880 | ||
|
|
162faf632a | ||
|
|
1b865cc834 | ||
|
|
c29478df45 | ||
|
|
df3283e5a1 | ||
|
|
0e382e15f2 | ||
|
|
9a697b9cab | ||
|
|
7bf7ed0569 | ||
|
|
a969a49ae8 | ||
|
|
687e61a3f2 | ||
|
|
4cc20ae620 | ||
|
|
2d510504e6 | ||
|
|
2028d68b17 | ||
|
|
e399c6b510 | ||
|
|
7aa1a87731 | ||
|
|
1a31e7463d | ||
|
|
14c55e3292 | ||
|
|
75a3b2bd83 | ||
|
|
9b300e084b | ||
|
|
d61dddfbc6 | ||
|
|
4ac4281c8b | ||
|
|
a56cf674b2 | ||
|
|
7f4d7b38d7 | ||
|
|
34c0f25225 | ||
|
|
57c3fdca88 | ||
|
|
805bb5d7fd | ||
|
|
7aac14c874 | ||
|
|
7c0c1afe1c | ||
|
|
5295a494e9 | ||
|
|
572d80d227 | ||
|
|
af3a3b84ab | ||
|
|
4df63c7142 | ||
|
|
26066dedfe | ||
|
|
3348ca3063 | ||
|
|
88ee33ae41 | ||
|
|
51afb2852e | ||
|
|
57b8a52f07 | ||
|
|
bb94e234bc | ||
|
|
f041524124 | ||
|
|
e1d23d0f2b | ||
|
|
613001791a | ||
|
|
6d12790d2b | ||
|
|
b197d537f1 | ||
|
|
1e3c802031 | ||
|
|
d89266d9f1 | ||
|
|
9e2d95c3e1 | ||
|
|
fb520bbddc | ||
|
|
8303be05c3 | ||
|
|
f4ed751c26 | ||
|
|
956933f58b | ||
|
|
7967b5a212 | ||
|
|
a836ed8434 | ||
|
|
c2a4f45b5a | ||
|
|
312c1510cb | ||
|
|
88fce1d679 | ||
|
|
ffd8476f74 | ||
|
|
728c5ccda5 | ||
|
|
4f0c81b6f8 | ||
|
|
11e643c0fe | ||
|
|
d1ee94248b | ||
|
|
eecef62de4 | ||
|
|
fae38cfecd | ||
|
|
515a1b3126 | ||
|
|
736b1b582f | ||
|
|
903850bc23 | ||
|
|
c3f3edf324 | ||
|
|
1dbac5190b | ||
|
|
497a97e0b0 | ||
|
|
9dd3282a81 | ||
|
|
e7e9a8b238 | ||
|
|
cb962d1157 | ||
|
|
403a91addf | ||
|
|
7ca84f3116 | ||
|
|
19457a6c1d | ||
|
|
5e85649a34 | ||
|
|
e76c53a295 | ||
|
|
f93720177b | ||
|
|
1181b725d0 | ||
|
|
12aa78271e | ||
|
|
862b41f903 | ||
|
|
904c202fb4 | ||
|
|
1001d29c01 | ||
|
|
29c23a7a26 | ||
|
|
7ef9078369 | ||
|
|
a1ae5f9313 | ||
|
|
b3e235a8a1 | ||
|
|
71583805c8 | ||
|
|
2ee971623c | ||
|
|
817e49940c | ||
|
|
a76fc112e7 | ||
|
|
575ff85346 | ||
|
|
7ed76b5161 | ||
|
|
65ed8a657e | ||
|
|
360108c39f | ||
|
|
0ad8ace335 | ||
|
|
7c9ec695f4 | ||
|
|
6af61917dd | ||
|
|
43e6748cd2 | ||
|
|
760a52a2a4 | ||
|
|
9f1642c730 | ||
|
|
011ea070db | ||
|
|
b632093ce2 | ||
|
|
525b3b9941 | ||
|
|
db8dd9ac99 | ||
|
|
1fdfc23cb8 | ||
|
|
7d3ee4bc1b | ||
|
|
694a4bdd69 | ||
|
|
07359c6157 | ||
|
|
215b3dedfd | ||
|
|
753a63417b | ||
|
|
af923014f5 | ||
|
|
a7c6e37333 | ||
|
|
a14bc306b3 | ||
|
|
a71d6f7945 | ||
|
|
45ab815039 | ||
|
|
c308aaf4a9 | ||
|
|
6584d0f1f0 | ||
|
|
5abaec66ae | ||
|
|
ea5452a604 | ||
|
|
40437c970d | ||
|
|
37bfbfe5e7 | ||
|
|
897a9a3bd3 | ||
|
|
e9fa4ca20a | ||
|
|
e4760126e2 | ||
|
|
12af4d8d9d | ||
|
|
81330e7aad | ||
|
|
e82b1e2033 | ||
|
|
2ba85772b6 | ||
|
|
64ffb52dae | ||
|
|
ea6ec0cd75 | ||
|
|
a691b4ed35 | ||
|
|
e2790538a8 | ||
|
|
75f3ef34e8 | ||
|
|
4cb51ae602 | ||
|
|
dad55456d5 | ||
|
|
87b0beef5a | ||
|
|
6cd6cd9dc0 | ||
|
|
0c5cc1a536 | ||
|
|
c224db7af8 | ||
|
|
4868746e94 | ||
|
|
bf999170d7 | ||
|
|
dbfd9231b5 | ||
|
|
3da24f0f33 | ||
|
|
6f4306973b | ||
|
|
71f2a43424 | ||
|
|
c386a9cdcf | ||
|
|
1596ffd2cb | ||
|
|
d847d3b95a | ||
|
|
d0e60ece92 | ||
|
|
3ac65b507f | ||
|
|
d71f581e10 | ||
|
|
aa42fb76b6 | ||
|
|
b0647d456e | ||
|
|
9610d79d7d | ||
|
|
412ff22dd9 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pkg/
|
||||||
130
CHANGELOG
130
CHANGELOG
@@ -1,28 +1,116 @@
|
|||||||
[2008-11-13]
|
= 2.3.1 [2010-03-19]
|
||||||
- allow uppercase meridian to be valid [reported by Alex (http://alex.digns.com/)]
|
- Fixed bug where custom attribute writer method for date/times were being overriden
|
||||||
|
|
||||||
[2008-10-28]
|
= 2.3.0 [2010-02-04]
|
||||||
- fixed bug when dirty attributes not reflecting change when attribute changed from time value to nil [reported by Brad (pvjq)]
|
- 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.
|
||||||
- fixes for Rails 2.2 compatibility. Will refactor in to Rails version specific branches in the future.
|
- Fixed I18n support so it returns missing translation message instead of error
|
||||||
|
- Fixed attribute method bug. Write method was bypassed when method was first generated and used Rails default parser.
|
||||||
|
- Fixed date/time selects when using enable_datetime_select_extension! when some values empty
|
||||||
|
- Fixed ISO8601 datetime format which is now split into two formats
|
||||||
|
- Changed I18n error value format to fallback to global default if missing in locale
|
||||||
|
- Refactored date/time select invalid value extension to use param values. Functionality will be extracted from plugin for v3.
|
||||||
|
|
||||||
[2008-09-24]
|
= 2.2.2 [2009-09-19]
|
||||||
- refactored attribute write method definitions
|
- Fixed dummy_time using make_time to respect timezone. Fixes 1.9.1 bug.
|
||||||
|
|
||||||
[2008-08-25]
|
= 2.2.1 [2009-09-12]
|
||||||
- fixed bug for non-timezone write method not updating changed attributes hash [reported by Sylvestre Mergulhão]
|
- Fixed dummy date part for time types in Validator.type_cast_value
|
||||||
|
- No more core extensions! Removed dummy_time methods.
|
||||||
|
|
||||||
[2008-08-22]
|
= 2.2.0 [2009-09-12]
|
||||||
- fixed bug with attribute cache not clearing on write for date and time columns [reported by Sylvestre Mergulhão]
|
- Ruby 1.9 support!
|
||||||
- parse method returns Date object for date column assigned string as per normal Rails behaviour
|
- Customise dummy date values for time types. See DUMMY DATE FOR TIME TYPES.
|
||||||
- parse method returns same object type when assigned Date or Time object as per normal Rails behaviour
|
- Fixed matcher conflict with Shoulda. Load plugin matcher manually now see matcher section in README
|
||||||
|
- Fixed :ignore_usec when used with :with_time or :with_date
|
||||||
|
- Some clean up and refactoring
|
||||||
|
|
||||||
[2008-08-07]
|
= 2.1.0 [2009-06-20]
|
||||||
- modified matcher option value parsing to allow same value types as validation method
|
- Added ambiguous year threshold setting in Formats class to customize the threshold for 2 digit years (See README)
|
||||||
- fixed matcher message
|
- Fixed interpolation values in custom error message for Rails 2.2+
|
||||||
|
- Fixed custom I18n local override of en locale
|
||||||
|
- Dramatically simplified ActiveRecord monkey patching and hackery
|
||||||
|
|
||||||
[2008-08-02]
|
= 2.0.0 [2009-04-12]
|
||||||
- refactored validation
|
- Error value formats are now specified in the i18n locale file instead of updating plugin hash. See OTHER CUSTOMISATION section in README.
|
||||||
- refactored matcher
|
- Date/time select helper extension is disabled by default. To enable see DISPLAY INVALID VALUES IN DATE HELPERS section in README to enable.
|
||||||
|
- Added :format option to limit validation to a single format if desired
|
||||||
|
- Matcher now supports :equal_to option
|
||||||
|
- 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.
|
||||||
|
- Refactored to remove as much plugin code from ActiveRecord as possible.
|
||||||
|
|
||||||
[2008-07-30]
|
= 1.1.7 [2009-03-26]
|
||||||
- removed setting values to nil when validation fails to preserve before_type_cast value
|
- Minor change to multiparameter attributes which I had not properly implemented for chaining
|
||||||
|
|
||||||
|
= 1.1.6 [2009-03-19]
|
||||||
|
- Rail 2.3 support
|
||||||
|
- 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
|
||||||
|
- Added :equal_to option
|
||||||
|
- Option key validation
|
||||||
|
- Better behaviour with other plugins using alias_method_chain on read_attribute and define_attribute_methods
|
||||||
|
- Added option to enable datetime_select extension for future use to optionally enable. Enabled by default until version 2.
|
||||||
|
- Added :ignore_usec option for datetime restrictions to be compared without microsecond
|
||||||
|
- some refactoring
|
||||||
|
|
||||||
|
= 1.1.5 [2009-01-21]
|
||||||
|
- 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
|
||||||
|
|
||||||
|
= 1.1.4 [2009-01-13]
|
||||||
|
- Make months names respect i18n in Formats
|
||||||
|
|
||||||
|
= 1.1.3 [2009-01-13]
|
||||||
|
- Fixed bug where time and date attributes still being parsed on read using Rails default parser [reported by Brad (pvjq)]
|
||||||
|
|
||||||
|
= 1.1.2 [2009-01-12]
|
||||||
|
- Fixed bugs
|
||||||
|
- matcher failing for custom error message without interpolation keys using I18n
|
||||||
|
- validator custom error messages not being extracted
|
||||||
|
|
||||||
|
= 1.1.1 [2009-01-03]
|
||||||
|
- Fixed bug in matcher for options local variable
|
||||||
|
|
||||||
|
= 1.1.0 [2009-01-01]
|
||||||
|
- Added between option
|
||||||
|
|
||||||
|
= 1.0.0 [2008-12-06]
|
||||||
|
- Gemified!
|
||||||
|
- Refactor of plugin into a Data Mapper style validator class which makes for a cleaner implementation and possible future Merb\Data Mapper support
|
||||||
|
- Added Rails 2.2 i18n support. Plugin error messages can specified in locale files. See README.
|
||||||
|
- ignore_datetime_restriction_errors setting has been moved from AR to ValidatesTimeliness::Validator.ignore_restriction_errors
|
||||||
|
- date_time_error_value_formats setting has been moved from AR to ValidatesTimeliness::Validator.error_value_formats
|
||||||
|
- Namespaced modules and specs
|
||||||
|
- Clean up of specs
|
||||||
|
- fixed a few bugs
|
||||||
|
- accessor methods not generating properly due method name stored as symbol in generated_attributes which fails on lookup
|
||||||
|
- force value assigned to time/datetime attributes to time objects
|
||||||
|
|
||||||
|
= 0.1.0 [2008-12-06]
|
||||||
|
- Tagged plugin as version 0.1.0
|
||||||
|
|
||||||
|
= 2008-11-13
|
||||||
|
- allow uppercase meridian to be valid [reported by Alex (http://alex.digns.com/)]
|
||||||
|
|
||||||
|
= 2008-10-28
|
||||||
|
- fixed bug when dirty attributes not reflecting change when attribute changed from time value to nil [reported by Brad (pvjq)]
|
||||||
|
- fixes for Rails 2.2 compatibility. Will refactor in to Rails version specific branches in the future.
|
||||||
|
|
||||||
|
= 2008-09-24
|
||||||
|
- refactored attribute write method definitions
|
||||||
|
|
||||||
|
= 2008-08-25
|
||||||
|
- fixed bug for non-timezone write method not updating changed attributes hash [reported by Sylvestre Mergulhão]
|
||||||
|
|
||||||
|
= 2008-08-22
|
||||||
|
- fixed bug with attribute cache not clearing on write for date and time columns [reported by Sylvestre Mergulhão]
|
||||||
|
- parse method returns Date object for date column assigned string as per normal Rails behaviour
|
||||||
|
- parse method returns same object type when assigned Date or Time object as per normal Rails behaviour
|
||||||
|
|
||||||
|
= 2008-08-07
|
||||||
|
- modified matcher option value parsing to allow same value types as validation method
|
||||||
|
- fixed matcher message
|
||||||
|
|
||||||
|
= 2008-08-02
|
||||||
|
- refactored validation
|
||||||
|
- refactored matcher
|
||||||
|
|
||||||
|
= 2008-07-30
|
||||||
|
- removed setting values to nil when validation fails to preserve before_type_cast value
|
||||||
|
|||||||
305
README
305
README
@@ -1,305 +0,0 @@
|
|||||||
= validates_timeliness
|
|
||||||
|
|
||||||
* Source: http://github.com/adzap/validates_timeliness
|
|
||||||
* Tickets: http://adzap.lighthouseapp.com/projects/14111-validates_timeliness
|
|
||||||
|
|
||||||
== DESCRIPTION:
|
|
||||||
|
|
||||||
Validate dates, times and datetimes for Rails 2.x. Plays nicely with new Rails 2.1
|
|
||||||
features such as automatic timezone handling and dirty attributes. Allows you to
|
|
||||||
add custom formats or remove defaults easily. This allows you to control what you
|
|
||||||
think should be a valid date or time string.
|
|
||||||
|
|
||||||
|
|
||||||
== FEATURES:
|
|
||||||
|
|
||||||
* Adds ActiveRecord validation for dates, times and datetimes
|
|
||||||
|
|
||||||
* Add or remove date/time formats to customize validation
|
|
||||||
|
|
||||||
* Create new formats using very simple date/time format tokens
|
|
||||||
|
|
||||||
* Restores ability to see raw value entered for date/time attributes with
|
|
||||||
_before_type_cast modifier, which was lost in Rails 2.1.
|
|
||||||
|
|
||||||
* Respects new timezone features of Rails 2.1.
|
|
||||||
|
|
||||||
|
|
||||||
== INSTALLATION:
|
|
||||||
|
|
||||||
./script/plugin git://github.com/adzap/validates_timeliness
|
|
||||||
|
|
||||||
|
|
||||||
== USAGE:
|
|
||||||
|
|
||||||
To validate a model with a date, time or datetime attribute you just use the
|
|
||||||
validation method
|
|
||||||
|
|
||||||
class Person < ActiveRecord::Base
|
|
||||||
validates_date :date_of_birth
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
The list of validation methods available are as follows:
|
|
||||||
|
|
||||||
* validates_date - validate value as date
|
|
||||||
|
|
||||||
* validates_time - validate value as time only i.e. '12:20pm'
|
|
||||||
|
|
||||||
* validates_datetime - validate value as a full date and time
|
|
||||||
|
|
||||||
The validation methods take the usual options plus some specific ones to restrict
|
|
||||||
the valid range of dates or times allowed
|
|
||||||
|
|
||||||
Temporal options:
|
|
||||||
:before - Attribute must be before this value to be valid
|
|
||||||
:on_or_before - Attribute must be equal to or before this value to be valid
|
|
||||||
:after - Attribute must be after this value to be valid
|
|
||||||
:on_or_after - Attribute must be equal to or after this value to be valid
|
|
||||||
|
|
||||||
Regular validation options:
|
|
||||||
:allow_nil - Allow a nil value to be valid
|
|
||||||
:allow_blank - Allows a nil or empty string value to be valid
|
|
||||||
:if - Execute validation when :if evaluates true
|
|
||||||
:unless - Execute validation when :unless evaluates false
|
|
||||||
|
|
||||||
Message options: - Use these to override the default error messages
|
|
||||||
:invalid_date_message
|
|
||||||
:invalid_time_message
|
|
||||||
:invalid_datetime_message
|
|
||||||
:before_message
|
|
||||||
:on_or_before_message
|
|
||||||
:after_message
|
|
||||||
:on_or_after_message
|
|
||||||
|
|
||||||
The temporal options can take 4 different value types:
|
|
||||||
|
|
||||||
* String value
|
|
||||||
* Date, Time, or DateTime object value
|
|
||||||
* Proc or lambda object
|
|
||||||
* A symbol matching the method name in the model
|
|
||||||
|
|
||||||
When an attribute value is compared to temporal options, they are compared as
|
|
||||||
the same type as the validation method type. So using validates_date means all
|
|
||||||
values are compared as dates.
|
|
||||||
|
|
||||||
== EXAMPLES:
|
|
||||||
|
|
||||||
validates_date :date_of_birth :before => Proc.new { 18.years.ago },
|
|
||||||
:before_message => "must be at least 18 years old"
|
|
||||||
|
|
||||||
validates_time :breakfast_time, :on_or_after => '6:00am',
|
|
||||||
:on_or_after_message => 'must be after opening time',
|
|
||||||
:before => :second_breakfast_time,
|
|
||||||
:allow_nil => true
|
|
||||||
|
|
||||||
validates_datetime :appointment_date, :before => Proc.new { 1.week.from_now }
|
|
||||||
|
|
||||||
|
|
||||||
=== DATE/TIME FORMATS:
|
|
||||||
|
|
||||||
So what formats does the plugin allow. Well there are default formats which can
|
|
||||||
be added to easily using the plugins format rules. Also formats can be easily
|
|
||||||
removed without hacking the plugin at all.
|
|
||||||
|
|
||||||
Below are the default formats. If you think they are easy to read then you will
|
|
||||||
be happy to know that is exactly the format you can use to define your own if
|
|
||||||
you want. No complex regular expressions or duck punching (monkey patching) the
|
|
||||||
plugin is needed.
|
|
||||||
|
|
||||||
Time formats:
|
|
||||||
hh:nn:ss
|
|
||||||
hh-nn-ss
|
|
||||||
h:nn
|
|
||||||
h.nn
|
|
||||||
h nn
|
|
||||||
h-nn
|
|
||||||
h:nn_ampm
|
|
||||||
h.nn_ampm
|
|
||||||
h nn_ampm
|
|
||||||
h-nn_ampm
|
|
||||||
h_ampm
|
|
||||||
|
|
||||||
NOTE: Any time format without a meridian token (the 'ampm' token) is considered
|
|
||||||
in 24 hour time.
|
|
||||||
|
|
||||||
Date formats:
|
|
||||||
yyyy/mm/dd
|
|
||||||
yyyy-mm-dd
|
|
||||||
yyyy.mm.dd
|
|
||||||
m/d/yy OR d/m/yy
|
|
||||||
m\d\yy OR d\m\yy
|
|
||||||
d-m-yy
|
|
||||||
d.m.yy
|
|
||||||
d mmm yy
|
|
||||||
|
|
||||||
NOTE: To use non-US date formats see US/EURO FORMATS section
|
|
||||||
|
|
||||||
Datetime formats:
|
|
||||||
m/d/yy h:nn:ss OR d/m/yy hh:nn:ss
|
|
||||||
m/d/yy h:nn OR d/m/yy h:nn
|
|
||||||
m/d/yy h:nn_ampm OR d/m/yy h:nn_ampm
|
|
||||||
yyyy-mm-dd hh:nn:ss
|
|
||||||
yyyy-mm-dd h:nn
|
|
||||||
ddd mmm d hh:nn:ss zo yyyy # Ruby time string
|
|
||||||
yyyy-mm-ddThh:nn:ss(?:Z|zo) # ISO 8601
|
|
||||||
|
|
||||||
NOTE: To use non-US date formats see US/EURO FORMATS section
|
|
||||||
|
|
||||||
Here is what each format token means:
|
|
||||||
|
|
||||||
Format tokens:
|
|
||||||
y = year
|
|
||||||
m = month
|
|
||||||
d = day
|
|
||||||
h = hour
|
|
||||||
n = minute
|
|
||||||
s = second
|
|
||||||
u = micro-seconds
|
|
||||||
ampm = meridian (am or pm) with or without dots (e.g. am, a.m, or a.m.)
|
|
||||||
_ = optional space
|
|
||||||
tz = Timezone abbreviation (e.g. UTC, GMT, PST, EST)
|
|
||||||
zo = Timezone offset (e.g. +10:00, -08:00, +1000)
|
|
||||||
|
|
||||||
Repeating tokens:
|
|
||||||
x = 1 or 2 digits for unit (e.g. 'h' means an hour can be '9' or '09')
|
|
||||||
xx = 2 digits exactly for unit (e.g. 'hh' means an hour can only be '09')
|
|
||||||
|
|
||||||
Special Cases:
|
|
||||||
yy = 2 or 4 digit year
|
|
||||||
yyyyy = exactly 4 digit year
|
|
||||||
mmm = month long name (e.g. 'Jul' or 'July')
|
|
||||||
ddd = Day name of 3 to 9 letters (e.g. Wed or Wednesday)
|
|
||||||
u = microseconds matches 1 to 3 digits
|
|
||||||
|
|
||||||
All other characters are considered literal. For the technically minded
|
|
||||||
(well you are developers), these formats are compiled into regular expressions
|
|
||||||
at runtime so don't add any extra overhead than using regular expressions
|
|
||||||
directly. So, no, it won't make your app slow!
|
|
||||||
|
|
||||||
To see all defined formats look in lib/validates_timeliness/formats.rb.
|
|
||||||
|
|
||||||
=== US/EURO FORMATS
|
|
||||||
|
|
||||||
The perenial problem for non-US developers or applications not primarily for the
|
|
||||||
US, is the US date format of m/d/yy. This is ambiguous with the European format
|
|
||||||
of d/my/yy. By default the plugin uses the US formats as this is the Ruby default
|
|
||||||
when it does date interpretation, and is in keeping PoLS (principle of least
|
|
||||||
surprise).
|
|
||||||
|
|
||||||
To switch to using the European (or Rest of The World) formats put this in an
|
|
||||||
initializer or environment.rb
|
|
||||||
|
|
||||||
ValidatesTimeliness::Formats.remove_us_formats
|
|
||||||
|
|
||||||
Now '01/02/2000' will be parsed as 1st February 2000, instead of 2nd January 2000.
|
|
||||||
|
|
||||||
=== CUSTOMISING FORMATS:
|
|
||||||
|
|
||||||
I hear you say "Thats greats but I don't want X format to be valid". Well to
|
|
||||||
remove a format stick this in an initializer file
|
|
||||||
|
|
||||||
ValidatesTimeliness::Formats.remove_formats(:date, 'm\d\yy')
|
|
||||||
|
|
||||||
Done! That format is no longer considered valid. Easy!
|
|
||||||
|
|
||||||
Ok, now I hear you say "Well I have format that I want to use but you don't have it".
|
|
||||||
Ahh, then add it yourself. Again stick this in an initializer file
|
|
||||||
|
|
||||||
ValidatesTimeliness::Formats.add_formats(:time, "d o'clock")
|
|
||||||
|
|
||||||
Now "10 o'clock" will be a valid value. So easy, no more whingeing!
|
|
||||||
|
|
||||||
You can embed regular expressions in the format but no gurantees that it will
|
|
||||||
remain intact. If you avoid the use of any token characters and regexp dots or
|
|
||||||
backslashes as special characters in the regexp, it may well work as expected.
|
|
||||||
For special characters use POSIX character classes for safety. See the ISO 8601
|
|
||||||
datetime for an example of an embedded regular expression.
|
|
||||||
|
|
||||||
Because formats are evaluated in order, adding a format which may be ambiguous
|
|
||||||
with an existing format, will mean your format is ignored. If you need to make
|
|
||||||
your new format higher precedence than an existing format, you can include the
|
|
||||||
before option like so
|
|
||||||
|
|
||||||
ValidatesTimeliness::Formats.add_formats(:time, 'ss:nn:hh', :before => 'hh:nn:ss')
|
|
||||||
|
|
||||||
Now a time of '59:30:23' will be interpreted as 11:30:59 pm. This option saves
|
|
||||||
you adding a new one and deleting an old one to get it to work.
|
|
||||||
|
|
||||||
|
|
||||||
=== TEMPORAL OPTION ERRORS:
|
|
||||||
|
|
||||||
When using the validation temporal options there are times when the restriction
|
|
||||||
value itself may be invalid. Normally this will add an error to the model such as
|
|
||||||
'restriction :before value was invalid'. These can be annoying if you are using
|
|
||||||
procs or methods as restrictions and don't care if they don't evaluate properly
|
|
||||||
and you want the validation to complete. In these situations you turn them off.
|
|
||||||
|
|
||||||
To turn them off globally:
|
|
||||||
|
|
||||||
ActiveRecord::Base.ignore_datetime_restriction_errors = true
|
|
||||||
|
|
||||||
To switch them on or off per model:
|
|
||||||
|
|
||||||
class Person < ActiveRecord::Base
|
|
||||||
self.ignore_datetime_restriction_errors = false
|
|
||||||
end
|
|
||||||
|
|
||||||
A word of warning though, as this may hide issues with the model and make those
|
|
||||||
corner cases a little harder to test. In general if you are using procs or
|
|
||||||
model methods and you only care when they return a value, then they should
|
|
||||||
return nil in all other situations. Restrictions are skipped if they are nil.
|
|
||||||
|
|
||||||
=== OTHER CUSTOMISATION:
|
|
||||||
|
|
||||||
The error messages for each temporal option can also be globally overridden by
|
|
||||||
updating the default AR error messages like so
|
|
||||||
|
|
||||||
ActiveRecord::Errors.default_error_messages.update(
|
|
||||||
:invalid_date => "is not a valid date",
|
|
||||||
:invalid_time => "is not a valid time",
|
|
||||||
:invalid_datetime => "is not a valid datetime",
|
|
||||||
:before => "must be before %s",
|
|
||||||
:on_or_before => "must be on or before %s",
|
|
||||||
:after => "must be after %s",
|
|
||||||
:on_or_after => "must be on or after %s"
|
|
||||||
)
|
|
||||||
|
|
||||||
Or for something a little more specific you can override the format of the values
|
|
||||||
inserted in the error messages for temporal options like so
|
|
||||||
|
|
||||||
ActiveRecord::Errors.date_time_error_value_formats.update(
|
|
||||||
:time => '%H:%M:%S',
|
|
||||||
:date => '%Y-%m-%d',
|
|
||||||
:datetime => '%Y-%m-%d %H:%M:%S'
|
|
||||||
)
|
|
||||||
|
|
||||||
Those are Ruby strftime formats not the plugin formats.
|
|
||||||
|
|
||||||
|
|
||||||
=== RSPEC MATCHER:
|
|
||||||
|
|
||||||
To sweeten the deal that little bit more, you have an Rspec matcher available for
|
|
||||||
you model specs. Now you can easily test the validations you have just written
|
|
||||||
with the plugin or better yet *before* you write them! You just use the
|
|
||||||
validation options you want as you would with the validation method. Those
|
|
||||||
options are then verified and reported if they fail. Use it like so:
|
|
||||||
|
|
||||||
|
|
||||||
@person.should validate_date(:birth_date, :before => Time.now, :before_message => 'should be before today')
|
|
||||||
|
|
||||||
|
|
||||||
The matcher names are just the singular of the validation methods.
|
|
||||||
|
|
||||||
== CREDITS:
|
|
||||||
|
|
||||||
* Adam Meehan (adam.meehan@gmail.com, http://duckpunching.com/)
|
|
||||||
|
|
||||||
* Jonathan Viney (http://workingwithrails.com/person/4985-jonathan-viney)
|
|
||||||
For his validates_date_time plugin which I have used up until this plugin and
|
|
||||||
which influenced some of the design and I borrowed a little of code from it.
|
|
||||||
|
|
||||||
|
|
||||||
== LICENSE:
|
|
||||||
|
|
||||||
Copyright (c) 2008 Adam Meehan, released under the MIT license
|
|
||||||
403
README.rdoc
Normal file
403
README.rdoc
Normal file
@@ -0,0 +1,403 @@
|
|||||||
|
= validates_timeliness
|
||||||
|
|
||||||
|
* Source: http://github.com/adzap/validates_timeliness
|
||||||
|
* Bugs: http://github.com/adzap/validates_timeliness/issues
|
||||||
|
|
||||||
|
== DESCRIPTION:
|
||||||
|
|
||||||
|
Validate dates, times and datetimes for Rails 2.x. Plays nicely with Rails
|
||||||
|
automatic timezone handling. Allows you to add custom formats or remove defaults
|
||||||
|
easily. This allows you to control what you think should be a valid date or
|
||||||
|
time string.
|
||||||
|
|
||||||
|
|
||||||
|
== FEATURES:
|
||||||
|
|
||||||
|
* Adds ActiveRecord validation for dates, times and datetimes
|
||||||
|
|
||||||
|
* Add or remove date/time formats to customize validation
|
||||||
|
|
||||||
|
* Create new formats using very simple date/time format tokens
|
||||||
|
|
||||||
|
* Restores ability to see raw value entered for date/time attributes with
|
||||||
|
_before_type_cast modifier, which was lost in Rails 2.1.
|
||||||
|
|
||||||
|
* Supports Rails timezone handling
|
||||||
|
|
||||||
|
* Supports Rails I18n for the error messages
|
||||||
|
|
||||||
|
* Rspec matcher for testing model validation of dates and times
|
||||||
|
|
||||||
|
|
||||||
|
== INSTALLATION:
|
||||||
|
|
||||||
|
As plugin (from master)
|
||||||
|
|
||||||
|
./script/plugin install git://github.com/adzap/validates_timeliness.git
|
||||||
|
|
||||||
|
As gem
|
||||||
|
|
||||||
|
sudo gem install validates_timeliness
|
||||||
|
|
||||||
|
# in environment.rb
|
||||||
|
|
||||||
|
config.gem 'validates_timeliness'
|
||||||
|
|
||||||
|
|
||||||
|
== USAGE:
|
||||||
|
|
||||||
|
To validate a model with a date, time or datetime attribute you just use the
|
||||||
|
validation method
|
||||||
|
|
||||||
|
class Person < ActiveRecord::Base
|
||||||
|
validates_date :date_of_birth
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
The list of validation methods available are as follows:
|
||||||
|
validates_date - validate value as date
|
||||||
|
validates_time - validate value as time only i.e. '12:20pm'
|
||||||
|
validates_datetime - validate value as a full date and time
|
||||||
|
|
||||||
|
The validation methods take the usual options plus some specific ones to restrict
|
||||||
|
the valid range of dates or times allowed
|
||||||
|
|
||||||
|
Temporal options (or restrictions):
|
||||||
|
:is_at - Attribute must be equal to value to be valid
|
||||||
|
:before - Attribute must be before this value to be valid
|
||||||
|
:on_or_before - Attribute must be equal to or before this value to be valid
|
||||||
|
:after - Attribute must be after this value to be valid
|
||||||
|
:on_or_after - Attribute must be equal to or after this value to be valid
|
||||||
|
:between - Attribute must be between the values to be valid. Takes an array of two values or a range
|
||||||
|
|
||||||
|
Regular validation options:
|
||||||
|
:allow_nil - Allow a nil value to be valid
|
||||||
|
:allow_blank - Allows a nil or empty string value to be valid
|
||||||
|
:if - Execute validation when :if evaluates true
|
||||||
|
:unless - Execute validation when :unless evaluates false
|
||||||
|
|
||||||
|
Special options:
|
||||||
|
:with_time - Validate a date attribute value combined with a time value against any temporal restrictions
|
||||||
|
:with_date - Validate a time attribute value combined with a date value against any temporal restrictions
|
||||||
|
:ignore_usec - Ignores microsecond value on datetime restrictions
|
||||||
|
:format - Limit validation to a single format for special cases. Takes plugin format value.
|
||||||
|
|
||||||
|
Message options: - Use these to override the default error messages
|
||||||
|
:invalid_date_message
|
||||||
|
:invalid_time_message
|
||||||
|
:invalid_datetime_message
|
||||||
|
:is_at_message
|
||||||
|
:before_message
|
||||||
|
:on_or_before_message
|
||||||
|
:after_message
|
||||||
|
:on_or_after_message
|
||||||
|
:between_message
|
||||||
|
|
||||||
|
The temporal restrictions, with_date and with_time can take 4 different value types:
|
||||||
|
* String value
|
||||||
|
* Date, Time, or DateTime object value
|
||||||
|
* Proc or lambda object which may take an optional parameter being the record object
|
||||||
|
* A symbol matching the method name in the model
|
||||||
|
|
||||||
|
When an attribute value is compared to temporal restrictions, they are compared as
|
||||||
|
the same type as the validation method type. So using validates_date means all
|
||||||
|
values are compared as dates. This is except in the case of with_time and with_date
|
||||||
|
options which effectively force the value to validated as a datetime against the
|
||||||
|
temporal options.
|
||||||
|
|
||||||
|
== EXAMPLES:
|
||||||
|
|
||||||
|
validates_date :date_of_birth :before => lambda { 18.years.ago },
|
||||||
|
:before_message => "must be at least 18 years old"
|
||||||
|
|
||||||
|
validates_time :breakfast_time, :on_or_after => '6:00am',
|
||||||
|
:on_or_after_message => 'must be after opening time',
|
||||||
|
:before => :second_breakfast_time,
|
||||||
|
:allow_nil => true
|
||||||
|
|
||||||
|
validates_datetime :appointment_date, :before => lambda { 1.week.from_now }
|
||||||
|
|
||||||
|
validates_date :entry_date, :with_time => '17:00', :on_or_before => :competition_closing
|
||||||
|
|
||||||
|
|
||||||
|
=== DATE/TIME FORMATS:
|
||||||
|
|
||||||
|
So what formats does the plugin allow. Well there are default formats which can
|
||||||
|
be added to easily using the plugins format rules. Also formats can be easily
|
||||||
|
removed without hacking the plugin at all.
|
||||||
|
|
||||||
|
Below are the default formats. If you think they are easy to read then you will
|
||||||
|
be happy to know that is exactly the format you can use to define your own if
|
||||||
|
you want. No complex regular expressions or duck punching (monkey patching) the
|
||||||
|
plugin is needed.
|
||||||
|
|
||||||
|
==== Time formats:
|
||||||
|
hh:nn:ss
|
||||||
|
hh-nn-ss
|
||||||
|
h:nn
|
||||||
|
h.nn
|
||||||
|
h nn
|
||||||
|
h-nn
|
||||||
|
h:nn_ampm
|
||||||
|
h.nn_ampm
|
||||||
|
h nn_ampm
|
||||||
|
h-nn_ampm
|
||||||
|
h_ampm
|
||||||
|
|
||||||
|
NOTE: Any time format without a meridian token (the 'ampm' token) is considered in 24 hour time.
|
||||||
|
|
||||||
|
==== Date formats:
|
||||||
|
yyyy/mm/dd
|
||||||
|
yyyy-mm-dd
|
||||||
|
yyyy.mm.dd
|
||||||
|
m/d/yy OR d/m/yy
|
||||||
|
m\d\yy OR d\m\yy
|
||||||
|
d-m-yy
|
||||||
|
d.m.yy
|
||||||
|
d mmm yy
|
||||||
|
|
||||||
|
NOTE: To use non-US date formats see US/EURO FORMATS section
|
||||||
|
|
||||||
|
==== Datetime formats:
|
||||||
|
m/d/yy h:nn:ss OR d/m/yy hh:nn:ss
|
||||||
|
m/d/yy h:nn OR d/m/yy h:nn
|
||||||
|
m/d/yy h:nn_ampm OR d/m/yy h:nn_ampm
|
||||||
|
yyyy-mm-dd hh:nn:ss
|
||||||
|
yyyy-mm-dd h:nn
|
||||||
|
ddd mmm d hh:nn:ss zo yyyy # Ruby time string
|
||||||
|
yyyy-mm-ddThh:nn:ssZ # ISO 8601 without zone offset
|
||||||
|
yyyy-mm-ddThh:nn:sszo # ISO 8601 with zone offset
|
||||||
|
|
||||||
|
NOTE: To use non-US date formats see US/EURO FORMATS section
|
||||||
|
|
||||||
|
Here is what each format token means:
|
||||||
|
|
||||||
|
Format tokens:
|
||||||
|
y = year
|
||||||
|
m = month
|
||||||
|
d = day
|
||||||
|
h = hour
|
||||||
|
n = minute
|
||||||
|
s = second
|
||||||
|
u = micro-seconds
|
||||||
|
ampm = meridian (am or pm) with or without dots (e.g. am, a.m, or a.m.)
|
||||||
|
_ = optional space
|
||||||
|
tz = Timezone abbreviation (e.g. UTC, GMT, PST, EST)
|
||||||
|
zo = Timezone offset (e.g. +10:00, -08:00, +1000)
|
||||||
|
|
||||||
|
Repeating tokens:
|
||||||
|
x = 1 or 2 digits for unit (e.g. 'h' means an hour can be '9' or '09')
|
||||||
|
xx = 2 digits exactly for unit (e.g. 'hh' means an hour can only be '09')
|
||||||
|
|
||||||
|
Special Cases:
|
||||||
|
yy = 2 or 4 digit year
|
||||||
|
yyyy = exactly 4 digit year
|
||||||
|
mmm = month long name (e.g. 'Jul' or 'July')
|
||||||
|
ddd = Day name of 3 to 9 letters (e.g. Wed or Wednesday)
|
||||||
|
u = microseconds matches 1 to 3 digits
|
||||||
|
|
||||||
|
All other characters are considered literal. For the technically minded
|
||||||
|
(well you are developers), these formats are compiled into regular expressions
|
||||||
|
at runtime so don't add any extra overhead than using regular expressions
|
||||||
|
directly. So, no, it won't make your app slow!
|
||||||
|
|
||||||
|
To see all defined formats look in lib/validates_timeliness/formats.rb.
|
||||||
|
|
||||||
|
=== US/EURO FORMATS
|
||||||
|
|
||||||
|
The perenial problem for non-US developers or applications not primarily for the
|
||||||
|
US, is the US date format of m/d/yy. This is ambiguous with the European format
|
||||||
|
of d/m/yy. By default the plugin uses the US formats as this is the Ruby default
|
||||||
|
when it does date interpretation, and is in keeping PoLS (principle of least
|
||||||
|
surprise).
|
||||||
|
|
||||||
|
To switch to using the European (or Rest of The World) formats put this in an
|
||||||
|
initializer or environment.rb
|
||||||
|
|
||||||
|
ValidatesTimeliness::Formats.remove_us_formats
|
||||||
|
|
||||||
|
Now '01/02/2000' will be parsed as 1st February 2000, instead of 2nd January 2000.
|
||||||
|
|
||||||
|
=== CUSTOMISING FORMATS:
|
||||||
|
|
||||||
|
I hear you say "Thats greats but I don't want X format to be valid". Well to
|
||||||
|
remove a format stick this in an initializer file
|
||||||
|
|
||||||
|
ValidatesTimeliness::Formats.remove_formats(:date, 'm\d\yy')
|
||||||
|
|
||||||
|
Done! That format is no longer considered valid. Easy!
|
||||||
|
|
||||||
|
Ok, now I hear you say "Well I have format that I want to use but you don't have it".
|
||||||
|
Ahh, then add it yourself. Again stick this in an initializer file
|
||||||
|
|
||||||
|
ValidatesTimeliness::Formats.add_formats(:time, "d o'clock")
|
||||||
|
|
||||||
|
Now "10 o'clock" will be a valid value. So easy, no more whingeing!
|
||||||
|
|
||||||
|
You can embed regular expressions in the format but no gurantees that it will
|
||||||
|
remain intact. If you avoid the use of any token characters and regexp dots or
|
||||||
|
backslashes as special characters in the regexp, it may well work as expected.
|
||||||
|
For special characters use POSIX character classes for safety. See the ISO 8601
|
||||||
|
datetime for an example of an embedded regular expression.
|
||||||
|
|
||||||
|
Because formats are evaluated in order, adding a format which may be ambiguous
|
||||||
|
with an existing format, will mean your format is ignored. If you need to make
|
||||||
|
your new format higher precedence than an existing format, you can include the
|
||||||
|
before option like so
|
||||||
|
|
||||||
|
ValidatesTimeliness::Formats.add_formats(:time, 'ss:nn:hh', :before => 'hh:nn:ss')
|
||||||
|
|
||||||
|
Now a time of '59:30:23' will be interpreted as 11:30:59 pm. This option saves
|
||||||
|
you adding a new one and deleting an old one to get it to work.
|
||||||
|
|
||||||
|
|
||||||
|
=== AMBIGUOUS YEAR THRESHOLD
|
||||||
|
|
||||||
|
When dealing with 2 digit year values, by default a year is interpreted as being
|
||||||
|
in the last century at or above 30. You can customize this however
|
||||||
|
|
||||||
|
ValidatesTimeliness::Formats.ambiguous_year_threshold = 20
|
||||||
|
|
||||||
|
Now you get:
|
||||||
|
|
||||||
|
year of 19 is considered 2019
|
||||||
|
year of 20 is considered 1920
|
||||||
|
|
||||||
|
|
||||||
|
=== DUMMY DATE FOR TIME TYPES
|
||||||
|
|
||||||
|
Given that Ruby has no support for a time-only type, all time type columns are evaluated
|
||||||
|
as a regular Time class objects with a dummy date value set. Rails defines the dummy date as
|
||||||
|
2000-01-01. So a time of '12:30' is evaluated as a Time value of '2000-01-01 12:30'. If you
|
||||||
|
need to customize this for some reason you can do so as follows
|
||||||
|
|
||||||
|
ValidatesTimeliness::Formats.dummy_date_for_time_type = [2009, 1, 1]
|
||||||
|
|
||||||
|
The value should be an array of 3 values being year, month and day in that order.
|
||||||
|
|
||||||
|
|
||||||
|
=== TEMPORAL RESTRICTION ERRORS:
|
||||||
|
|
||||||
|
When using the validation temporal restrictions there are times when the restriction
|
||||||
|
value itself may be invalid. Normally this will add an error to the model such as
|
||||||
|
'restriction :before value was invalid'. These can be annoying if you are using
|
||||||
|
procs or methods as restrictions and don't care if they don't evaluate properly
|
||||||
|
and you want the validation to complete. In these situations you turn them off.
|
||||||
|
|
||||||
|
To turn them off:
|
||||||
|
|
||||||
|
ValidatesTimeliness::Validator.ignore_restriction_errors = true
|
||||||
|
|
||||||
|
A word of warning though, as this may hide issues with the model and make those
|
||||||
|
corner cases a little harder to test. In general if you are using procs or
|
||||||
|
model methods and you only care when they return a value, then they should
|
||||||
|
return nil in all other situations. Restrictions are skipped if they are nil.
|
||||||
|
|
||||||
|
|
||||||
|
=== DISPLAY INVALID VALUES IN DATE HELPERS:
|
||||||
|
|
||||||
|
The plugin has some extensions to ActionView and ActiveRecord by allowing invalid
|
||||||
|
date and time values to be redisplayed to the user as feedback, instead of
|
||||||
|
a blank field which happens by default in Rails. Though the date helpers make this a
|
||||||
|
pretty rare occurence, given the select dropdowns for each date/time component, but
|
||||||
|
it may be something of interest.
|
||||||
|
|
||||||
|
To activate it, put this in an initializer:
|
||||||
|
|
||||||
|
ValidatesTimeliness.enable_datetime_select_extension!
|
||||||
|
|
||||||
|
This will be removed from v3 as it adds too little to maintain.
|
||||||
|
|
||||||
|
=== OTHER CUSTOMISATION:
|
||||||
|
|
||||||
|
The error messages for each temporal restrictions can also be globally overridden by
|
||||||
|
updating the default AR error messages like so
|
||||||
|
|
||||||
|
For Rails 2.0/2.1:
|
||||||
|
|
||||||
|
ActiveRecord::Errors.default_error_messages.update(
|
||||||
|
:invalid_date => "is not a valid date",
|
||||||
|
:invalid_time => "is not a valid time",
|
||||||
|
:invalid_datetime => "is not a valid datetime",
|
||||||
|
:is_at => "must be at %s",
|
||||||
|
:before => "must be before %s",
|
||||||
|
:on_or_before => "must be on or before %s",
|
||||||
|
:after => "must be after %s",
|
||||||
|
:on_or_after => "must be on or after %s",
|
||||||
|
:between => "must be between %s and %s"
|
||||||
|
)
|
||||||
|
|
||||||
|
Where %s is the interpolation value for the restriction.
|
||||||
|
|
||||||
|
Rails 2.2+ using the I18n system to define new defaults:
|
||||||
|
|
||||||
|
en:
|
||||||
|
activerecord:
|
||||||
|
errors:
|
||||||
|
messages:
|
||||||
|
invalid_date: "is not a valid date"
|
||||||
|
invalid_time: "is not a valid time"
|
||||||
|
invalid_datetime: "is not a valid datetime"
|
||||||
|
is_at: "must be at {{restriction}}"
|
||||||
|
before: "must be before {{restriction}}"
|
||||||
|
on_or_before: "must be on or before {{restriction}}"
|
||||||
|
after: "must be after {{restriction}}"
|
||||||
|
on_or_after: "must be on or after {{restriction}}"
|
||||||
|
between: "must be between {{earliest}} and {{latest}}"
|
||||||
|
|
||||||
|
The {{restriction}} signifies where the interpolation value for the restriction
|
||||||
|
will be inserted.
|
||||||
|
|
||||||
|
And for something a little more specific you can override the format of the interpolation
|
||||||
|
values inserted in the error messages for temporal restrictions like so
|
||||||
|
|
||||||
|
For Rails 2.0/2.1:
|
||||||
|
|
||||||
|
ValidatesTimeliness::Validator.error_value_formats.update(
|
||||||
|
:time => '%H:%M:%S',
|
||||||
|
:date => '%Y-%m-%d',
|
||||||
|
:datetime => '%Y-%m-%d %H:%M:%S'
|
||||||
|
)
|
||||||
|
|
||||||
|
Rails 2.2+ using the I18n system to define new defaults:
|
||||||
|
|
||||||
|
validates_timeliness:
|
||||||
|
error_value_formats:
|
||||||
|
date: '%Y-%m-%d'
|
||||||
|
time: '%H:%M:%S'
|
||||||
|
datetime: '%Y-%m-%d %H:%M:%S'
|
||||||
|
|
||||||
|
Those are Ruby strftime formats not the plugin formats.
|
||||||
|
|
||||||
|
|
||||||
|
=== RSPEC MATCHER:
|
||||||
|
|
||||||
|
To sweeten the deal that little bit more, you have an Rspec matcher available for
|
||||||
|
you model specs. Now you can easily test the validations you have just written
|
||||||
|
with the plugin or better yet *before* you write them! You just use the
|
||||||
|
validation options you want as you would with the validation method. Those
|
||||||
|
options are then verified and reported if they fail.
|
||||||
|
|
||||||
|
First require it in your spec_helper.rb
|
||||||
|
|
||||||
|
require 'validates_timeliness/matcher'
|
||||||
|
|
||||||
|
Use it like so:
|
||||||
|
|
||||||
|
@person.should validate_date(:birth_date, :before => Time.now, :before_message => 'should be before today')
|
||||||
|
|
||||||
|
|
||||||
|
The matcher names are just the singular of the validation methods.
|
||||||
|
|
||||||
|
== CREDITS:
|
||||||
|
|
||||||
|
* Adam Meehan (adam.meehan@gmail.com, http://duckpunching.com/)
|
||||||
|
|
||||||
|
* Jonathan Viney (http://workingwithrails.com/person/4985-jonathan-viney)
|
||||||
|
For his validates_date_time plugin which I have used up until this plugin and
|
||||||
|
which influenced some of the design and I borrowed a little of code from it.
|
||||||
|
|
||||||
|
|
||||||
|
== LICENSE:
|
||||||
|
|
||||||
|
Copyright (c) 2008-2010 Adam Meehan, released under the MIT license
|
||||||
54
Rakefile
54
Rakefile
@@ -1,14 +1,52 @@
|
|||||||
require 'rake'
|
require 'rubygems'
|
||||||
require 'rake/testtask'
|
require 'rake/gempackagetask'
|
||||||
require 'rake/rdoctask'
|
require 'rubygems/specification'
|
||||||
|
require 'date'
|
||||||
require 'spec/rake/spectask'
|
require 'spec/rake/spectask'
|
||||||
|
require 'lib/validates_timeliness/version'
|
||||||
|
|
||||||
spec_files = Rake::FileList["spec/**/*_spec.rb"]
|
GEM = "validates_timeliness"
|
||||||
|
GEM_VERSION = ValidatesTimeliness::VERSION
|
||||||
|
|
||||||
desc "Run specs"
|
spec = Gem::Specification.new do |s|
|
||||||
Spec::Rake::SpecTask.new do |t|
|
s.name = GEM
|
||||||
t.spec_files = spec_files
|
s.version = GEM_VERSION
|
||||||
t.spec_opts = ["-c"]
|
s.platform = Gem::Platform::RUBY
|
||||||
|
s.rubyforge_project = "validatestime"
|
||||||
|
s.has_rdoc = true
|
||||||
|
s.extra_rdoc_files = ["README.rdoc", "LICENSE", "TODO", "CHANGELOG"]
|
||||||
|
s.summary = "Date and time validation plugin for Rails 2.x which allows custom formats"
|
||||||
|
s.description = s.summary
|
||||||
|
s.author = "Adam Meehan"
|
||||||
|
s.email = "adam.meehan@gmail.com"
|
||||||
|
s.homepage = "http://github.com/adzap/validates_timeliness"
|
||||||
|
|
||||||
|
s.require_path = 'lib'
|
||||||
|
s.autorequire = GEM
|
||||||
|
s.files = %w(LICENSE README.rdoc Rakefile TODO CHANGELOG) + Dir.glob("{lib,spec}/**/*")
|
||||||
end
|
end
|
||||||
|
|
||||||
task :default => :spec
|
task :default => :spec
|
||||||
|
|
||||||
|
desc "Run specs"
|
||||||
|
Spec::Rake::SpecTask.new do |t|
|
||||||
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
||||||
|
t.spec_opts = %w(--color)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Rake::GemPackageTask.new(spec) do |pkg|
|
||||||
|
pkg.gem_spec = spec
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "install the gem locally"
|
||||||
|
task :install => [:package] do
|
||||||
|
sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "create a gemspec file"
|
||||||
|
task :make_spec do
|
||||||
|
File.open("#{GEM}.gemspec", "w") do |file|
|
||||||
|
file.puts spec.to_ruby
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
8
TODO
Normal file
8
TODO
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
- valid formats could come from locale file
|
||||||
|
- add replace_formats instead add_formats :before
|
||||||
|
- array of values for all temporal options
|
||||||
|
- use tz and zo value from time string?
|
||||||
|
- filter valid formats rather than remove for hot swapping without recompilation
|
||||||
|
- config generator
|
||||||
|
- move all config into top namespace
|
||||||
|
- remove action_view stuff
|
||||||
28
benchmark.rb
28
benchmark.rb
@@ -4,73 +4,74 @@ require 'date'
|
|||||||
require 'parsedate'
|
require 'parsedate'
|
||||||
require 'benchmark'
|
require 'benchmark'
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'active_support'
|
|
||||||
require 'active_record'
|
require 'active_record'
|
||||||
require 'action_controller'
|
|
||||||
require 'rails/version'
|
|
||||||
|
|
||||||
require 'validates_timeliness'
|
require 'validates_timeliness'
|
||||||
|
|
||||||
|
def parse(*args)
|
||||||
|
ValidatesTimeliness::Parser.parse(*args)
|
||||||
|
end
|
||||||
|
|
||||||
n = 10000
|
n = 10000
|
||||||
Benchmark.bm do |x|
|
Benchmark.bm do |x|
|
||||||
x.report('timeliness - datetime') {
|
x.report('timeliness - datetime') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("2000-01-04 12:12:12", :datetime)
|
parse("2000-01-04 12:12:12", :datetime)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
x.report('timeliness - date') {
|
x.report('timeliness - date') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("2000-01-04", :date)
|
parse("2000-01-04", :date)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
x.report('timeliness - date as datetime') {
|
x.report('timeliness - date as datetime') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("2000-01-04", :datetime)
|
parse("2000-01-04", :datetime)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
x.report('timeliness - time') {
|
x.report('timeliness - time') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("12:01:02", :time)
|
parse("12:01:02", :time)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
x.report('timeliness - invalid format datetime') {
|
x.report('timeliness - invalid format datetime') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("20xx-01-04 12:12:12", :datetime)
|
parse("20xx-01-04 12:12:12", :datetime)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
x.report('timeliness - invalid format date') {
|
x.report('timeliness - invalid format date') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("20xx-01-04", :date)
|
parse("20xx-01-04", :date)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
x.report('timeliness - invalid format time') {
|
x.report('timeliness - invalid format time') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("12:xx:02", :time)
|
parse("12:xx:02", :time)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
x.report('timeliness - invalid value datetime') {
|
x.report('timeliness - invalid value datetime') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("2000-01-32 12:12:12", :datetime)
|
parse("2000-01-32 12:12:12", :datetime)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
x.report('timeliness - invalid value date') {
|
x.report('timeliness - invalid value date') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("2000-01-32", :date)
|
parse("2000-01-32", :date)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
x.report('timeliness - invalid value time') {
|
x.report('timeliness - invalid value time') {
|
||||||
n.times do
|
n.times do
|
||||||
ActiveRecord::Base.parse_date_time("12:61:02", :time)
|
parse("12:61:02", :time)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
x.report('date/time') {
|
x.report('date/time') {
|
||||||
@@ -96,4 +97,3 @@ Benchmark.bm do |x|
|
|||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,49 @@
|
|||||||
require 'validates_timeliness/attribute_methods'
|
|
||||||
require 'validates_timeliness/validations'
|
|
||||||
require 'validates_timeliness/formats'
|
require 'validates_timeliness/formats'
|
||||||
require 'validates_timeliness/multiparameter_attributes'
|
require 'validates_timeliness/parser'
|
||||||
require 'validates_timeliness/instance_tag'
|
require 'validates_timeliness/validator'
|
||||||
require 'validates_timeliness/validate_timeliness_matcher' if ENV['RAILS_ENV'] == 'test'
|
require 'validates_timeliness/validation_methods'
|
||||||
|
|
||||||
require 'validates_timeliness/core_ext/time'
|
require 'validates_timeliness/active_record/attribute_methods'
|
||||||
require 'validates_timeliness/core_ext/date'
|
require 'validates_timeliness/active_record/multiparameter_attributes'
|
||||||
require 'validates_timeliness/core_ext/date_time'
|
require 'validates_timeliness/action_view/instance_tag'
|
||||||
|
|
||||||
ActiveRecord::Base.send(:include, ValidatesTimeliness::AttributeMethods)
|
module ValidatesTimeliness
|
||||||
ActiveRecord::Base.send(:include, ValidatesTimeliness::Validations)
|
|
||||||
ActiveRecord::Base.send(:include, ValidatesTimeliness::MultiparameterAttributes)
|
|
||||||
ActionView::Helpers::InstanceTag.send(:include, ValidatesTimeliness::InstanceTag)
|
|
||||||
|
|
||||||
Time.send(:include, ValidatesTimeliness::CoreExtensions::Time)
|
mattr_accessor :default_timezone
|
||||||
Date.send(:include, ValidatesTimeliness::CoreExtensions::Date)
|
self.default_timezone = :utc
|
||||||
DateTime.send(:include, ValidatesTimeliness::CoreExtensions::DateTime)
|
|
||||||
|
|
||||||
ValidatesTimeliness::Formats.compile_format_expressions
|
mattr_accessor :use_time_zones
|
||||||
|
self.use_time_zones = false
|
||||||
|
|
||||||
|
LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/validates_timeliness/locale/en.yml')
|
||||||
|
|
||||||
|
class << self
|
||||||
|
|
||||||
|
def enable_datetime_select_extension!
|
||||||
|
enable_datetime_select_invalid_value_extension!
|
||||||
|
enable_multiparameter_attributes_extension!
|
||||||
|
end
|
||||||
|
|
||||||
|
def load_error_messages
|
||||||
|
defaults = YAML::load(IO.read(LOCALE_PATH))['en']
|
||||||
|
ValidatesTimeliness::Validator.error_value_formats = defaults['validates_timeliness']['error_value_formats'].symbolize_keys
|
||||||
|
|
||||||
|
if defined?(I18n)
|
||||||
|
I18n.load_path.unshift(LOCALE_PATH)
|
||||||
|
I18n.reload!
|
||||||
|
else
|
||||||
|
errors = defaults['activerecord']['errors']['messages'].inject({}) {|h,(k,v)| h[k.to_sym] = v.gsub(/\{\{\w*\}\}/, '%s');h }
|
||||||
|
::ActiveRecord::Errors.default_error_messages.update(errors)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup_for_rails
|
||||||
|
self.default_timezone = ::ActiveRecord::Base.default_timezone
|
||||||
|
self.use_time_zones = ::ActiveRecord::Base.time_zone_aware_attributes rescue false
|
||||||
|
self.enable_active_record_datetime_parser!
|
||||||
|
load_error_messages
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ValidatesTimeliness.setup_for_rails
|
||||||
|
|||||||
52
lib/validates_timeliness/action_view/instance_tag.rb
Normal file
52
lib/validates_timeliness/action_view/instance_tag.rb
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# TODO remove this from the plugin for v3.
|
||||||
|
module ValidatesTimeliness
|
||||||
|
|
||||||
|
def self.enable_datetime_select_invalid_value_extension!
|
||||||
|
::ActionView::Helpers::InstanceTag.send(:include, ValidatesTimeliness::ActionView::InstanceTag)
|
||||||
|
end
|
||||||
|
|
||||||
|
module ActionView
|
||||||
|
|
||||||
|
# Intercepts the date and time select helpers to reuse the values from the
|
||||||
|
# the params rather than the parsed value. This allows invalid date/time
|
||||||
|
# values to be redisplayed instead of blanks to aid correction by the user.
|
||||||
|
# Its a minor usability improvement which is rarely an issue for the user.
|
||||||
|
#
|
||||||
|
module InstanceTag
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
selector_method = Rails::VERSION::STRING.to_f < 2.2 ? :date_or_time_select : :datetime_selector
|
||||||
|
base.class_eval do
|
||||||
|
alias_method :datetime_selector_without_timeliness, selector_method
|
||||||
|
alias_method selector_method, :datetime_selector_with_timeliness
|
||||||
|
end
|
||||||
|
base.alias_method_chain :value, :timeliness
|
||||||
|
end
|
||||||
|
|
||||||
|
TimelinessDateTime = Struct.new(:year, :month, :day, :hour, :min, :sec)
|
||||||
|
|
||||||
|
def datetime_selector_with_timeliness(*args)
|
||||||
|
@timeliness_date_or_time_tag = true
|
||||||
|
datetime_selector_without_timeliness(*args)
|
||||||
|
end
|
||||||
|
|
||||||
|
def value_with_timeliness(object)
|
||||||
|
unless @timeliness_date_or_time_tag && @template_object.params[@object_name]
|
||||||
|
return value_without_timeliness(object)
|
||||||
|
end
|
||||||
|
|
||||||
|
pairs = @template_object.params[@object_name].select {|k,v| k =~ /^#{@method_name}\(/ }
|
||||||
|
return value_without_timeliness(object) if pairs.empty?
|
||||||
|
|
||||||
|
values = pairs.map do |(param, value)|
|
||||||
|
position = param.scan(/\(([0-9]*).*\)/).first.first
|
||||||
|
[position, value]
|
||||||
|
end.sort {|a,b| a[0] <=> b[0] }.map {|v| v[1] }
|
||||||
|
|
||||||
|
TimelinessDateTime.new(*values)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
77
lib/validates_timeliness/active_record/attribute_methods.rb
Normal file
77
lib/validates_timeliness/active_record/attribute_methods.rb
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
module ValidatesTimeliness
|
||||||
|
|
||||||
|
def self.enable_active_record_datetime_parser!
|
||||||
|
::ActiveRecord::Base.send(:include, ValidatesTimeliness::ActiveRecord::AttributeMethods)
|
||||||
|
end
|
||||||
|
|
||||||
|
module ActiveRecord
|
||||||
|
|
||||||
|
# Overrides write method for date, time and datetime columns
|
||||||
|
# to use plugin parser. Also adds mechanism to store value
|
||||||
|
# before type cast.
|
||||||
|
#
|
||||||
|
module AttributeMethods
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.extend ClassMethods
|
||||||
|
base.class_eval do
|
||||||
|
alias_method_chain :read_attribute_before_type_cast, :timeliness
|
||||||
|
class << self
|
||||||
|
alias_method_chain :define_attribute_methods, :timeliness
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def write_date_time_attribute(attr_name, value, type, time_zone_aware)
|
||||||
|
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
||||||
|
value = ValidatesTimeliness::Parser.parse(value, type)
|
||||||
|
|
||||||
|
if value && type != :date
|
||||||
|
value = value.to_time
|
||||||
|
value = value.in_time_zone if time_zone_aware
|
||||||
|
end
|
||||||
|
|
||||||
|
write_attribute(attr_name.to_sym, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_attribute_before_type_cast_with_timeliness(attr_name)
|
||||||
|
cached_attr = "_#{attr_name}_before_type_cast"
|
||||||
|
return @attributes_cache[cached_attr] if @attributes_cache.has_key?(cached_attr)
|
||||||
|
read_attribute_before_type_cast_without_timeliness(attr_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
|
||||||
|
def define_attribute_methods_with_timeliness
|
||||||
|
return if generated_methods?
|
||||||
|
timeliness_methods = []
|
||||||
|
|
||||||
|
columns_hash.each do |name, column|
|
||||||
|
if [:date, :time, :datetime].include?(column.type)
|
||||||
|
method_name = "#{name}="
|
||||||
|
next if instance_method_already_implemented?(method_name)
|
||||||
|
|
||||||
|
time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false
|
||||||
|
define_method(method_name) do |value|
|
||||||
|
write_date_time_attribute(name, value, column.type, time_zone_aware)
|
||||||
|
end
|
||||||
|
timeliness_methods << method_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Hack to get around instance_method_already_implemented? caching the
|
||||||
|
# methods in the ivar. It then appears to subsequent calls that the
|
||||||
|
# methods defined here, have not been and get defined again.
|
||||||
|
@_defined_class_methods = nil
|
||||||
|
|
||||||
|
define_attribute_methods_without_timeliness
|
||||||
|
# add generated methods which is a Set object hence no += method
|
||||||
|
timeliness_methods.each {|attr| generated_methods << attr }
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
module ValidatesTimeliness
|
||||||
|
|
||||||
|
def self.enable_multiparameter_attributes_extension!
|
||||||
|
::ActiveRecord::Base.send(:include, ValidatesTimeliness::ActiveRecord::MultiparameterAttributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
module ActiveRecord
|
||||||
|
|
||||||
|
class << self
|
||||||
|
|
||||||
|
def time_array_to_string(values, type)
|
||||||
|
values.collect! {|v| v.to_s }
|
||||||
|
|
||||||
|
case type
|
||||||
|
when :date
|
||||||
|
extract_date_from_multiparameter_attributes(values)
|
||||||
|
when :time
|
||||||
|
extract_time_from_multiparameter_attributes(values)
|
||||||
|
when :datetime
|
||||||
|
extract_date_from_multiparameter_attributes(values) + " " + extract_time_from_multiparameter_attributes(values)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def extract_date_from_multiparameter_attributes(values)
|
||||||
|
year = values[0].blank? ? nil : ValidatesTimeliness::Formats.unambiguous_year(values[0].rjust(2, "0"))
|
||||||
|
[year, *values.slice(1, 2).map { |s| s.blank? ? nil : s.rjust(2, "0") }].join("-")
|
||||||
|
end
|
||||||
|
|
||||||
|
def extract_time_from_multiparameter_attributes(values)
|
||||||
|
values[3..5].map { |s| s.blank? ? nil : s.rjust(2, "0") }.join(":")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
module MultiparameterAttributes
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
|
||||||
|
end
|
||||||
|
|
||||||
|
# Assign dates and times as formatted strings to force the use of the plugin parser
|
||||||
|
def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
|
||||||
|
errors = []
|
||||||
|
callstack.each do |name, values|
|
||||||
|
column = column_for_attribute(name)
|
||||||
|
if column && [:date, :time, :datetime].include?(column.type)
|
||||||
|
begin
|
||||||
|
callstack.delete(name)
|
||||||
|
if values.empty? || values.all?(&:nil?)
|
||||||
|
send("#{name}=", nil)
|
||||||
|
else
|
||||||
|
value = ValidatesTimeliness::ActiveRecord.time_array_to_string(values, column.type)
|
||||||
|
send("#{name}=", value)
|
||||||
|
end
|
||||||
|
rescue => ex
|
||||||
|
errors << ::ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
unless errors.empty?
|
||||||
|
raise ::ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
||||||
|
end
|
||||||
|
execute_callstack_for_multiparameter_attributes_without_timeliness(callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,150 +0,0 @@
|
|||||||
module ValidatesTimeliness
|
|
||||||
|
|
||||||
# The crux of the plugin is being able to store raw user entered values
|
|
||||||
# while not interfering with the Rails 2.1 automatic timezone handling. This
|
|
||||||
# requires us to distinguish a user entered value from a value read from the
|
|
||||||
# database. Both maybe in string form, but only the database value should be
|
|
||||||
# interpreted as being in the default timezone which is normally UTC. The user
|
|
||||||
# entered value should be interpreted as being in the current zone as indicated
|
|
||||||
# by Time.zone.
|
|
||||||
#
|
|
||||||
# To do this we must cache the user entered values on write and store the raw
|
|
||||||
# value in the attributes hash for later retrieval and possibly validation.
|
|
||||||
# Any value from the database will not be in the attribute cache on first
|
|
||||||
# read so will be considered in default timezone and converted to local time.
|
|
||||||
# It is then stored back in the attributes hash and cached to avoid the need
|
|
||||||
# for any subsequent differentiation.
|
|
||||||
#
|
|
||||||
# The wholesale replacement of the Rails time type casting is not done to
|
|
||||||
# preserve the quickest conversion for timestamp columns and also any value
|
|
||||||
# which is never changed during the life of the record object.
|
|
||||||
module AttributeMethods
|
|
||||||
|
|
||||||
def self.included(base)
|
|
||||||
base.extend ClassMethods
|
|
||||||
|
|
||||||
if Rails::VERSION::STRING < '2.1'
|
|
||||||
base.class_eval do
|
|
||||||
class << self
|
|
||||||
def create_time_zone_conversion_attribute?(name, column)
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Adds check for cached date/time attributes which have been type cast already
|
|
||||||
# and value can be used from cache. This prevents the raw date/time value from
|
|
||||||
# being type cast using default Rails type casting when writing values
|
|
||||||
# to the database.
|
|
||||||
def read_attribute(attr_name)
|
|
||||||
attr_name = attr_name.to_s
|
|
||||||
if !(value = @attributes[attr_name]).nil?
|
|
||||||
if column = column_for_attribute(attr_name)
|
|
||||||
if unserializable_attribute?(attr_name, column)
|
|
||||||
unserialize_attribute(attr_name)
|
|
||||||
elsif [:date, :time, :datetime].include?(column.type) && @attributes_cache.has_key?(attr_name)
|
|
||||||
@attributes_cache[attr_name]
|
|
||||||
else
|
|
||||||
column.type_cast(value)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
value
|
|
||||||
end
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Writes attribute value by storing raw value in attributes hash,
|
|
||||||
# then convert it with parser and cache it.
|
|
||||||
#
|
|
||||||
# If Rails dirty attributes is enabled then the value is added to
|
|
||||||
# changed attributes if changed. Can't use the default dirty checking
|
|
||||||
# implementation as it chains the write_attribute method which deletes
|
|
||||||
# the attribute from the cache.
|
|
||||||
def write_date_time_attribute(attr_name, value)
|
|
||||||
attr_name = attr_name.to_s
|
|
||||||
column = column_for_attribute(attr_name)
|
|
||||||
old = read_attribute(attr_name) if defined?(ActiveRecord::Dirty)
|
|
||||||
new = self.class.parse_date_time(value, column.type)
|
|
||||||
|
|
||||||
if self.class.send(:create_time_zone_conversion_attribute?, attr_name, column)
|
|
||||||
new = new.in_time_zone rescue nil
|
|
||||||
end
|
|
||||||
@attributes_cache[attr_name] = new
|
|
||||||
|
|
||||||
if defined?(ActiveRecord::Dirty) && !changed_attributes.include?(attr_name) && old != new
|
|
||||||
changed_attributes[attr_name] = (old.duplicable? ? old.clone : old)
|
|
||||||
end
|
|
||||||
@attributes[attr_name] = value
|
|
||||||
end
|
|
||||||
|
|
||||||
module ClassMethods
|
|
||||||
|
|
||||||
# Override AR method to define attribute reader and writer method for
|
|
||||||
# date, time and datetime attributes to use plugin parser.
|
|
||||||
def define_attribute_methods
|
|
||||||
return if generated_methods?
|
|
||||||
columns_hash.each do |name, column|
|
|
||||||
unless instance_method_already_implemented?(name)
|
|
||||||
if self.serialized_attributes[name]
|
|
||||||
define_read_method_for_serialized_attribute(name)
|
|
||||||
elsif create_time_zone_conversion_attribute?(name, column)
|
|
||||||
define_read_method_for_time_zone_conversion(name.to_sym)
|
|
||||||
else
|
|
||||||
define_read_method(name.to_sym, name, column)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
unless instance_method_already_implemented?("#{name}=")
|
|
||||||
if [:date, :time, :datetime].include?(column.type)
|
|
||||||
define_write_method_for_dates_and_times(name.to_sym)
|
|
||||||
else
|
|
||||||
define_write_method(name.to_sym)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
unless instance_method_already_implemented?("#{name}?")
|
|
||||||
define_question_method(name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Define write method for date, time and datetime columns
|
|
||||||
def define_write_method_for_dates_and_times(attr_name)
|
|
||||||
method_body = <<-EOV
|
|
||||||
def #{attr_name}=(value)
|
|
||||||
write_date_time_attribute('#{attr_name}', value)
|
|
||||||
end
|
|
||||||
EOV
|
|
||||||
evaluate_attribute_method attr_name, method_body
|
|
||||||
end
|
|
||||||
|
|
||||||
# Define time attribute reader. If reloading then check if cached,
|
|
||||||
# which means its in local time. If local, convert with parser as local
|
|
||||||
# timezone, otherwise use read_attribute method for quick default type
|
|
||||||
# cast of values from database using default timezone.
|
|
||||||
def define_read_method_for_time_zone_conversion(attr_name)
|
|
||||||
method_body = <<-EOV
|
|
||||||
def #{attr_name}(reload = false)
|
|
||||||
cached = @attributes_cache['#{attr_name}']
|
|
||||||
return cached if @attributes_cache.has_key?('#{attr_name}') && !reload
|
|
||||||
if @attributes_cache.has_key?('#{attr_name}')
|
|
||||||
time = read_attribute_before_type_cast('#{attr_name}')
|
|
||||||
time = self.class.parse_date_time(date, :datetime)
|
|
||||||
else
|
|
||||||
time = read_attribute('#{attr_name}')
|
|
||||||
@attributes['#{attr_name}'] = time.in_time_zone rescue nil
|
|
||||||
end
|
|
||||||
@attributes_cache['#{attr_name}'] = time.in_time_zone rescue nil
|
|
||||||
end
|
|
||||||
EOV
|
|
||||||
evaluate_attribute_method attr_name, method_body
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
module ValidatesTimeliness
|
|
||||||
module CoreExtensions
|
|
||||||
module Date
|
|
||||||
|
|
||||||
def to_dummy_time
|
|
||||||
::Time.mktime(2000, 1, 1, 0, 0, 0)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
module ValidatesTimeliness
|
|
||||||
module CoreExtensions
|
|
||||||
module DateTime
|
|
||||||
|
|
||||||
def to_dummy_time
|
|
||||||
::Time.mktime(2000, 1, 1, hour, min, sec)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
module ValidatesTimeliness
|
|
||||||
module CoreExtensions
|
|
||||||
module Time
|
|
||||||
|
|
||||||
def to_dummy_time
|
|
||||||
self.class.mktime(2000, 1, 1, hour, min, sec)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,27 +1,45 @@
|
|||||||
|
require 'date'
|
||||||
|
|
||||||
module ValidatesTimeliness
|
module ValidatesTimeliness
|
||||||
|
|
||||||
# A date and time format regular expression generator. Allows you to
|
# A date and time parsing library which allows you to add custom formats using
|
||||||
# construct a date, time or datetime format using predefined tokens in
|
# simple predefined tokens. This makes it much easier to catalogue and customize
|
||||||
# a string. This makes it much easier to catalogue and customize the formats
|
# the formats rather than dealing directly with regular expressions.
|
||||||
# rather than dealing directly with regular expressions. The formats are then
|
|
||||||
# compiled into regular expressions for use validating date or time strings.
|
|
||||||
#
|
#
|
||||||
# Formats can be added or removed to customize the set of valid date or time
|
# Formats can be added or removed to customize the set of valid date or time
|
||||||
# string values.
|
# string values.
|
||||||
#
|
#
|
||||||
class Formats
|
class Formats
|
||||||
cattr_accessor :time_formats
|
cattr_accessor :time_formats,
|
||||||
cattr_accessor :date_formats
|
:date_formats,
|
||||||
cattr_accessor :datetime_formats
|
:datetime_formats,
|
||||||
|
:time_expressions,
|
||||||
cattr_accessor :time_expressions
|
:date_expressions,
|
||||||
cattr_accessor :date_expressions
|
:datetime_expressions,
|
||||||
cattr_accessor :datetime_expressions
|
:format_tokens,
|
||||||
|
:format_proc_args
|
||||||
cattr_accessor :format_tokens
|
|
||||||
cattr_accessor :format_proc_args
|
|
||||||
|
# Set the threshold value for a two digit year to be considered last century
|
||||||
# Format tokens:
|
#
|
||||||
|
# Default: 30
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
# year = '29' is considered 2029
|
||||||
|
# year = '30' is considered 1930
|
||||||
|
#
|
||||||
|
cattr_accessor :ambiguous_year_threshold
|
||||||
|
self.ambiguous_year_threshold = 30
|
||||||
|
|
||||||
|
# Set the dummy date part for a time type value. Should be an array of 3 values
|
||||||
|
# being year, month and day in that order.
|
||||||
|
#
|
||||||
|
# Default: [ 2000, 1, 1 ] same as ActiveRecord
|
||||||
|
#
|
||||||
|
cattr_accessor :dummy_date_for_time_type
|
||||||
|
self.dummy_date_for_time_type = [ 2000, 1, 1 ]
|
||||||
|
|
||||||
|
# Format tokens:
|
||||||
# y = year
|
# y = year
|
||||||
# m = month
|
# m = month
|
||||||
# d = day
|
# d = day
|
||||||
@@ -36,25 +54,25 @@ module ValidatesTimeliness
|
|||||||
#
|
#
|
||||||
# All other characters are considered literal. You can embed regexp in the
|
# All other characters are considered literal. You can embed regexp in the
|
||||||
# format but no gurantees that it will remain intact. If you avoid the use
|
# format but no gurantees that it will remain intact. If you avoid the use
|
||||||
# of any token characters and regexp dots or backslashes as special characters
|
# of any token characters and regexp dots or backslashes as special characters
|
||||||
# in the regexp, it may well work as expected. For special characters use
|
# in the regexp, it may well work as expected. For special characters use
|
||||||
# POSIX character clsses for safety.
|
# POSIX character clsses for safety.
|
||||||
#
|
#
|
||||||
# Repeating tokens:
|
# Repeating tokens:
|
||||||
# x = 1 or 2 digits for unit (e.g. 'h' means an hour can be '9' or '09')
|
# x = 1 or 2 digits for unit (e.g. 'h' means an hour can be '9' or '09')
|
||||||
# xx = 2 digits exactly for unit (e.g. 'hh' means an hour can only be '09')
|
# xx = 2 digits exactly for unit (e.g. 'hh' means an hour can only be '09')
|
||||||
#
|
#
|
||||||
# Special Cases:
|
# Special Cases:
|
||||||
# yy = 2 or 4 digit year
|
# yy = 2 or 4 digit year
|
||||||
# yyyyy = exactly 4 digit year
|
# yyyy = exactly 4 digit year
|
||||||
# mmm = month long name (e.g. 'Jul' or 'July')
|
# mmm = month long name (e.g. 'Jul' or 'July')
|
||||||
# ddd = Day name of 3 to 9 letters (e.g. Wed or Wednesday)
|
# ddd = Day name of 3 to 9 letters (e.g. Wed or Wednesday)
|
||||||
# u = microseconds matches 1 to 6 digits
|
# u = microseconds matches 1 to 6 digits
|
||||||
#
|
#
|
||||||
# Any other invalid combination of repeating tokens will be swallowed up
|
# Any other invalid combination of repeating tokens will be swallowed up
|
||||||
# by the next lowest length valid repeating token (e.g. yyy will be
|
# by the next lowest length valid repeating token (e.g. yyy will be
|
||||||
# replaced with yy)
|
# replaced with yy)
|
||||||
|
|
||||||
@@time_formats = [
|
@@time_formats = [
|
||||||
'hh:nn:ss',
|
'hh:nn:ss',
|
||||||
'hh-nn-ss',
|
'hh-nn-ss',
|
||||||
@@ -68,7 +86,7 @@ module ValidatesTimeliness
|
|||||||
'h-nn_ampm',
|
'h-nn_ampm',
|
||||||
'h_ampm'
|
'h_ampm'
|
||||||
]
|
]
|
||||||
|
|
||||||
@@date_formats = [
|
@@date_formats = [
|
||||||
'yyyy-mm-dd',
|
'yyyy-mm-dd',
|
||||||
'yyyy/mm/dd',
|
'yyyy/mm/dd',
|
||||||
@@ -81,10 +99,11 @@ module ValidatesTimeliness
|
|||||||
'd.m.yy',
|
'd.m.yy',
|
||||||
'd mmm yy'
|
'd mmm yy'
|
||||||
]
|
]
|
||||||
|
|
||||||
@@datetime_formats = [
|
@@datetime_formats = [
|
||||||
'yyyy-mm-dd hh:nn:ss',
|
'yyyy-mm-dd hh:nn:ss',
|
||||||
'yyyy-mm-dd h:nn',
|
'yyyy-mm-dd h:nn',
|
||||||
|
'yyyy-mm-dd h:nn_ampm',
|
||||||
'yyyy-mm-dd hh:nn:ss.u',
|
'yyyy-mm-dd hh:nn:ss.u',
|
||||||
'm/d/yy h:nn:ss',
|
'm/d/yy h:nn:ss',
|
||||||
'm/d/yy h:nn_ampm',
|
'm/d/yy h:nn_ampm',
|
||||||
@@ -94,14 +113,15 @@ module ValidatesTimeliness
|
|||||||
'd/m/yy h:nn',
|
'd/m/yy h:nn',
|
||||||
'ddd, dd mmm yyyy hh:nn:ss (zo|tz)', # RFC 822
|
'ddd, dd mmm yyyy hh:nn:ss (zo|tz)', # RFC 822
|
||||||
'ddd mmm d hh:nn:ss zo yyyy', # Ruby time string
|
'ddd mmm d hh:nn:ss zo yyyy', # Ruby time string
|
||||||
'yyyy-mm-ddThh:nn:ss(?:Z|zo)' # iso 8601
|
'yyyy-mm-ddThh:nn:ssZ', # iso 8601 without zone offset
|
||||||
|
'yyyy-mm-ddThh:nn:sszo' # iso 8601 with zone offset
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# All tokens available for format construction. The token array is made of
|
# All tokens available for format construction. The token array is made of
|
||||||
# token regexp, validation regexp and key for format proc mapping if any.
|
# token regexp, validation regexp and key for format proc mapping if any.
|
||||||
# If the token needs no format proc arg then the validation regexp should
|
# If the token needs no format proc arg then the validation regexp should
|
||||||
# not have a capturing group, as all captured groups are passed to the
|
# not have a capturing group, as all captured groups are passed to the
|
||||||
# format proc.
|
# format proc.
|
||||||
#
|
#
|
||||||
# The token regexp should only use a capture group if 'look-behind' anchor
|
# The token regexp should only use a capture group if 'look-behind' anchor
|
||||||
@@ -115,7 +135,7 @@ module ValidatesTimeliness
|
|||||||
{ 'mm' => [ /m{2}/, '(\d{2})', :month ] },
|
{ 'mm' => [ /m{2}/, '(\d{2})', :month ] },
|
||||||
{ 'm' => [ /(\A|[^ap])m{1}/, '(\d{1,2})', :month ] },
|
{ 'm' => [ /(\A|[^ap])m{1}/, '(\d{1,2})', :month ] },
|
||||||
{ 'yyyy' => [ /y{4,}/, '(\d{4})', :year ] },
|
{ 'yyyy' => [ /y{4,}/, '(\d{4})', :year ] },
|
||||||
{ 'yy' => [ /y{2,}/, '(\d{2}|\d{4})', :year ] },
|
{ 'yy' => [ /y{2,}/, '(\d{4}|\d{2})', :year ] },
|
||||||
{ 'hh' => [ /h{2,}/, '(\d{2})', :hour ] },
|
{ 'hh' => [ /h{2,}/, '(\d{2})', :hour ] },
|
||||||
{ 'h' => [ /h{1}/, '(\d{1,2})', :hour ] },
|
{ 'h' => [ /h{1}/, '(\d{1,2})', :hour ] },
|
||||||
{ 'nn' => [ /n{2,}/, '(\d{2})', :min ] },
|
{ 'nn' => [ /n{2,}/, '(\d{2})', :min ] },
|
||||||
@@ -124,58 +144,73 @@ module ValidatesTimeliness
|
|||||||
{ 's' => [ /s{1}/, '(\d{1,2})', :sec ] },
|
{ 's' => [ /s{1}/, '(\d{1,2})', :sec ] },
|
||||||
{ 'u' => [ /u{1,}/, '(\d{1,6})', :usec ] },
|
{ 'u' => [ /u{1,}/, '(\d{1,6})', :usec ] },
|
||||||
{ 'ampm' => [ /ampm/, '((?:[aApP])\.?[mM]\.?)', :meridian ] },
|
{ 'ampm' => [ /ampm/, '((?:[aApP])\.?[mM]\.?)', :meridian ] },
|
||||||
{ 'zo' => [ /zo/, '(?:[+-]\d{2}:?\d{2})'] },
|
{ 'zo' => [ /zo/, '([+-]\d{2}:?\d{2})', :offset ] },
|
||||||
{ 'tz' => [ /tz/, '(?:[A-Z]{1,4})' ] },
|
{ 'tz' => [ /tz/, '(?:[A-Z]{1,4})' ] },
|
||||||
{ '_' => [ /_/, '\s?' ] }
|
{ '_' => [ /_/, '\s?' ] }
|
||||||
]
|
]
|
||||||
|
|
||||||
# Arguments whichs will be passed to the format proc if matched in the
|
# Arguments which will be passed to the format proc if matched in the
|
||||||
# time string. The key must should the key from the format tokens. The array
|
# time string. The key must be the key from the format tokens. The array
|
||||||
# consists of the arry position of the arg, the arg name, and the code to
|
# consists of the arry position of the arg, the arg name, and the code to
|
||||||
# place in the time array slot. The position can be nil which means the arg
|
# place in the time array slot. The position can be nil which means the arg
|
||||||
# won't be placed in the array.
|
# won't be placed in the array.
|
||||||
#
|
#
|
||||||
# The code can be used to manipulate the arg value if required, otherwise
|
# The code can be used to manipulate the arg value if required, otherwise
|
||||||
# should just be the arg name.
|
# should just be the arg name.
|
||||||
#
|
#
|
||||||
@@format_proc_args = {
|
@@format_proc_args = {
|
||||||
:year => [0, 'y', 'unambiguous_year(y)'],
|
:year => [0, 'y', 'unambiguous_year(y)'],
|
||||||
:month => [1, 'm', 'month_index(m)'],
|
:month => [1, 'm', 'month_index(m)'],
|
||||||
:day => [2, 'd', 'd'],
|
:day => [2, 'd', 'd'],
|
||||||
:hour => [3, 'h', 'full_hour(h,md)'],
|
:hour => [3, 'h', 'full_hour(h,md)'],
|
||||||
:min => [4, 'n', 'n'],
|
:min => [4, 'n', 'n'],
|
||||||
:sec => [5, 's', 's'],
|
:sec => [5, 's', 's'],
|
||||||
:usec => [6, 'u', 'microseconds(u)'],
|
:usec => [6, 'u', 'microseconds(u)'],
|
||||||
|
:offset => [7, 'z', 'offset_in_seconds(z)'],
|
||||||
:meridian => [nil, 'md', nil]
|
:meridian => [nil, 'md', nil]
|
||||||
}
|
}
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
def compile_format_expressions
|
def compile_format_expressions
|
||||||
@@time_expressions = compile_formats(@@time_formats)
|
@@time_expressions = compile_formats(@@time_formats)
|
||||||
@@date_expressions = compile_formats(@@date_formats)
|
@@date_expressions = compile_formats(@@date_formats)
|
||||||
@@datetime_expressions = compile_formats(@@datetime_formats)
|
@@datetime_expressions = compile_formats(@@datetime_formats)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Loop through format expressions for type and call proc on matches. Allow
|
# Loop through format expressions for type and call proc on matches. Allow
|
||||||
# pre or post match strings to exist if strict is false. Otherwise wrap
|
# pre or post match strings to exist if strict is false. Otherwise wrap
|
||||||
# regexp in start and end anchors.
|
# regexp in start and end anchors.
|
||||||
# Returns 7 part time array.
|
# Returns time array if matches a format, nil otherwise.
|
||||||
def parse(string, type, strict=true)
|
def parse(string, type, options={})
|
||||||
return string unless string.is_a?(String)
|
return string unless string.is_a?(String)
|
||||||
|
options.reverse_merge!(:strict => true)
|
||||||
expressions = expression_set(type, string)
|
|
||||||
time_array = nil
|
sets = if options[:format]
|
||||||
expressions.each do |(regexp, processor)|
|
options[:strict] = true
|
||||||
regexp = strict || type == :datetime ? /\A#{regexp}\Z/ : (type == :date ? /\A#{regexp}/ : /#{regexp}\Z/)
|
[ send("#{type}_expressions").assoc(options[:format]) ]
|
||||||
if matches = regexp.match(string.strip)
|
else
|
||||||
time_array = processor.call(*matches[1..7])
|
expression_set(type, string)
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return time_array
|
|
||||||
end
|
matches = nil
|
||||||
|
processor = sets.each do |format, regexp, proc|
|
||||||
|
full = /\A#{regexp}\Z/ if options[:strict]
|
||||||
|
full ||= case type
|
||||||
|
when :date then /\A#{regexp}/
|
||||||
|
when :time then /#{regexp}\Z/
|
||||||
|
when :datetime then /\A#{regexp}\Z/
|
||||||
|
end
|
||||||
|
break(proc) if matches = full.match(string.strip)
|
||||||
|
end
|
||||||
|
last = options[:include_offset] ? 8 : 7
|
||||||
|
if matches
|
||||||
|
values = processor.call(*matches[1..last])
|
||||||
|
values[0..2] = dummy_date_for_time_type if type == :time
|
||||||
|
return values
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Delete formats of specified type. Error raised if format not found.
|
# Delete formats of specified type. Error raised if format not found.
|
||||||
def remove_formats(type, *remove_formats)
|
def remove_formats(type, *remove_formats)
|
||||||
remove_formats.each do |format|
|
remove_formats.each do |format|
|
||||||
@@ -185,10 +220,10 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
compile_format_expressions
|
compile_format_expressions
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds new formats. Must specify format type and can specify a :before
|
# Adds new formats. Must specify format type and can specify a :before
|
||||||
# option to nominate which format the new formats should be inserted in
|
# option to nominate which format the new formats should be inserted in
|
||||||
# front on to take higher precedence.
|
# front on to take higher precedence.
|
||||||
# Error is raised if format already exists or if :before format is not found.
|
# Error is raised if format already exists or if :before format is not found.
|
||||||
def add_formats(type, *add_formats)
|
def add_formats(type, *add_formats)
|
||||||
formats = self.send("#{type}_formats")
|
formats = self.send("#{type}_formats")
|
||||||
@@ -196,7 +231,7 @@ module ValidatesTimeliness
|
|||||||
options = add_formats.pop if add_formats.last.is_a?(Hash)
|
options = add_formats.pop if add_formats.last.is_a?(Hash)
|
||||||
before = options[:before]
|
before = options[:before]
|
||||||
raise "Format for :before option #{format} was not found." if before && !formats.include?(before)
|
raise "Format for :before option #{format} was not found." if before && !formats.include?(before)
|
||||||
|
|
||||||
add_formats.each do |format|
|
add_formats.each do |format|
|
||||||
raise "Format #{format} is already included in #{type} formats" if formats.include?(format)
|
raise "Format #{format} is already included in #{type} formats" if formats.include?(format)
|
||||||
|
|
||||||
@@ -205,10 +240,9 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
compile_format_expressions
|
compile_format_expressions
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Removes formats where the 1 or 2 digit month comes first, to eliminate
|
# Removes formats where the 1 or 2 digit month comes first, to eliminate
|
||||||
# formats which are ambiguous with the European style of day then month.
|
# formats which are ambiguous with the European style of day then month.
|
||||||
# The mmm token is ignored as its not ambigous.
|
# The mmm token is ignored as its not ambigous.
|
||||||
def remove_us_formats
|
def remove_us_formats
|
||||||
us_format_regexp = /\Am{1,2}[^m]/
|
us_format_regexp = /\Am{1,2}[^m]/
|
||||||
@@ -216,72 +250,7 @@ module ValidatesTimeliness
|
|||||||
datetime_formats.reject! { |format| us_format_regexp =~ format }
|
datetime_formats.reject! { |format| us_format_regexp =~ format }
|
||||||
compile_format_expressions
|
compile_format_expressions
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
# Compile formats into validation regexps and format procs
|
|
||||||
def format_expression_generator(string_format)
|
|
||||||
regexp = string_format.dup
|
|
||||||
order = {}
|
|
||||||
regexp.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes ]/
|
|
||||||
|
|
||||||
format_tokens.each do |token|
|
|
||||||
token_name = token.keys.first
|
|
||||||
token_regexp, regexp_str, arg_key = *token.values.first
|
|
||||||
|
|
||||||
# hack for lack of look-behinds. If has a capture group then is
|
|
||||||
# considered an anchor to put straight back in the regexp string.
|
|
||||||
regexp.gsub!(token_regexp) {|m| "#{$1}" + regexp_str }
|
|
||||||
order[arg_key] = $~.begin(0) if $~ && !arg_key.nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
return Regexp.new(regexp), format_proc(order)
|
|
||||||
rescue
|
|
||||||
puts "The following format regular expression failed to compile: #{regexp}\n from format #{string_format}."
|
|
||||||
raise
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generates a proc which when executed maps the regexp capture groups to a
|
|
||||||
# proc argument based on order captured. A time array is built using the proc
|
|
||||||
# argument in the position indicated by the first element of the proc arg
|
|
||||||
# array.
|
|
||||||
#
|
|
||||||
# Examples:
|
|
||||||
#
|
|
||||||
# 'yyyy-mm-dd hh:nn' => lambda {|y,m,d,h,n| md||=0; [unambiguous_year(y),month_index(m),d,full_hour(h,md),n,nil,nil].map {|i| i.to_i } }
|
|
||||||
# 'dd/mm/yyyy h:nn_ampm' => lambda {|d,m,y,h,n,md| md||=0; [unambiguous_year(y),month_index(m),d,full_hour(h,md),n,nil,nil].map {|i| i.to_i } }
|
|
||||||
#
|
|
||||||
def format_proc(order)
|
|
||||||
arg_map = format_proc_args
|
|
||||||
args = order.invert.sort.map {|p| arg_map[p[1]][1] }
|
|
||||||
arr = [nil] * 7
|
|
||||||
order.keys.each {|k| i = arg_map[k][0]; arr[i] = arg_map[k][2] unless i.nil? }
|
|
||||||
proc_string = "lambda {|#{args.join(',')}| md||=nil; [#{arr.map {|i| i.nil? ? 'nil' : i }.join(',')}].map {|i| i.to_i } }"
|
|
||||||
eval proc_string
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile_formats(formats)
|
|
||||||
formats.collect { |format| regexp, format_proc = format_expression_generator(format) }
|
|
||||||
end
|
|
||||||
|
|
||||||
# Pick expression set and combine date and datetimes for
|
|
||||||
# datetime attributes to allow date string as datetime
|
|
||||||
def expression_set(type, string)
|
|
||||||
case type
|
|
||||||
when :date
|
|
||||||
date_expressions
|
|
||||||
when :time
|
|
||||||
time_expressions
|
|
||||||
when :datetime
|
|
||||||
# gives a speed-up for date string as datetime attributes
|
|
||||||
if string.length < 11
|
|
||||||
date_expressions + datetime_expressions
|
|
||||||
else
|
|
||||||
datetime_expressions + date_expressions
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def full_hour(hour, meridian)
|
def full_hour(hour, meridian)
|
||||||
hour = hour.to_i
|
hour = hour.to_i
|
||||||
return hour if meridian.nil?
|
return hour if meridian.nil?
|
||||||
@@ -291,20 +260,106 @@ module ValidatesTimeliness
|
|||||||
hour == 12 ? hour : hour + 12
|
hour == 12 ? hour : hour + 12
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def unambiguous_year(year, threshold=30)
|
def unambiguous_year(year)
|
||||||
year = "#{year.to_i < threshold ? '20' : '19'}#{year}" if year.length == 2
|
if year.length <= 2
|
||||||
|
century = Time.now.year.to_s[0..1].to_i
|
||||||
|
century -= 1 if year.to_i >= ambiguous_year_threshold
|
||||||
|
year = "#{century}#{year.rjust(2,'0')}"
|
||||||
|
end
|
||||||
year.to_i
|
year.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def month_index(month)
|
def month_index(month)
|
||||||
return month.to_i if month.to_i.nonzero?
|
return month.to_i if month.to_i.nonzero?
|
||||||
Date::ABBR_MONTHNAMES.index(month.capitalize) || Date::MONTHNAMES.index(month.capitalize)
|
abbr_month_names.index(month.capitalize) || month_names.index(month.capitalize)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def month_names
|
||||||
|
defined?(I18n) ? I18n.t('date.month_names') : Date::MONTHNAMES
|
||||||
|
end
|
||||||
|
|
||||||
|
def abbr_month_names
|
||||||
|
defined?(I18n) ? I18n.t('date.abbr_month_names') : Date::ABBR_MONTHNAMES
|
||||||
|
end
|
||||||
|
|
||||||
def microseconds(usec)
|
def microseconds(usec)
|
||||||
(".#{usec}".to_f * 1_000_000).to_i
|
(".#{usec}".to_f * 1_000_000).to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def offset_in_seconds(offset)
|
||||||
|
sign = offset =~ /^-/ ? -1 : 1
|
||||||
|
parts = offset.scan(/\d\d/).map {|p| p.to_f }
|
||||||
|
parts[1] = parts[1].to_f / 60
|
||||||
|
(parts[0] + parts[1]) * sign * 3600
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# Generate regular expression and processor from format string
|
||||||
|
def generate_format_expression(string_format)
|
||||||
|
regexp = string_format.dup
|
||||||
|
order = {}
|
||||||
|
regexp.gsub!(/([\.\\])/, '\\\\\1') # escapes dots and backslashes
|
||||||
|
|
||||||
|
format_tokens.each do |token|
|
||||||
|
token_name = token.keys.first
|
||||||
|
token_regexp, regexp_str, arg_key = *token.values.first
|
||||||
|
|
||||||
|
# hack for lack of look-behinds. If has a capture group then is
|
||||||
|
# considered an anchor to put straight back in the regexp string.
|
||||||
|
regexp.gsub!(token_regexp) {|m| "#{$1}" + regexp_str }
|
||||||
|
order[arg_key] = $~.begin(0) if $~ && !arg_key.nil?
|
||||||
|
end
|
||||||
|
|
||||||
|
return Regexp.new(regexp), format_proc(order)
|
||||||
|
rescue
|
||||||
|
raise "The following format regular expression failed to compile: #{regexp}\n from format #{string_format}."
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generates a proc which when executed maps the regexp capture groups to a
|
||||||
|
# proc argument based on order captured. A time array is built using the proc
|
||||||
|
# argument in the position indicated by the first element of the proc arg
|
||||||
|
# array.
|
||||||
|
#
|
||||||
|
def format_proc(order)
|
||||||
|
arg_map = format_proc_args
|
||||||
|
args = order.invert.sort.map {|p| arg_map[p[1]][1] }
|
||||||
|
arr = [nil] * 7
|
||||||
|
order.keys.each {|k| i = arg_map[k][0]; arr[i] = arg_map[k][2] unless i.nil? }
|
||||||
|
proc_string = <<-EOL
|
||||||
|
lambda {|#{args.join(',')}|
|
||||||
|
md ||= nil
|
||||||
|
[#{arr.map {|i| i.nil? ? 'nil' : i }.join(',')}].map {|i| i.is_a?(Float) ? i : i.to_i }
|
||||||
|
}
|
||||||
|
EOL
|
||||||
|
eval proc_string
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile_formats(formats)
|
||||||
|
formats.map { |format| [ format, *generate_format_expression(format) ] }
|
||||||
|
end
|
||||||
|
|
||||||
|
# Pick expression set and combine date and datetimes for
|
||||||
|
# datetime attributes to allow date string as datetime
|
||||||
|
def expression_set(type, string)
|
||||||
|
case type
|
||||||
|
when :date
|
||||||
|
date_expressions
|
||||||
|
when :time
|
||||||
|
time_expressions
|
||||||
|
when :datetime
|
||||||
|
# gives a speed-up for date string as datetime attributes
|
||||||
|
if string.length < 11
|
||||||
|
date_expressions + datetime_expressions
|
||||||
|
else
|
||||||
|
datetime_expressions + date_expressions
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ValidatesTimeliness::Formats.compile_format_expressions
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
module ValidatesTimeliness
|
|
||||||
|
|
||||||
# Intercepts the date and time select helpers to allow the
|
|
||||||
# attribute value before type cast to be used as in the select helpers.
|
|
||||||
# This means that an invalid date or time will be redisplayed rather than the
|
|
||||||
# type cast value which would be nil if invalid.
|
|
||||||
module InstanceTag
|
|
||||||
|
|
||||||
def self.included(base)
|
|
||||||
selector_method = Rails::VERSION::STRING < '2.2' ? :date_or_time_select : :datetime_selector
|
|
||||||
base.class_eval do
|
|
||||||
alias_method :datetime_selector_without_timeliness, selector_method
|
|
||||||
alias_method selector_method, :datetime_selector_with_timeliness
|
|
||||||
end
|
|
||||||
base.alias_method_chain :value, :timeliness
|
|
||||||
end
|
|
||||||
|
|
||||||
TimelinessDateTime = Struct.new(:year, :month, :day, :hour, :min, :sec)
|
|
||||||
|
|
||||||
def datetime_selector_with_timeliness(*args)
|
|
||||||
@timeliness_date_or_time_tag = true
|
|
||||||
datetime_selector_without_timeliness(*args)
|
|
||||||
end
|
|
||||||
|
|
||||||
def value_with_timeliness(object)
|
|
||||||
return value_without_timeliness(object) unless @timeliness_date_or_time_tag
|
|
||||||
|
|
||||||
raw_value = value_before_type_cast(object)
|
|
||||||
|
|
||||||
if raw_value.nil? || raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
|
||||||
return value_without_timeliness(object)
|
|
||||||
end
|
|
||||||
|
|
||||||
time_array = ParseDate.parsedate(raw_value)
|
|
||||||
|
|
||||||
TimelinessDateTime.new(*time_array[0..5])
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
18
lib/validates_timeliness/locale/en.yml
Normal file
18
lib/validates_timeliness/locale/en.yml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
en:
|
||||||
|
activerecord:
|
||||||
|
errors:
|
||||||
|
messages:
|
||||||
|
invalid_date: "is not a valid date"
|
||||||
|
invalid_time: "is not a valid time"
|
||||||
|
invalid_datetime: "is not a valid datetime"
|
||||||
|
is_at: "must be at {{restriction}}"
|
||||||
|
before: "must be before {{restriction}}"
|
||||||
|
on_or_before: "must be on or before {{restriction}}"
|
||||||
|
after: "must be after {{restriction}}"
|
||||||
|
on_or_after: "must be on or after {{restriction}}"
|
||||||
|
between: "must be between {{earliest}} and {{latest}}"
|
||||||
|
validates_timeliness:
|
||||||
|
error_value_formats:
|
||||||
|
date: '%Y-%m-%d'
|
||||||
|
time: '%H:%M:%S'
|
||||||
|
datetime: '%Y-%m-%d %H:%M:%S'
|
||||||
1
lib/validates_timeliness/matcher.rb
Normal file
1
lib/validates_timeliness/matcher.rb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
require 'validates_timeliness/spec/rails/matchers/validate_timeliness'
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
module ValidatesTimeliness
|
|
||||||
module MultiparameterAttributes
|
|
||||||
|
|
||||||
def self.included(base)
|
|
||||||
base.alias_method_chain :execute_callstack_for_multiparameter_attributes, :timeliness
|
|
||||||
end
|
|
||||||
|
|
||||||
# Overrides AR method to store multiparameter time and dates as string
|
|
||||||
# allowing validation later.
|
|
||||||
def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
|
|
||||||
errors = []
|
|
||||||
callstack.each do |name, values|
|
|
||||||
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
|
|
||||||
if values.empty?
|
|
||||||
send(name + "=", nil)
|
|
||||||
else
|
|
||||||
column = column_for_attribute(name)
|
|
||||||
begin
|
|
||||||
value = if [:date, :time, :datetime].include?(column.type)
|
|
||||||
time_array_to_string(values, column.type)
|
|
||||||
else
|
|
||||||
klass.new(*values)
|
|
||||||
end
|
|
||||||
send(name + "=", value)
|
|
||||||
rescue => ex
|
|
||||||
errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
unless errors.empty?
|
|
||||||
raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def time_array_to_string(values, type)
|
|
||||||
values = values.map(&:to_s)
|
|
||||||
|
|
||||||
case type
|
|
||||||
when :date
|
|
||||||
extract_date_from_multiparameter_attributes(values)
|
|
||||||
when :time
|
|
||||||
extract_time_from_multiparameter_attributes(values)
|
|
||||||
when :datetime
|
|
||||||
date_values, time_values = values.slice!(0, 3), values
|
|
||||||
extract_date_from_multiparameter_attributes(date_values) + " " + extract_time_from_multiparameter_attributes(time_values)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def extract_date_from_multiparameter_attributes(values)
|
|
||||||
[values[0], *values.slice(1, 2).map { |s| s.rjust(2, "0") }].join("-")
|
|
||||||
end
|
|
||||||
|
|
||||||
def extract_time_from_multiparameter_attributes(values)
|
|
||||||
values.last(3).map { |s| s.rjust(2, "0") }.join(":")
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
44
lib/validates_timeliness/parser.rb
Normal file
44
lib/validates_timeliness/parser.rb
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
module ValidatesTimeliness
|
||||||
|
module Parser
|
||||||
|
|
||||||
|
class << self
|
||||||
|
|
||||||
|
def parse(raw_value, type, options={})
|
||||||
|
return nil if raw_value.blank?
|
||||||
|
return raw_value if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
||||||
|
|
||||||
|
time_array = ValidatesTimeliness::Formats.parse(raw_value, type, options.reverse_merge(:strict => true))
|
||||||
|
return nil if time_array.nil?
|
||||||
|
|
||||||
|
if type == :date
|
||||||
|
Date.new(*time_array[0..2]) rescue nil
|
||||||
|
else
|
||||||
|
make_time(time_array[0..7])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def make_time(time_array)
|
||||||
|
# Enforce date part validity which Time class does not
|
||||||
|
return nil unless Date.valid_civil?(*time_array[0..2])
|
||||||
|
|
||||||
|
if Time.respond_to?(:zone) && ValidatesTimeliness.use_time_zones
|
||||||
|
Time.zone.local(*time_array)
|
||||||
|
else
|
||||||
|
# Older AR way of handling times with datetime fallback
|
||||||
|
begin
|
||||||
|
time_zone = ValidatesTimeliness.default_timezone
|
||||||
|
Time.send(time_zone, *time_array)
|
||||||
|
rescue ArgumentError, TypeError
|
||||||
|
zone_offset = time_zone == :local ? DateTime.local_offset : 0
|
||||||
|
time_array.pop # remove microseconds
|
||||||
|
DateTime.civil(*(time_array << zone_offset))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rescue ArgumentError, TypeError
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
module Spec
|
||||||
|
module Rails
|
||||||
|
module Matchers
|
||||||
|
class ValidateTimeliness
|
||||||
|
|
||||||
|
VALIDITY_TEST_VALUES = {
|
||||||
|
:date => {:pass => '2000-01-01', :fail => '2000-01-32'},
|
||||||
|
:time => {:pass => '12:00', :fail => '25:00'},
|
||||||
|
:datetime => {:pass => '2000-01-01 00:00:00', :fail => '2000-01-32 00:00:00'}
|
||||||
|
}
|
||||||
|
|
||||||
|
OPTION_TEST_SETTINGS = {
|
||||||
|
:is_at => { :method => :+, :modify_on => :invalid },
|
||||||
|
:before => { :method => :-, :modify_on => :valid },
|
||||||
|
:after => { :method => :+, :modify_on => :valid },
|
||||||
|
:on_or_before => { :method => :+, :modify_on => :invalid },
|
||||||
|
:on_or_after => { :method => :-, :modify_on => :invalid }
|
||||||
|
}
|
||||||
|
|
||||||
|
def initialize(attribute, options)
|
||||||
|
@expected, @options = attribute, options
|
||||||
|
@validator = ValidatesTimeliness::Validator.new(options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def matches?(record)
|
||||||
|
@record = record
|
||||||
|
@type = @options[:type]
|
||||||
|
|
||||||
|
valid = test_validity
|
||||||
|
|
||||||
|
valid = test_option(:is_at) if valid && @options[:is_at]
|
||||||
|
valid = test_option(:before) if valid && @options[:before]
|
||||||
|
valid = test_option(:after) if valid && @options[:after]
|
||||||
|
valid = test_option(:on_or_before) if valid && @options[:on_or_before]
|
||||||
|
valid = test_option(:on_or_after) if valid && @options[:on_or_after]
|
||||||
|
valid = test_between if valid && @options[:between]
|
||||||
|
|
||||||
|
return valid
|
||||||
|
end
|
||||||
|
|
||||||
|
def failure_message
|
||||||
|
"expected model to validate #{@type} attribute #{@expected.inspect} with #{@last_failure}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def negative_failure_message
|
||||||
|
"expected not to validate #{@type} attribute #{@expected.inspect}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def description
|
||||||
|
"have validated #{@type} attribute #{@expected.inspect}"
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def test_validity
|
||||||
|
invalid_value = VALIDITY_TEST_VALUES[@type][:fail]
|
||||||
|
valid_value = parse_and_cast(VALIDITY_TEST_VALUES[@type][:pass])
|
||||||
|
error_matching(invalid_value, "invalid_#{@type}".to_sym) &&
|
||||||
|
no_error_matching(valid_value, "invalid_#{@type}".to_sym)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_option(option)
|
||||||
|
settings = OPTION_TEST_SETTINGS[option]
|
||||||
|
boundary = parse_and_cast(@options[option])
|
||||||
|
|
||||||
|
method = settings[:method]
|
||||||
|
|
||||||
|
valid_value, invalid_value = if settings[:modify_on] == :valid
|
||||||
|
[ boundary.send(method, 1), boundary ]
|
||||||
|
else
|
||||||
|
[ boundary, boundary.send(method, 1) ]
|
||||||
|
end
|
||||||
|
|
||||||
|
error_matching(invalid_value, option) &&
|
||||||
|
no_error_matching(valid_value, option)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_before
|
||||||
|
before = parse_and_cast(@options[:before])
|
||||||
|
|
||||||
|
error_matching(before - 1, :before) &&
|
||||||
|
no_error_matching(before, :before)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_between
|
||||||
|
between = parse_and_cast(@options[:between])
|
||||||
|
|
||||||
|
error_matching(between.first - 1, :between) &&
|
||||||
|
error_matching(between.last + 1, :between) &&
|
||||||
|
no_error_matching(between.first, :between) &&
|
||||||
|
no_error_matching(between.last, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_and_cast(value)
|
||||||
|
value = @validator.class.send(:evaluate_option_value, value, @type, @record)
|
||||||
|
@validator.class.send(:type_cast_value, value, @type)
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_matching(value, option)
|
||||||
|
match = error_message_for(option)
|
||||||
|
@record.send("#{@expected}=", value)
|
||||||
|
@record.valid?
|
||||||
|
errors = @record.errors.on(@expected)
|
||||||
|
pass = [ errors ].flatten.any? {|error| /#{match}/ === error }
|
||||||
|
@last_failure = "error matching '#{match}' when value is #{format_value(value)}" unless pass
|
||||||
|
pass
|
||||||
|
end
|
||||||
|
|
||||||
|
def no_error_matching(value, option)
|
||||||
|
pass = !error_matching(value, option)
|
||||||
|
unless pass
|
||||||
|
error = error_message_for(option)
|
||||||
|
@last_failure = "no error matching '#{error}' when value is #{format_value(value)}"
|
||||||
|
end
|
||||||
|
pass
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_message_for(message)
|
||||||
|
restriction = @validator.class.send(:evaluate_option_value, @validator.configuration[message], @type, @record)
|
||||||
|
|
||||||
|
if restriction
|
||||||
|
restriction = @validator.class.send(:type_cast_value, restriction, @type)
|
||||||
|
interpolate = @validator.send(:interpolation_values, message, restriction)
|
||||||
|
end
|
||||||
|
|
||||||
|
if defined?(I18n)
|
||||||
|
interpolate ||= {}
|
||||||
|
options = interpolate.merge(:default => @validator.send(:custom_error_messages)[message])
|
||||||
|
if defined?(ActiveRecord::Error)
|
||||||
|
ActiveRecord::Error.new(@record, @expected, message, options).message
|
||||||
|
else
|
||||||
|
@record.errors.generate_message(@expected, message, options)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
interpolate ||= nil
|
||||||
|
@validator.error_messages[message] % interpolate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_value(value)
|
||||||
|
return value if value.is_a?(String)
|
||||||
|
value.strftime(@validator.class.error_value_formats[@type])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_date(attribute, options={})
|
||||||
|
options[:type] = :date
|
||||||
|
ValidateTimeliness.new(attribute, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_time(attribute, options={})
|
||||||
|
options[:type] = :time
|
||||||
|
ValidateTimeliness.new(attribute, options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_datetime(attribute, options={})
|
||||||
|
options[:type] = :datetime
|
||||||
|
ValidateTimeliness.new(attribute, options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
module Spec
|
|
||||||
module Rails
|
|
||||||
module Matchers
|
|
||||||
class ValidateTimeliness
|
|
||||||
|
|
||||||
def initialize(attribute, options)
|
|
||||||
@expected, @options = attribute, options
|
|
||||||
@options.reverse_merge!(error_messages)
|
|
||||||
end
|
|
||||||
|
|
||||||
def matches?(record)
|
|
||||||
@record = record
|
|
||||||
type = options[:type]
|
|
||||||
|
|
||||||
test_values = {
|
|
||||||
:date => {:pass => '2000-01-01', :fail => '2000-01-32'},
|
|
||||||
:time => {:pass => '12:00', :fail => '25:00'},
|
|
||||||
:datetime => {:pass => '2000-01-01 00:00:00', :fail => '2000-01-32 00:00:00'}
|
|
||||||
}
|
|
||||||
|
|
||||||
invalid_value = test_values[type][:fail]
|
|
||||||
valid_value = parse_and_cast(test_values[type][:pass])
|
|
||||||
valid = error_matching(invalid_value, /#{options["invalid_#{type}_message".to_sym]}/) &&
|
|
||||||
no_error_matching(valid_value, /#{options["invalid_#{type}_message".to_sym]}/)
|
|
||||||
|
|
||||||
valid = test_option(:before, :-) if options[:before] && valid
|
|
||||||
valid = test_option(:after, :+) if options[:after] && valid
|
|
||||||
|
|
||||||
valid = test_option(:on_or_before, :+, :modify_on => :invalid) if options[:on_or_before] && valid
|
|
||||||
valid = test_option(:on_or_after, :-, :modify_on => :invalid) if options[:on_or_after] && valid
|
|
||||||
|
|
||||||
return valid
|
|
||||||
end
|
|
||||||
|
|
||||||
def failure_message
|
|
||||||
"expected model to validate #{options[:type]} attribute #{expected.inspect} with #{last_failure}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def negative_failure_message
|
|
||||||
"expected not to validate #{options[:type]} attribute #{expected.inspect}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def description
|
|
||||||
"have validated #{options[:type]} attribute #{expected.inspect}"
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
attr_reader :actual, :expected, :record, :options, :last_failure
|
|
||||||
|
|
||||||
def test_option(option, modifier, settings={})
|
|
||||||
settings.reverse_merge!(:modify_on => :valid)
|
|
||||||
boundary = parse_and_cast(options[option])
|
|
||||||
|
|
||||||
valid_value, invalid_value = if settings[:modify_on] == :valid
|
|
||||||
[ boundary.send(modifier, 1), boundary ]
|
|
||||||
else
|
|
||||||
[ boundary, boundary.send(modifier, 1) ]
|
|
||||||
end
|
|
||||||
|
|
||||||
message = options["#{option}_message".to_sym]
|
|
||||||
error_matching(invalid_value, /#{message}/) &&
|
|
||||||
no_error_matching(valid_value, /#{message}/)
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_and_cast(value)
|
|
||||||
value = ActiveRecord::Base.send(:timeliness_restriction_value, value, record, options[:type])
|
|
||||||
cast_method = ActiveRecord::Base.send(:restriction_type_cast_method, options[:type])
|
|
||||||
value.send(cast_method) rescue nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def error_messages
|
|
||||||
messages = ActiveRecord::Base.send(:timeliness_default_error_messages)
|
|
||||||
messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub(' %s', ''); h }
|
|
||||||
@options.reverse_merge!(messages)
|
|
||||||
end
|
|
||||||
|
|
||||||
def error_matching(value, match)
|
|
||||||
record.send("#{expected}=", value)
|
|
||||||
record.valid?
|
|
||||||
errors = record.errors.on(expected)
|
|
||||||
pass = [ errors ].flatten.any? {|error| match === error }
|
|
||||||
@last_failure = "error matching #{match.inspect} when value is #{format_value(value)}" unless pass
|
|
||||||
pass
|
|
||||||
end
|
|
||||||
|
|
||||||
def no_error_matching(value, match)
|
|
||||||
pass = !error_matching(value, match)
|
|
||||||
@last_failure = "no error matching #{match.inspect} when value is #{format_value(value)}" unless pass
|
|
||||||
pass
|
|
||||||
end
|
|
||||||
|
|
||||||
def format_value(value)
|
|
||||||
return value if value.is_a?(String)
|
|
||||||
value.strftime(ActiveRecord::Errors.date_time_error_value_formats[options[:type]])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_date(attribute, options={})
|
|
||||||
options[:type] = :date
|
|
||||||
validate_timeliness_of(attribute, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_time(attribute, options={})
|
|
||||||
options[:type] = :time
|
|
||||||
validate_timeliness_of(attribute, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
def validate_datetime(attribute, options={})
|
|
||||||
options[:type] = :datetime
|
|
||||||
validate_timeliness_of(attribute, options)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def validate_timeliness_of(attribute, options={})
|
|
||||||
ValidateTimeliness.new(attribute, options)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
46
lib/validates_timeliness/validation_methods.rb
Normal file
46
lib/validates_timeliness/validation_methods.rb
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
module ValidatesTimeliness
|
||||||
|
module ValidationMethods
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.extend ClassMethods
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
|
||||||
|
def validates_time(*attr_names)
|
||||||
|
configuration = attr_names.extract_options!
|
||||||
|
configuration[:type] = :time
|
||||||
|
validates_timeliness_of(attr_names, configuration)
|
||||||
|
end
|
||||||
|
|
||||||
|
def validates_date(*attr_names)
|
||||||
|
configuration = attr_names.extract_options!
|
||||||
|
configuration[:type] = :date
|
||||||
|
validates_timeliness_of(attr_names, configuration)
|
||||||
|
end
|
||||||
|
|
||||||
|
def validates_datetime(*attr_names)
|
||||||
|
configuration = attr_names.extract_options!
|
||||||
|
configuration[:type] = :datetime
|
||||||
|
validates_timeliness_of(attr_names, configuration)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def validates_timeliness_of(attr_names, configuration)
|
||||||
|
validator = ValidatesTimeliness::Validator.new(configuration.symbolize_keys)
|
||||||
|
|
||||||
|
# bypass handling of allow_nil and allow_blank to validate raw value
|
||||||
|
configuration.delete(:allow_nil)
|
||||||
|
configuration.delete(:allow_blank)
|
||||||
|
validates_each(attr_names, configuration) do |record, attr_name, value|
|
||||||
|
validator.call(record, attr_name, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveRecord::Base.send(:include, ValidatesTimeliness::ValidationMethods)
|
||||||
@@ -1,189 +0,0 @@
|
|||||||
module ValidatesTimeliness
|
|
||||||
# Adds ActiveRecord validation methods for date, time and datetime validation.
|
|
||||||
# The validity of values can be restricted to be before or after certain dates
|
|
||||||
# or times.
|
|
||||||
module Validations
|
|
||||||
|
|
||||||
# Error messages and error value formats added to AR defaults to allow
|
|
||||||
# global override.
|
|
||||||
def self.included(base)
|
|
||||||
base.extend ClassMethods
|
|
||||||
|
|
||||||
base.class_inheritable_accessor :ignore_datetime_restriction_errors
|
|
||||||
base.ignore_datetime_restriction_errors = false
|
|
||||||
|
|
||||||
ActiveRecord::Errors.class_inheritable_accessor :date_time_error_value_formats
|
|
||||||
ActiveRecord::Errors.date_time_error_value_formats = {
|
|
||||||
:time => '%H:%M:%S',
|
|
||||||
:date => '%Y-%m-%d',
|
|
||||||
:datetime => '%Y-%m-%d %H:%M:%S'
|
|
||||||
}
|
|
||||||
|
|
||||||
ActiveRecord::Errors.default_error_messages.update(
|
|
||||||
:invalid_date => "is not a valid date",
|
|
||||||
:invalid_time => "is not a valid time",
|
|
||||||
:invalid_datetime => "is not a valid datetime",
|
|
||||||
:before => "must be before %s",
|
|
||||||
:on_or_before => "must be on or before %s",
|
|
||||||
:after => "must be after %s",
|
|
||||||
:on_or_after => "must be on or after %s"
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
module ClassMethods
|
|
||||||
|
|
||||||
def parse_date_time(raw_value, type, strict=true)
|
|
||||||
return nil if raw_value.blank?
|
|
||||||
return raw_value if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
|
||||||
|
|
||||||
time_array = ValidatesTimeliness::Formats.parse(raw_value, type, strict)
|
|
||||||
raise if time_array.nil?
|
|
||||||
|
|
||||||
# Rails dummy time date part is defined as 2000-01-01
|
|
||||||
time_array[0..2] = 2000, 1, 1 if type == :time
|
|
||||||
|
|
||||||
# Date.new enforces days per month, unlike Time
|
|
||||||
date = Date.new(*time_array[0..2]) unless type == :time
|
|
||||||
|
|
||||||
return date if type == :date
|
|
||||||
|
|
||||||
# Create time object which checks time part, and return time object
|
|
||||||
make_time(time_array)
|
|
||||||
rescue
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
# The main validation method which can be used directly or called through
|
|
||||||
# the other specific type validation methods.
|
|
||||||
def validates_timeliness_of(*attr_names)
|
|
||||||
configuration = { :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false }
|
|
||||||
configuration.update(timeliness_default_error_messages)
|
|
||||||
configuration.update(attr_names.extract_options!)
|
|
||||||
|
|
||||||
# we need to check raw value for blank or nil
|
|
||||||
allow_nil = configuration.delete(:allow_nil)
|
|
||||||
allow_blank = configuration.delete(:allow_blank)
|
|
||||||
|
|
||||||
validates_each(attr_names, configuration) do |record, attr_name, value|
|
|
||||||
raw_value = record.send("#{attr_name}_before_type_cast")
|
|
||||||
|
|
||||||
next if (raw_value.nil? && allow_nil) || (raw_value.blank? && allow_blank)
|
|
||||||
|
|
||||||
record.errors.add(attr_name, configuration[:blank_message]) and next if raw_value.blank?
|
|
||||||
|
|
||||||
column = record.column_for_attribute(attr_name)
|
|
||||||
begin
|
|
||||||
unless time = parse_date_time(raw_value, configuration[:type])
|
|
||||||
record.errors.add(attr_name, configuration["invalid_#{configuration[:type]}_message".to_sym])
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
validate_timeliness_restrictions(record, attr_name, time, configuration)
|
|
||||||
rescue Exception => e
|
|
||||||
record.errors.add(attr_name, configuration["invalid_#{configuration[:type]}_message".to_sym])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Use this validation to force validation of values and restrictions
|
|
||||||
# as dummy time
|
|
||||||
def validates_time(*attr_names)
|
|
||||||
configuration = attr_names.extract_options!
|
|
||||||
configuration[:type] = :time
|
|
||||||
validates_timeliness_of(attr_names, configuration)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Use this validation to force validation of values and restrictions
|
|
||||||
# as Date
|
|
||||||
def validates_date(*attr_names)
|
|
||||||
configuration = attr_names.extract_options!
|
|
||||||
configuration[:type] = :date
|
|
||||||
validates_timeliness_of(attr_names, configuration)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Use this validation to force validation of values and restrictions
|
|
||||||
# as Time/DateTime
|
|
||||||
def validates_datetime(*attr_names)
|
|
||||||
configuration = attr_names.extract_options!
|
|
||||||
configuration[:type] = :datetime
|
|
||||||
validates_timeliness_of(attr_names, configuration)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def timeliness_restriction_value(restriction, record, type)
|
|
||||||
case restriction
|
|
||||||
when Time, Date, DateTime
|
|
||||||
restriction
|
|
||||||
when Symbol
|
|
||||||
timeliness_restriction_value(record.send(restriction), record, type)
|
|
||||||
when Proc
|
|
||||||
timeliness_restriction_value(restriction.call(record), record, type)
|
|
||||||
else
|
|
||||||
parse_date_time(restriction, type, false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def restriction_type_cast_method(type)
|
|
||||||
case type
|
|
||||||
when :time then :to_dummy_time
|
|
||||||
when :date then :to_date
|
|
||||||
when :datetime then :to_time
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Validate value against the temporal restrictions. Restriction values
|
|
||||||
# maybe of mixed type, so they are evaluated as a common type, which may
|
|
||||||
# require conversion. The type used is defined by validation type.
|
|
||||||
def validate_timeliness_restrictions(record, attr_name, value, configuration)
|
|
||||||
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
|
||||||
|
|
||||||
type_cast_method = restriction_type_cast_method(configuration[:type])
|
|
||||||
|
|
||||||
display = ActiveRecord::Errors.date_time_error_value_formats[configuration[:type]]
|
|
||||||
|
|
||||||
value = value.send(type_cast_method)
|
|
||||||
|
|
||||||
restriction_methods.each do |option, method|
|
|
||||||
next unless restriction = configuration[option]
|
|
||||||
begin
|
|
||||||
compare = timeliness_restriction_value(restriction, record, configuration[:type])
|
|
||||||
|
|
||||||
next if compare.nil?
|
|
||||||
|
|
||||||
compare = compare.send(type_cast_method)
|
|
||||||
record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare.strftime(display)) unless value.send(method, compare)
|
|
||||||
rescue
|
|
||||||
record.errors.add(attr_name, "restriction '#{option}' value was invalid") unless self.ignore_datetime_restriction_errors
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Map error message keys to *_message to merge with validation options
|
|
||||||
def timeliness_default_error_messages
|
|
||||||
defaults = ActiveRecord::Errors.default_error_messages.slice(
|
|
||||||
:blank, :invalid_date, :invalid_time, :invalid_datetime, :before, :on_or_before, :after, :on_or_after)
|
|
||||||
returning({}) do |messages|
|
|
||||||
defaults.each {|k, v| messages["#{k}_message".to_sym] = v }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Create time in correct timezone. For Rails 2.1 that is value in
|
|
||||||
# Time.zone. Rails 2.0 should be default_timezone.
|
|
||||||
def make_time(time_array)
|
|
||||||
if Time.respond_to?(:zone) && time_zone_aware_attributes
|
|
||||||
Time.zone.local(*time_array)
|
|
||||||
else
|
|
||||||
begin
|
|
||||||
Time.send(ActiveRecord::Base.default_timezone, *time_array)
|
|
||||||
rescue ArgumentError, TypeError
|
|
||||||
zone_offset = ActiveRecord::Base.default_timezone == :local ? DateTime.local_offset : 0
|
|
||||||
time_array.pop # remove microseconds
|
|
||||||
DateTime.civil(*(time_array << zone_offset))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
230
lib/validates_timeliness/validator.rb
Normal file
230
lib/validates_timeliness/validator.rb
Normal file
@@ -0,0 +1,230 @@
|
|||||||
|
#TODO remove deprecated option :equal_to
|
||||||
|
module ValidatesTimeliness
|
||||||
|
|
||||||
|
class Validator
|
||||||
|
cattr_accessor :error_value_formats
|
||||||
|
cattr_accessor :ignore_restriction_errors
|
||||||
|
self.ignore_restriction_errors = false
|
||||||
|
|
||||||
|
RESTRICTION_METHODS = {
|
||||||
|
:is_at => :==,
|
||||||
|
:equal_to => :==,
|
||||||
|
:before => :<,
|
||||||
|
:after => :>,
|
||||||
|
:on_or_before => :<=,
|
||||||
|
:on_or_after => :>=,
|
||||||
|
:between => lambda {|v, r| (r.first..r.last).include?(v) }
|
||||||
|
}
|
||||||
|
|
||||||
|
VALID_OPTION_KEYS = [
|
||||||
|
:on, :if, :unless, :allow_nil, :empty, :allow_blank,
|
||||||
|
:with_time, :with_date, :ignore_usec, :format,
|
||||||
|
:invalid_time_message, :invalid_date_message, :invalid_datetime_message
|
||||||
|
] + RESTRICTION_METHODS.keys.map {|option| [option, "#{option}_message".to_sym] }.flatten
|
||||||
|
|
||||||
|
DEFAULT_OPTIONS = { :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false, :ignore_usec => false }
|
||||||
|
|
||||||
|
attr_reader :configuration, :type
|
||||||
|
|
||||||
|
def initialize(configuration)
|
||||||
|
@configuration = DEFAULT_OPTIONS.merge(configuration)
|
||||||
|
@type = @configuration.delete(:type)
|
||||||
|
validate_options(@configuration)
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(record, attr_name, value)
|
||||||
|
raw_value = raw_value(record, attr_name) || value
|
||||||
|
|
||||||
|
if value.is_a?(String) || configuration[:format]
|
||||||
|
value = ValidatesTimeliness::Parser.parse(raw_value, type, :strict => false, :format => configuration[:format])
|
||||||
|
end
|
||||||
|
|
||||||
|
return if (raw_value.nil? && configuration[:allow_nil]) || (raw_value.blank? && configuration[:allow_blank])
|
||||||
|
|
||||||
|
return add_error(record, attr_name, :blank) if raw_value.blank?
|
||||||
|
return add_error(record, attr_name, "invalid_#{type}".to_sym) if value.nil?
|
||||||
|
|
||||||
|
validate_restrictions(record, attr_name, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_messages
|
||||||
|
@error_messages ||= ::ActiveRecord::Errors.default_error_messages.merge(custom_error_messages)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def raw_value(record, attr_name)
|
||||||
|
record.send("#{attr_name}_before_type_cast") rescue nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_restrictions(record, attr_name, value)
|
||||||
|
if configuration[:with_time] || configuration[:with_date]
|
||||||
|
value = combine_date_and_time(value, record)
|
||||||
|
end
|
||||||
|
|
||||||
|
value = self.class.type_cast_value(value, implied_type, configuration[:ignore_usec])
|
||||||
|
|
||||||
|
return if value.nil?
|
||||||
|
|
||||||
|
RESTRICTION_METHODS.each do |option, method|
|
||||||
|
next unless restriction = configuration[option]
|
||||||
|
begin
|
||||||
|
restriction = self.class.evaluate_option_value(restriction, implied_type, record)
|
||||||
|
next if restriction.nil?
|
||||||
|
restriction = self.class.type_cast_value(restriction, implied_type, configuration[:ignore_usec])
|
||||||
|
|
||||||
|
unless evaluate_restriction(restriction, value, method)
|
||||||
|
add_error(record, attr_name, option, interpolation_values(option, restriction))
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
unless self.class.ignore_restriction_errors
|
||||||
|
add_error(record, attr_name, "restriction '#{option}' value was invalid")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def interpolation_values(option, restriction)
|
||||||
|
format = self.class.error_value_format_for(type)
|
||||||
|
restriction = [restriction] unless restriction.is_a?(Array)
|
||||||
|
|
||||||
|
if defined?(I18n)
|
||||||
|
interpolations = {}
|
||||||
|
keys = restriction.size == 1 ? [:restriction] : [:earliest, :latest]
|
||||||
|
keys.each_with_index {|key, i| interpolations[key] = restriction[i].strftime(format) }
|
||||||
|
interpolations
|
||||||
|
else
|
||||||
|
restriction.map {|r| r.strftime(format) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def evaluate_restriction(restriction, value, comparator)
|
||||||
|
return true if restriction.nil?
|
||||||
|
|
||||||
|
case comparator
|
||||||
|
when Symbol
|
||||||
|
value.send(comparator, restriction)
|
||||||
|
when Proc
|
||||||
|
comparator.call(value, restriction)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_error(record, attr_name, message, interpolate=nil)
|
||||||
|
if defined?(I18n)
|
||||||
|
custom = custom_error_messages[message]
|
||||||
|
record.errors.add(attr_name, message, { :default => custom }.merge(interpolate || {}))
|
||||||
|
else
|
||||||
|
message = error_messages[message] if message.is_a?(Symbol)
|
||||||
|
record.errors.add(attr_name, message % interpolate)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def custom_error_messages
|
||||||
|
@custom_error_messages ||= configuration.inject({}) {|msgs, (k, v)|
|
||||||
|
if md = /(.*)_message$/.match(k.to_s)
|
||||||
|
msgs[md[1].to_sym] = v
|
||||||
|
end
|
||||||
|
msgs
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def combine_date_and_time(value, record)
|
||||||
|
if type == :date
|
||||||
|
date = value
|
||||||
|
time = configuration[:with_time]
|
||||||
|
else
|
||||||
|
date = configuration[:with_date]
|
||||||
|
time = value
|
||||||
|
end
|
||||||
|
date, time = self.class.evaluate_option_value(date, :date, record), self.class.evaluate_option_value(time, :time, record)
|
||||||
|
return if date.nil? || time.nil?
|
||||||
|
ValidatesTimeliness::Parser.make_time([date.year, date.month, date.day, time.hour, time.min, time.sec, time.usec])
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_options(options)
|
||||||
|
if options.key?(:equal_to)
|
||||||
|
::ActiveSupport::Deprecation.warn("ValidatesTimeliness :equal_to option is deprecated due to clash with a default Rails option. Use :is_at instead. You will need to fix any I18n error message references to this option date/time attributes now.")
|
||||||
|
options[:is_at] = options.delete(:equal_to)
|
||||||
|
options[:is_at_message] = options.delete(:equal_to_message)
|
||||||
|
end
|
||||||
|
|
||||||
|
invalid_for_type = ([:time, :date, :datetime] - [type]).map {|k| "invalid_#{k}_message".to_sym }
|
||||||
|
invalid_for_type << :with_date unless type == :time
|
||||||
|
invalid_for_type << :with_time unless type == :date
|
||||||
|
options.assert_valid_keys(VALID_OPTION_KEYS - invalid_for_type)
|
||||||
|
end
|
||||||
|
|
||||||
|
def implied_type
|
||||||
|
@implied_type ||= configuration[:with_date] || configuration[:with_time] ? :datetime : type
|
||||||
|
end
|
||||||
|
|
||||||
|
# class methods
|
||||||
|
class << self
|
||||||
|
|
||||||
|
def error_value_format_for(type)
|
||||||
|
if defined?(I18n)
|
||||||
|
# work around for syntax check in vendored I18n for Rails <= 2.3.3
|
||||||
|
I18n.t('validates_timeliness.error_value_formats')[type] || error_value_formats[type]
|
||||||
|
else
|
||||||
|
error_value_formats[type]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def evaluate_option_value(value, type, record)
|
||||||
|
case value
|
||||||
|
when Time, Date
|
||||||
|
value
|
||||||
|
when Symbol
|
||||||
|
evaluate_option_value(record.send(value), type, record)
|
||||||
|
when Proc
|
||||||
|
result = value.arity > 0 ? value.call(record) : value.call
|
||||||
|
evaluate_option_value(result, type, record)
|
||||||
|
when Array
|
||||||
|
value.map {|r| evaluate_option_value(r, type, record) }.sort
|
||||||
|
when Range
|
||||||
|
evaluate_option_value([value.first, value.last], type, record)
|
||||||
|
else
|
||||||
|
ValidatesTimeliness::Parser.parse(value, type, :strict => false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def type_cast_value(value, type, ignore_usec=false)
|
||||||
|
if value.is_a?(Array)
|
||||||
|
value.map {|v| type_cast_value(v, type, ignore_usec) }
|
||||||
|
else
|
||||||
|
value = case type
|
||||||
|
when :time
|
||||||
|
dummy_time(value)
|
||||||
|
when :date
|
||||||
|
value.to_date
|
||||||
|
when :datetime
|
||||||
|
if value.is_a?(Time) || value.is_a?(DateTime)
|
||||||
|
value.to_time
|
||||||
|
else
|
||||||
|
value.to_time(ValidatesTimeliness.default_timezone)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
if ignore_usec && value.is_a?(Time)
|
||||||
|
ValidatesTimeliness::Parser.make_time(Array(value).reverse[4..9])
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def dummy_time(value)
|
||||||
|
if value.is_a?(Time) || value.is_a?(DateTime)
|
||||||
|
time = [value.hour, value.min, value.sec]
|
||||||
|
else
|
||||||
|
time = [0,0,0]
|
||||||
|
end
|
||||||
|
dummy_date = ValidatesTimeliness::Formats.dummy_date_for_time_type
|
||||||
|
ValidatesTimeliness::Parser.make_time(dummy_date + time)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
3
lib/validates_timeliness/version.rb
Normal file
3
lib/validates_timeliness/version.rb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module ValidatesTimeliness
|
||||||
|
VERSION = "2.3.1"
|
||||||
|
end
|
||||||
194
spec/action_view/instance_tag_spec.rb
Normal file
194
spec/action_view/instance_tag_spec.rb
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||||
|
|
||||||
|
describe 'ValidatesTimeliness::ActionView::InstanceTag' do
|
||||||
|
include ActionView::Helpers::DateHelper
|
||||||
|
include ActionController::Assertions::SelectorAssertions
|
||||||
|
|
||||||
|
before do
|
||||||
|
@person = Person.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def params
|
||||||
|
@params ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "datetime_select" do
|
||||||
|
it "should use param values when attribute is nil" do
|
||||||
|
params["person"] = {
|
||||||
|
"birth_date_and_time(1i)" => 2009,
|
||||||
|
"birth_date_and_time(2i)" => 2,
|
||||||
|
"birth_date_and_time(3i)" => 29,
|
||||||
|
"birth_date_and_time(4i)" => 12,
|
||||||
|
"birth_date_and_time(5i)" => 13,
|
||||||
|
"birth_date_and_time(6i)" => 14,
|
||||||
|
}
|
||||||
|
@person.birth_date_and_time = nil
|
||||||
|
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]', '2009')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]', 'February')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]', '29')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]', '12')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]', '13')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]', '14')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should override object values and use params if present" do
|
||||||
|
params["person"] = {
|
||||||
|
"birth_date_and_time(1i)" => 2009,
|
||||||
|
"birth_date_and_time(2i)" => 2,
|
||||||
|
"birth_date_and_time(3i)" => 29,
|
||||||
|
"birth_date_and_time(4i)" => 13,
|
||||||
|
"birth_date_and_time(5i)" => 14,
|
||||||
|
"birth_date_and_time(6i)" => 15,
|
||||||
|
}
|
||||||
|
@person.birth_date_and_time = "2009-03-01 13:14:15"
|
||||||
|
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]', '2009')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]', 'February')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]', '29')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]', '13')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]', '14')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]', '15')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should select attribute values from object if no params" do
|
||||||
|
@person.birth_date_and_time = "2009-01-02 13:14:15"
|
||||||
|
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]', '2009')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]', 'January')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]', '2')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]', '13')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]', '14')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]', '15')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should select attribute values if params does not contain attribute params" do
|
||||||
|
@person.birth_date_and_time = "2009-01-02 13:14:15"
|
||||||
|
params["person"] = { }
|
||||||
|
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]', '2009')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]', 'January')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]', '2')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]', '13')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]', '14')
|
||||||
|
output.should have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]', '15')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not select values when attribute value is nil and has no param values" do
|
||||||
|
@person.birth_date_and_time = nil
|
||||||
|
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_and_time_1i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_and_time_2i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_and_time_3i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_and_time_4i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_and_time_5i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_and_time_6i] option[selected=selected]')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "date_select" do
|
||||||
|
it "should use param values when attribute is nil" do
|
||||||
|
params["person"] = {
|
||||||
|
"birth_date(1i)" => 2009,
|
||||||
|
"birth_date(2i)" => 2,
|
||||||
|
"birth_date(3i)" => 29,
|
||||||
|
}
|
||||||
|
@person.birth_date = nil
|
||||||
|
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
|
||||||
|
output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February')
|
||||||
|
output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should override object values and use params if present" do
|
||||||
|
params["person"] = {
|
||||||
|
"birth_date(1i)" => 2009,
|
||||||
|
"birth_date(2i)" => 2,
|
||||||
|
"birth_date(3i)" => 29,
|
||||||
|
}
|
||||||
|
@person.birth_date = "2009-03-01"
|
||||||
|
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
|
||||||
|
output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'February')
|
||||||
|
output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '29')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should select attribute values from object if no params" do
|
||||||
|
@person.birth_date = "2009-01-02"
|
||||||
|
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
|
||||||
|
output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January')
|
||||||
|
output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should select attribute values if params does not contain attribute params" do
|
||||||
|
@person.birth_date = "2009-01-02"
|
||||||
|
params["person"] = { }
|
||||||
|
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('select[id=person_birth_date_1i] option[selected=selected]', '2009')
|
||||||
|
output.should have_tag('select[id=person_birth_date_2i] option[selected=selected]', 'January')
|
||||||
|
output.should have_tag('select[id=person_birth_date_3i] option[selected=selected]', '2')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not select values when attribute value is nil and has no param values" do
|
||||||
|
@person.birth_date = nil
|
||||||
|
output = date_select(:person, :birth_date, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_1i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_2i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_date_3i] option[selected=selected]')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "time_select" do
|
||||||
|
before :all do
|
||||||
|
Time.now = Time.mktime(2009,1,1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should use param values when attribute is nil" do
|
||||||
|
params["person"] = {
|
||||||
|
"birth_time(1i)" => 2000,
|
||||||
|
"birth_time(2i)" => 1,
|
||||||
|
"birth_time(3i)" => 1,
|
||||||
|
"birth_time(4i)" => 12,
|
||||||
|
"birth_time(5i)" => 13,
|
||||||
|
"birth_time(6i)" => 14,
|
||||||
|
}
|
||||||
|
@person.birth_time = nil
|
||||||
|
output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('input[id=person_birth_time_1i][value=2000]')
|
||||||
|
output.should have_tag('input[id=person_birth_time_2i][value=1]')
|
||||||
|
output.should have_tag('input[id=person_birth_time_3i][value=1]')
|
||||||
|
output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '12')
|
||||||
|
output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '13')
|
||||||
|
output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '14')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should select attribute values from object if no params" do
|
||||||
|
@person.birth_time = "13:14:15"
|
||||||
|
output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('input[id=person_birth_time_1i][value=2000]')
|
||||||
|
output.should have_tag('input[id=person_birth_time_2i][value=1]')
|
||||||
|
output.should have_tag('input[id=person_birth_time_3i][value=1]')
|
||||||
|
output.should have_tag('select[id=person_birth_time_4i] option[selected=selected]', '13')
|
||||||
|
output.should have_tag('select[id=person_birth_time_5i] option[selected=selected]', '14')
|
||||||
|
output.should have_tag('select[id=person_birth_time_6i] option[selected=selected]', '15')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not select values when attribute value is nil and has no param values" do
|
||||||
|
@person.birth_time = nil
|
||||||
|
output = time_select(:person, :birth_time, :include_blank => true, :include_seconds => true)
|
||||||
|
output.should have_tag('input[id=person_birth_time_1i][value=""]')
|
||||||
|
# Annoyingly these may or not have value attribute depending on rails version.
|
||||||
|
# output.should have_tag('input[id=person_birth_time_2i][value=""]')
|
||||||
|
# output.should have_tag('input[id=person_birth_time_3i][value=""]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_time_4i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_time_5i] option[selected=selected]')
|
||||||
|
output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]')
|
||||||
|
end
|
||||||
|
|
||||||
|
after :all do
|
||||||
|
Time.now = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -1,25 +1,40 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||||
|
|
||||||
describe ValidatesTimeliness::AttributeMethods do
|
describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
||||||
include ValidatesTimeliness::AttributeMethods
|
|
||||||
include ValidatesTimeliness::Validations
|
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@person = Person.new
|
@person = Person.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should define and call write method on first assign" do
|
||||||
|
Person.class_eval { @generated_methods = Set.new; @_defined_class_methods = nil }
|
||||||
|
Person.send(:undef_method, :birth_date=) if Person.instance_methods.include?('birth_date=')
|
||||||
|
person = Person.new
|
||||||
|
person.should_receive(:write_date_time_attribute)
|
||||||
|
person.birth_date = "2000-01-01"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should call write_date_time_attribute when time attribute assigned value" do
|
||||||
|
@person.should_receive(:write_date_time_attribute)
|
||||||
|
@person.birth_time = "12:00"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should call write_date_time_attribute when datetime attribute assigned value" do
|
||||||
|
@person.should_receive(:write_date_time_attribute)
|
||||||
|
@person.birth_date_and_time = "2000-01-01 12:00"
|
||||||
|
end
|
||||||
|
|
||||||
it "should call parser on write for datetime attribute" do
|
it "should call parser on write for datetime attribute" do
|
||||||
@person.class.should_receive(:parse_date_time).once
|
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||||
@person.birth_date_and_time = "2000-01-01 02:03:04"
|
@person.birth_date_and_time = "2000-01-01 02:03:04"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should call parser on write for date attribute" do
|
it "should call parser on write for date attribute" do
|
||||||
@person.class.should_receive(:parse_date_time).once
|
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||||
@person.birth_date = "2000-01-01"
|
@person.birth_date = "2000-01-01"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should call parser on write for time attribute" do
|
it "should call parser on write for time attribute" do
|
||||||
@person.class.should_receive(:parse_date_time).once
|
ValidatesTimeliness::Parser.should_receive(:parse).once
|
||||||
@person.birth_time = "12:00"
|
@person.birth_time = "12:00"
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -28,7 +43,7 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
@person.birth_date_and_time = time_string
|
@person.birth_date_and_time = time_string
|
||||||
@person.birth_date_and_time_before_type_cast.should == time_string
|
@person.birth_date_and_time_before_type_cast.should == time_string
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return Time object for attribute_before_type_cast when written as Time" do
|
it "should return Time object for attribute_before_type_cast when written as Time" do
|
||||||
@person.birth_date_and_time = Time.mktime(2000, 1, 1, 2, 3, 4)
|
@person.birth_date_and_time = Time.mktime(2000, 1, 1, 2, 3, 4)
|
||||||
@person.birth_date_and_time_before_type_cast.should be_kind_of(Time)
|
@person.birth_date_and_time_before_type_cast.should be_kind_of(Time)
|
||||||
@@ -39,36 +54,52 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
@person.birth_date_and_time.should be_kind_of(Time)
|
@person.birth_date_and_time.should be_kind_of(Time)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should return Time object for datetime attribute read method when assigned Date object" do
|
||||||
|
@person.birth_date_and_time = Date.today
|
||||||
|
@person.birth_date_and_time.should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
it "should return Time object for datetime attribute read method when assigned string" do
|
it "should return Time object for datetime attribute read method when assigned string" do
|
||||||
@person.birth_date_and_time = "2000-01-01 02:03:04"
|
@person.birth_date_and_time = "2000-01-01 02:03:04"
|
||||||
@person.birth_date_and_time.should be_kind_of(Time)
|
@person.birth_date_and_time.should be_kind_of(Time)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return Date object for date attribute read method when assigned Date object" do
|
it "should return Date object for date attribute read method when assigned Date object" do
|
||||||
@person.birth_date = Date.today
|
@person.birth_date = Date.today
|
||||||
@person.birth_date.should be_kind_of(Date)
|
@person.birth_date.should be_kind_of(Date)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return Date object for date attribute read method when assigned string" do
|
it "should return Date object for date attribute read method when assigned string" do
|
||||||
@person.birth_date = '2000-01-01'
|
@person.birth_date = '2000-01-01'
|
||||||
@person.birth_date.should be_kind_of(Date)
|
@person.birth_date.should be_kind_of(Date)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return nil when time is invalid" do
|
it "should return nil when time is invalid" do
|
||||||
@person.birth_date_and_time = "2000-01-32 02:03:04"
|
@person.birth_date_and_time = "2000-01-32 02:03:04"
|
||||||
@person.birth_date_and_time.should be_nil
|
@person.birth_date_and_time.should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not save invalid date value to database" do
|
it "should not save invalid date value to database" do
|
||||||
time_string = "2000-01-32 02:03:04"
|
time_string = "2000-01-32 02:03:04"
|
||||||
@person = Person.new
|
|
||||||
@person.birth_date_and_time = time_string
|
@person.birth_date_and_time = time_string
|
||||||
@person.save
|
@person.save
|
||||||
@person.reload
|
@person.reload
|
||||||
@person.birth_date_and_time_before_type_cast.should be_nil
|
@person.birth_date_and_time_before_type_cast.should be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
unless RAILS_VER < '2.1'
|
if RAILS_VER < '2.1'
|
||||||
|
|
||||||
|
it "should return time object from database in default timezone" do
|
||||||
|
ActiveRecord::Base.default_timezone = :utc
|
||||||
|
time_string = "2000-01-01 09:00:00"
|
||||||
|
@person.birth_date_and_time = time_string
|
||||||
|
@person.save
|
||||||
|
@person.reload
|
||||||
|
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z').should == time_string + ' UTC'
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
it "should return stored time string as Time with correct timezone" do
|
it "should return stored time string as Time with correct timezone" do
|
||||||
Time.zone = 'Melbourne'
|
Time.zone = 'Melbourne'
|
||||||
time_string = "2000-06-01 02:03:04"
|
time_string = "2000-06-01 02:03:04"
|
||||||
@@ -76,114 +107,51 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
|
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return time object from database in correct timezone" do
|
it "should return time object from database in correct timezone" do
|
||||||
Time.zone = 'Melbourne'
|
Time.zone = 'Melbourne'
|
||||||
time_string = "2000-06-01 09:00:00"
|
time_string = "2000-06-01 09:00:00"
|
||||||
@person = Person.new
|
|
||||||
@person.birth_date_and_time = time_string
|
@person.birth_date_and_time = time_string
|
||||||
@person.save
|
@person.save
|
||||||
@person.reload
|
@person.reload
|
||||||
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
|
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "dirty attributes" do
|
|
||||||
|
|
||||||
it "should return true for attribute changed? when value updated" do
|
|
||||||
time_string = "2000-01-01 02:03:04"
|
|
||||||
@person.birth_date_and_time = time_string
|
|
||||||
@person.birth_date_and_time_changed?.should be_true
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should show changes when time attribute changed from nil to Time object" do
|
|
||||||
time_string = "2000-01-01 02:03:04"
|
|
||||||
@person.birth_date_and_time = time_string
|
|
||||||
time = @person.birth_date_and_time
|
|
||||||
@person.changes.should == {"birth_date_and_time" => [nil, time]}
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should show changes when time attribute changed from Time object to nil" do
|
|
||||||
time_string = "2020-01-01 02:03:04"
|
|
||||||
@person.birth_date_and_time = time_string
|
|
||||||
@person.save false
|
|
||||||
@person.reload
|
|
||||||
time = @person.birth_date_and_time
|
|
||||||
@person.birth_date_and_time = nil
|
|
||||||
@person.changes.should == {"birth_date_and_time" => [time, nil]}
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should show no changes when assigned same value as Time object" do
|
|
||||||
time_string = "2020-01-01 02:03:04"
|
|
||||||
@person.birth_date_and_time = time_string
|
|
||||||
@person.save false
|
|
||||||
@person.reload
|
|
||||||
time = @person.birth_date_and_time
|
|
||||||
@person.birth_date_and_time = time
|
|
||||||
@person.changes.should == {}
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should show no changes when assigned same value as time string" do
|
|
||||||
time_string = "2020-01-01 02:03:04"
|
|
||||||
@person.birth_date_and_time = time_string
|
|
||||||
@person.save false
|
|
||||||
@person.reload
|
|
||||||
@person.birth_date_and_time = time_string
|
|
||||||
@person.changes.should == {}
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
else
|
|
||||||
|
|
||||||
it "should return time object from database in default timezone" do
|
|
||||||
ActiveRecord::Base.default_timezone = :utc
|
|
||||||
time_string = "2000-01-01 09:00:00"
|
|
||||||
@person = Person.new
|
|
||||||
@person.birth_date_and_time = time_string
|
|
||||||
@person.save
|
|
||||||
@person.reload
|
|
||||||
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z').should == time_string + ' GMT'
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return same time object on repeat reads on existing object" do
|
|
||||||
Time.zone = 'Melbourne' unless RAILS_VER < '2.1'
|
|
||||||
time_string = "2000-01-01 09:00:00"
|
|
||||||
@person = Person.new
|
|
||||||
@person.birth_date_and_time = time_string
|
|
||||||
@person.save!
|
|
||||||
@person.reload
|
|
||||||
time = @person.birth_date_and_time
|
|
||||||
@person.birth_date_and_time.should == time
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return same date object on repeat reads on existing object" do
|
|
||||||
date_string = Date.today
|
|
||||||
@person = Person.new
|
|
||||||
@person.birth_date = date_string
|
|
||||||
@person.save!
|
|
||||||
@person.reload
|
|
||||||
date = @person.birth_date
|
|
||||||
@person.birth_date.should == date
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return correct date value after new value assigned" do
|
it "should return correct date value after new value assigned" do
|
||||||
today = Date.today
|
today = Date.today
|
||||||
tomorrow = Date.today + 1.day
|
tomorrow = Date.today + 1.day
|
||||||
@person = Person.new
|
|
||||||
@person.birth_date = today
|
@person.birth_date = today
|
||||||
@person.birth_date.should == today
|
@person.birth_date.should == today
|
||||||
@person.birth_date = tomorrow
|
@person.birth_date = tomorrow
|
||||||
@person.birth_date.should == tomorrow
|
@person.birth_date.should == tomorrow
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should update date attribute on existing object" do
|
it "should update date attribute on existing object" do
|
||||||
today = Date.today
|
today = Date.today
|
||||||
tomorrow = Date.today + 1.day
|
tomorrow = Date.today + 1.day
|
||||||
@person = Person.create(:birth_date => today)
|
person = Person.create(:birth_date => today)
|
||||||
@person.birth_date = tomorrow
|
person.birth_date = tomorrow
|
||||||
@person.save!
|
person.save!
|
||||||
@person.reload
|
person.reload
|
||||||
@person.birth_date.should == tomorrow
|
person.birth_date.should == tomorrow
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "attribute writer" do
|
||||||
|
|
||||||
|
it "should be able to be overridden in class" do
|
||||||
|
Person.class_eval { attr_accessor :birth_date }
|
||||||
|
person = Person.new
|
||||||
|
person.birth_date = 'overriden'
|
||||||
|
person.birth_date.should == 'overriden'
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
Person.class_eval do
|
||||||
|
undef_method(:birth_date)
|
||||||
|
undef_method(:birth_date=)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
118
spec/active_record/multiparameter_attributes_spec.rb
Normal file
118
spec/active_record/multiparameter_attributes_spec.rb
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
||||||
|
|
||||||
|
describe ValidatesTimeliness::ActiveRecord::MultiparameterAttributes do
|
||||||
|
def obj
|
||||||
|
@obj ||= Person.new
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should convert array for datetime type into datetime string" do
|
||||||
|
time_string = time_array_to_string([2000,2,1,9,10,11], :datetime)
|
||||||
|
time_string.should == "2000-02-01 09:10:11"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should convert array for date type into date string" do
|
||||||
|
time_string = time_array_to_string([2000,2,1], :date)
|
||||||
|
time_string.should == "2000-02-01"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should convert array for time type into time string" do
|
||||||
|
time_string = time_array_to_string([2000,1,1,9,10,11], :time)
|
||||||
|
time_string.should == "09:10:11"
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "execute_callstack_for_multiparameter_attributes" do
|
||||||
|
|
||||||
|
describe "for valid values" do
|
||||||
|
before do
|
||||||
|
@callstack = {
|
||||||
|
'birth_date_and_time' => [2000,2,1,9,10,11],
|
||||||
|
'birth_date' => [2000,2,1,9,10,11],
|
||||||
|
'birth_time' => [2000,2,1,9,10,11]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store datetime string for datetime column" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with("2000-02-01 09:10:11")
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store date string for a date column" do
|
||||||
|
obj.should_receive(:birth_date=).once.with("2000-02-01")
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store time string for a time column" do
|
||||||
|
obj.should_receive(:birth_time=).once.with("09:10:11")
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for invalid values" do
|
||||||
|
before do
|
||||||
|
@callstack = {
|
||||||
|
'birth_date_and_time' => [2000,13,1,9,10,11],
|
||||||
|
'birth_date' => [2000,2,41,9,10,11],
|
||||||
|
'birth_time' => [2000,2,1,25,10,11]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store invalid datetime string for datetime column" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with("2000-13-01 09:10:11")
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store invalid date string for a date column" do
|
||||||
|
obj.should_receive(:birth_date=).once.with("2000-02-41")
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store invalid time string for a time column" do
|
||||||
|
obj.should_receive(:birth_time=).once.with("25:10:11")
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for missing values" do
|
||||||
|
it "should store nil if all datetime values nil" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with(nil)
|
||||||
|
callstack = { 'birth_date_and_time' => [nil,nil,nil,nil,nil,nil] }
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store nil year as empty value in string" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with("-02-01 09:10:11")
|
||||||
|
callstack = { 'birth_date_and_time' => [nil,2,1,9,10,11] }
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store nil month as empty value in string" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with("2000--01 09:10:11")
|
||||||
|
callstack = { 'birth_date_and_time' => [2000,nil,1,9,10,11] }
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store nil day as empty value in string" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with("2000-02- 09:10:11")
|
||||||
|
callstack = { 'birth_date_and_time' => [2000,2,nil,9,10,11] }
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store nil hour as empty value in string" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with("2000-02-01 :10:11")
|
||||||
|
callstack = { 'birth_date_and_time' => [2000,2,1,nil,10,11] }
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, callstack)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should store nil minute as empty value in string" do
|
||||||
|
obj.should_receive(:birth_date_and_time=).once.with("2000-02-01 09:10:")
|
||||||
|
callstack = { 'birth_date_and_time' => [2000,2,1,9,10,nil] }
|
||||||
|
obj.send(:execute_callstack_for_multiparameter_attributes, callstack)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def time_array_to_string(*args)
|
||||||
|
ValidatesTimeliness::ActiveRecord.time_array_to_string(*args)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
|
||||||
|
|
||||||
describe ValidatesTimeliness::CoreExtensions::Date do
|
|
||||||
before do
|
|
||||||
@a_date = Date.new(2008, 7, 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should " do
|
|
||||||
@a_date.to_dummy_time.should == Time.mktime(2000,1,1,0,0,0)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe ValidatesTimeliness::CoreExtensions::Time do
|
|
||||||
before do
|
|
||||||
@a_time = Time.mktime(2008, 7, 1, 2, 3, 4)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should " do
|
|
||||||
@a_time.to_dummy_time.should == Time.mktime(2000,1,1,2,3,4)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe ValidatesTimeliness::CoreExtensions::DateTime do
|
|
||||||
before do
|
|
||||||
@a_datetime = DateTime.new(2008, 7, 1, 2, 3, 4)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should " do
|
|
||||||
@a_datetime.to_dummy_time.should == Time.mktime(2000,1,1,2,3,4)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,82 +1,41 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||||
|
|
||||||
describe ValidatesTimeliness::Formats do
|
describe ValidatesTimeliness::Formats do
|
||||||
attr_reader :formats
|
|
||||||
|
|
||||||
before do
|
|
||||||
@formats = ValidatesTimeliness::Formats
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "expression generator" do
|
|
||||||
it "should generate regexp for time" do
|
|
||||||
generate_regexp_str('hh:nn:ss').should == '/(\d{2}):(\d{2}):(\d{2})/'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate regexp for time with meridian" do
|
|
||||||
generate_regexp_str('hh:nn:ss ampm').should == '/(\d{2}):(\d{2}):(\d{2}) ((?:[aApP])\.?[mM]\.?)/'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate regexp for time with meridian and optional space between" do
|
|
||||||
generate_regexp_str('hh:nn:ss_ampm').should == '/(\d{2}):(\d{2}):(\d{2})\s?((?:[aApP])\.?[mM]\.?)/'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate regexp for time with single or double digits" do
|
|
||||||
generate_regexp_str('h:n:s').should == '/(\d{1,2}):(\d{1,2}):(\d{1,2})/'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate regexp for date" do
|
|
||||||
generate_regexp_str('yyyy-mm-dd').should == '/(\d{4})-(\d{2})-(\d{2})/'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate regexp for date with slashes" do
|
|
||||||
generate_regexp_str('dd/mm/yyyy').should == '/(\d{2})\/(\d{2})\/(\d{4})/'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate regexp for date with dots" do
|
|
||||||
generate_regexp_str('dd.mm.yyyy').should == '/(\d{2})\.(\d{2})\.(\d{4})/'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate regexp for Ruby time string" do
|
|
||||||
expected = '/(\w{3,9}) (\w{3,9}) (\d{2}):(\d{2}):(\d{2}) (?:[+-]\d{2}:?\d{2}) (\d{4})/'
|
|
||||||
generate_regexp_str('ddd mmm hh:nn:ss zo yyyy').should == expected
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate regexp for iso8601 datetime" do
|
|
||||||
expected = '/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:Z|(?:[+-]\d{2}:?\d{2}))/'
|
|
||||||
generate_regexp_str('yyyy-mm-ddThh:nn:ss(?:Z|zo)').should == expected
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "format proc generator" do
|
describe "format proc generator" do
|
||||||
it "should generate proc which outputs date array with values in correct order" do
|
it "should generate proc which outputs date array with values in correct order" do
|
||||||
generate_proc('yyyy-mm-dd').call('2000', '1', '2').should == [2000,1,2,0,0,0,0]
|
generate_proc('yyyy-mm-dd').call('2000', '1', '2').should == [2000,1,2,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should generate proc which outputs date array from format with different order" do
|
it "should generate proc which outputs date array from format with different order" do
|
||||||
generate_proc('dd/mm/yyyy').call('2', '1', '2000').should == [2000,1,2,0,0,0,0]
|
generate_proc('dd/mm/yyyy').call('2', '1', '2000').should == [2000,1,2,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should generate proc which outputs time array" do
|
it "should generate proc which outputs time array" do
|
||||||
generate_proc('hh:nn:ss').call('01', '02', '03').should == [0,0,0,1,2,3,0]
|
generate_proc('hh:nn:ss').call('01', '02', '03').should == [0,0,0,1,2,3,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should generate proc which outputs time array with meridian 'pm' adjusted hour" do
|
it "should generate proc which outputs time array with meridian 'pm' adjusted hour" do
|
||||||
generate_proc('hh:nn:ss ampm').call('01', '02', '03', 'pm').should == [0,0,0,13,2,3,0]
|
generate_proc('hh:nn:ss ampm').call('01', '02', '03', 'pm').should == [0,0,0,13,2,3,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should generate proc which outputs time array with meridian 'am' unadjusted hour" do
|
it "should generate proc which outputs time array with meridian 'am' unadjusted hour" do
|
||||||
generate_proc('hh:nn:ss ampm').call('01', '02', '03', 'am').should == [0,0,0,1,2,3,0]
|
generate_proc('hh:nn:ss ampm').call('01', '02', '03', 'am').should == [0,0,0,1,2,3,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should generate proc which outputs time array with microseconds" do
|
it "should generate proc which outputs time array with microseconds" do
|
||||||
generate_proc('hh:nn:ss.u').call('01', '02', '03', '99').should == [0,0,0,1,2,3,990000]
|
generate_proc('hh:nn:ss.u').call('01', '02', '03', '99').should == [0,0,0,1,2,3,990000]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should generate proc which outputs datetime array with zone offset" do
|
||||||
|
generate_proc('yyyy-mm-dd hh:nn:ss.u zo').call('2001', '02', '03', '04', '05', '06', '99', '+10:00').should == [2001,2,3,4,5,6,990000,36000]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "validation regexps" do
|
describe "validate regexps" do
|
||||||
|
|
||||||
describe "for time formats" do
|
describe "for time formats" do
|
||||||
format_tests = {
|
format_tests = {
|
||||||
'hh:nn:ss' => {:pass => ['12:12:12', '01:01:01'], :fail => ['1:12:12', '12:1:12', '12:12:1', '12-12-12']},
|
'hh:nn:ss' => {:pass => ['12:12:12', '01:01:01'], :fail => ['1:12:12', '12:1:12', '12:12:1', '12-12-12']},
|
||||||
'hh-nn-ss' => {:pass => ['12-12-12', '01-01-01'], :fail => ['1-12-12', '12-1-12', '12-12-1', '12:12:12']},
|
'hh-nn-ss' => {:pass => ['12-12-12', '01-01-01'], :fail => ['1-12-12', '12-1-12', '12-12-1', '12:12:12']},
|
||||||
'h:nn' => {:pass => ['12:12', '1:01'], :fail => ['12:2', '12-12']},
|
'h:nn' => {:pass => ['12:12', '1:01'], :fail => ['12:2', '12-12']},
|
||||||
@@ -95,9 +54,9 @@ describe ValidatesTimeliness::Formats do
|
|||||||
values[:pass].each {|value| value.should match(regexp)}
|
values[:pass].each {|value| value.should match(regexp)}
|
||||||
values[:fail].each {|value| value.should_not match(regexp)}
|
values[:fail].each {|value| value.should_not match(regexp)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "for date formats" do
|
describe "for date formats" do
|
||||||
format_tests = {
|
format_tests = {
|
||||||
'yyyy/mm/dd' => {:pass => ['2000/02/01'], :fail => ['2000\02\01', '2000/2/1', '00/02/01']},
|
'yyyy/mm/dd' => {:pass => ['2000/02/01'], :fail => ['2000\02\01', '2000/2/1', '00/02/01']},
|
||||||
@@ -109,130 +68,198 @@ describe ValidatesTimeliness::Formats do
|
|||||||
'd\m\yy' => {:pass => ['1\2\01', '1\02\00', '01\02\2000'], :fail => ['1\2\0', '1/2/01']},
|
'd\m\yy' => {:pass => ['1\2\01', '1\02\00', '01\02\2000'], :fail => ['1\2\0', '1/2/01']},
|
||||||
'd-m-yy' => {:pass => ['1-2-01', '1-02-00', '01-02-2000'], :fail => ['1-2-0', '1/2/01']},
|
'd-m-yy' => {:pass => ['1-2-01', '1-02-00', '01-02-2000'], :fail => ['1-2-0', '1/2/01']},
|
||||||
'd.m.yy' => {:pass => ['1.2.01', '1.02.00', '01.02.2000'], :fail => ['1.2.0', '1/2/01']},
|
'd.m.yy' => {:pass => ['1.2.01', '1.02.00', '01.02.2000'], :fail => ['1.2.0', '1/2/01']},
|
||||||
'd mmm yy' => {:pass => ['1 Feb 00', '1 Feb 2000', '1 February 00', '01 February 2000'],
|
'd mmm yy' => {:pass => ['1 Feb 00', '1 Feb 2000', '1 February 00', '01 February 2000'],
|
||||||
:fail => ['1 Fe 00', 'Feb 1 2000', '1 Feb 0']}
|
:fail => ['1 Fe 00', 'Feb 1 2000', '1 Feb 0']}
|
||||||
}
|
}
|
||||||
format_tests.each do |format, values|
|
format_tests.each do |format, values|
|
||||||
it "should correctly validate dates in format '#{format}'" do
|
it "should correctly validate dates in format '#{format}'" do
|
||||||
regexp = generate_regexp(format)
|
regexp = generate_regexp(format)
|
||||||
values[:pass].each {|value| value.should match(regexp)}
|
values[:pass].each {|value| value.should match(regexp)}
|
||||||
values[:fail].each {|value| value.should_not match(regexp)}
|
values[:fail].each {|value| value.should_not match(regexp)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "for datetime formats" do
|
describe "for datetime formats" do
|
||||||
format_tests = {
|
format_tests = {
|
||||||
'ddd mmm d hh:nn:ss zo yyyy' => {:pass => ['Sat Jul 19 12:00:00 +1000 2008'], :fail => []},
|
'ddd mmm d hh:nn:ss zo yyyy' => {:pass => ['Sat Jul 19 12:00:00 +1000 2008'], :fail => []},
|
||||||
'yyyy-mm-ddThh:nn:ss(?:Z|zo)' => {:pass => ['2008-07-19T12:00:00+10:00', '2008-07-19T12:00:00Z'], :fail => ['2008-07-19T12:00:00Z+10:00']},
|
'yyyy-mm-ddThh:nn:ss(?:Z|zo)' => {:pass => ['2008-07-19T12:00:00+10:00', '2008-07-19T12:00:00Z'], :fail => ['2008-07-19T12:00:00Z+10:00']},
|
||||||
}
|
}
|
||||||
format_tests.each do |format, values|
|
format_tests.each do |format, values|
|
||||||
it "should correctly validate datetimes in format '#{format}'" do
|
it "should correctly validate datetimes in format '#{format}'" do
|
||||||
regexp = generate_regexp(format)
|
regexp = generate_regexp(format)
|
||||||
values[:pass].each {|value| value.should match(regexp)}
|
values[:pass].each {|value| value.should match(regexp)}
|
||||||
values[:fail].each {|value| value.should_not match(regexp)}
|
values[:fail].each {|value| value.should_not match(regexp)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "extracting values" do
|
describe "parse" do
|
||||||
|
|
||||||
it "should return time array from date string" do
|
it "should return time array from date string" do
|
||||||
time_array = formats.parse('12:13:14', :time, true)
|
time_array = formats.parse('12:13:14', :time, :strict => true)
|
||||||
time_array.should == [0,0,0,12,13,14,0]
|
time_array.should == [2000,1,1,12,13,14,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return date array from time string" do
|
it "should return date array from time string" do
|
||||||
time_array = formats.parse('2000-02-01', :date, true)
|
time_array = formats.parse('2000-02-01', :date, :strict => true)
|
||||||
time_array.should == [2000,2,1,0,0,0,0]
|
time_array.should == [2000,2,1,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return datetime array from string value" do
|
it "should return datetime array from string value" do
|
||||||
time_array = formats.parse('2000-02-01 12:13:14', :datetime, true)
|
time_array = formats.parse('2000-02-01 12:13:14', :datetime, :strict => true)
|
||||||
time_array.should == [2000,2,1,12,13,14,0]
|
time_array.should == [2000,2,1,12,13,14,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should parse date string when type is datetime" do
|
it "should parse date string when type is datetime" do
|
||||||
time_array = formats.parse('2000-02-01', :datetime, false)
|
time_array = formats.parse('2000-02-01', :datetime, :strict => false)
|
||||||
time_array.should == [2000,2,1,0,0,0,0]
|
time_array.should == [2000,2,1,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should ignore time when extracting date and strict is false" do
|
it "should ignore time when extracting date and strict is false" do
|
||||||
time_array = formats.parse('2000-02-01 12:12', :date, false)
|
time_array = formats.parse('2000-02-01 12:13', :date, :strict => false)
|
||||||
time_array.should == [2000,2,1,0,0,0,0]
|
time_array.should == [2000,2,1,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should ignore time when extracting date from format with trailing year and strict is false" do
|
||||||
|
time_array = formats.parse('01-02-2000 12:13', :date, :strict => false)
|
||||||
|
time_array.should == [2000,2,1,0,0,0,0]
|
||||||
|
end
|
||||||
|
|
||||||
it "should ignore date when extracting time and strict is false" do
|
it "should ignore date when extracting time and strict is false" do
|
||||||
time_array = formats.parse('2000-02-01 12:12', :time, false)
|
time_array = formats.parse('2000-02-01 12:13', :time, :strict => false)
|
||||||
time_array.should == [0,0,0,12,12,0,0]
|
time_array.should == [2000,1,1,12,13,0,0]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return zone offset when :include_offset option is true" do
|
||||||
|
time_array = formats.parse('2000-02-01T12:13:14-10:30', :datetime, :include_offset => true)
|
||||||
|
time_array.should == [2000,2,1,12,13,14,0,-37800]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "removing formats" do
|
describe "parse with format option" do
|
||||||
before do
|
it "should return values if string matches specified format" do
|
||||||
formats.compile_format_expressions
|
time_array = formats.parse('2000-02-01 12:13:14', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
|
||||||
|
time_array.should == [2000,2,1,12,13,14,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should remove format from format array" do
|
it "should return nil if string does not match specified format" do
|
||||||
|
time_array = formats.parse('2000-02-01 12:13', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
|
||||||
|
time_array.should be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "parsing date with ambiguous year" do
|
||||||
|
it "should return year in current century if year below threshold" do
|
||||||
|
time_array = formats.parse('01-02-29', :date)
|
||||||
|
time_array.should == [2029,2,1,0,0,0,0]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return year in last century if year at or above threshold" do
|
||||||
|
time_array = formats.parse('01-02-30', :date)
|
||||||
|
time_array.should == [1930,2,1,0,0,0,0]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow custom threshold" do
|
||||||
|
default = ValidatesTimeliness::Formats.ambiguous_year_threshold
|
||||||
|
ValidatesTimeliness::Formats.ambiguous_year_threshold = 40
|
||||||
|
time_array = formats.parse('01-02-39', :date)
|
||||||
|
time_array.should == [2039,2,1,0,0,0,0]
|
||||||
|
time_array = formats.parse('01-02-40', :date)
|
||||||
|
time_array.should == [1940,2,1,0,0,0,0]
|
||||||
|
ValidatesTimeliness::Formats.ambiguous_year_threshold = default
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "parse with custom dummy date values" do
|
||||||
|
before(:all) do
|
||||||
|
@old_dummy_date = formats.dummy_date_for_time_type
|
||||||
|
formats.dummy_date_for_time_type = [2009,1,1]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return time array with custom dummy date" do
|
||||||
|
time_array = formats.parse('12:13:14', :time, :strict => true)
|
||||||
|
time_array.should == [2009,1,1,12,13,14,0]
|
||||||
|
end
|
||||||
|
|
||||||
|
after(:all) do
|
||||||
|
formats.dummy_date_for_time_type = @old_dummy_date
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "parse ISO8601 datetime" do
|
||||||
|
it "should return array without zone offset when no offset in string" do
|
||||||
|
time_array = formats.parse('2000-02-01T12:13:14Z', :datetime, :strict => true)
|
||||||
|
time_array.should == [2000,2,1,12,13,14,0]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return array with zone offset when offset in string" do
|
||||||
|
time_array = formats.parse('2000-02-01T12:13:14+10:00', :datetime, :strict => true)
|
||||||
|
time_array.should == [2000,2,1,12,13,14,0,36000]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "removing formats" do
|
||||||
|
it "should remove format from format array" do
|
||||||
formats.remove_formats(:time, 'h.nn_ampm')
|
formats.remove_formats(:time, 'h.nn_ampm')
|
||||||
formats.time_formats.should_not include("h o'clock")
|
formats.time_formats.should_not include("h o'clock")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not match time after its format is removed" do
|
it "should not match time after its format is removed" do
|
||||||
validate('2.12am', :time).should be_true
|
validate('2.12am', :time).should be_true
|
||||||
formats.remove_formats(:time, 'h.nn_ampm')
|
formats.remove_formats(:time, 'h.nn_ampm')
|
||||||
validate('2.12am', :time).should be_false
|
validate('2.12am', :time).should be_false
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise error if format does not exist" do
|
it "should raise error if format does not exist" do
|
||||||
lambda { formats.remove_formats(:time, "ss:hh:nn") }.should raise_error()
|
lambda { formats.remove_formats(:time, "ss:hh:nn") }.should raise_error()
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
formats.time_formats << 'h.nn_ampm'
|
formats.time_formats << 'h.nn_ampm'
|
||||||
# reload class instead
|
formats.compile_format_expressions
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "adding formats" do
|
describe "adding formats" do
|
||||||
before do
|
before do
|
||||||
formats.compile_format_expressions
|
formats.compile_format_expressions
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should add format to format array" do
|
it "should add format to format array" do
|
||||||
formats.add_formats(:time, "h o'clock")
|
formats.add_formats(:time, "h o'clock")
|
||||||
formats.time_formats.should include("h o'clock")
|
formats.time_formats.should include("h o'clock")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should match new format after its added" do
|
it "should match new format after its added" do
|
||||||
validate("12 o'clock", :time).should be_false
|
validate("12 o'clock", :time).should be_false
|
||||||
formats.add_formats(:time, "h o'clock")
|
formats.add_formats(:time, "h o'clock")
|
||||||
validate("12 o'clock", :time).should be_true
|
validate("12 o'clock", :time).should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should add format before specified format and be higher precedence" do
|
it "should add format before specified format and be higher precedence" do
|
||||||
formats.add_formats(:time, "ss:hh:nn", :before => 'hh:nn:ss')
|
formats.add_formats(:time, "ss:hh:nn", :before => 'hh:nn:ss')
|
||||||
validate("59:23:58", :time).should be_true
|
validate("59:23:58", :time).should be_true
|
||||||
time_array = formats.parse('59:23:58', :time)
|
time_array = formats.parse('59:23:58', :time)
|
||||||
time_array.should == [0,0,0,23,58,59,0]
|
time_array.should == [2000,1,1,23,58,59,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise error if format exists" do
|
it "should raise error if format exists" do
|
||||||
lambda { formats.add_formats(:time, "hh:nn:ss") }.should raise_error()
|
lambda { formats.add_formats(:time, "hh:nn:ss") }.should raise_error()
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise error if format exists" do
|
it "should raise error if format exists" do
|
||||||
lambda { formats.add_formats(:time, "ss:hh:nn", :before => 'nn:hh:ss') }.should raise_error()
|
lambda { formats.add_formats(:time, "ss:hh:nn", :before => 'nn:hh:ss') }.should raise_error()
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
formats.time_formats.delete("h o'clock")
|
formats.time_formats.delete("h o'clock")
|
||||||
formats.time_formats.delete("ss:hh:nn")
|
formats.time_formats.delete("ss:hh:nn")
|
||||||
# reload class instead
|
# reload class instead
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "removing US formats" do
|
describe "removing US formats" do
|
||||||
it "should validate a date as European format when US formats removed" do
|
it "should validate a date as European format when US formats removed" do
|
||||||
time_array = formats.parse('01/02/2000', :date)
|
time_array = formats.parse('01/02/2000', :date)
|
||||||
@@ -241,33 +268,38 @@ describe ValidatesTimeliness::Formats do
|
|||||||
time_array = formats.parse('01/02/2000', :date)
|
time_array = formats.parse('01/02/2000', :date)
|
||||||
time_array.should == [2000, 2, 1,0,0,0,0]
|
time_array.should == [2000, 2, 1,0,0,0,0]
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
# reload class
|
# reload class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def formats
|
||||||
|
ValidatesTimeliness::Formats
|
||||||
|
end
|
||||||
|
|
||||||
def validate(time_string, type)
|
def validate(time_string, type)
|
||||||
valid = false
|
valid = false
|
||||||
formats.send("#{type}_expressions").each do |(regexp, processor)|
|
formats.send("#{type}_expressions").each do |format, regexp, processor|
|
||||||
valid = true and break if /\A#{regexp}\Z/ =~ time_string
|
valid = true and break if /\A#{regexp}\Z/ =~ time_string
|
||||||
end
|
end
|
||||||
valid
|
valid
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_regexp(format)
|
def generate_regexp(format)
|
||||||
# wrap in line start and end anchors to emulate extract values method
|
# wrap in line start and end anchors to emulate extract values method
|
||||||
/\A#{formats.send(:format_expression_generator, format)[0]}\Z/
|
/\A#{formats.send(:generate_format_expression, format)[0]}\Z/
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_regexp_str(format)
|
def generate_regexp_str(format)
|
||||||
formats.send(:format_expression_generator, format)[0].inspect
|
formats.send(:generate_format_expression, format)[0].inspect
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_proc(format)
|
def generate_proc(format)
|
||||||
formats.send(:format_expression_generator, format)[1]
|
formats.send(:generate_format_expression, format)[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_format(type, format)
|
def delete_format(type, format)
|
||||||
formats.send("#{type}_formats").delete(format)
|
formats.send("#{type}_formats").delete(format)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
# For use with the ginger gem to test plugin against multiple versions of Rails.
|
# For use with the ginger gem to test plugin against multiple versions of Rails.
|
||||||
#
|
#
|
||||||
# To use ginger:
|
# To use ginger:
|
||||||
#
|
#
|
||||||
# sudo gem install freelancing-god-ginger --source=http://gems.github.com
|
# gem install ginger
|
||||||
#
|
#
|
||||||
# Then run
|
# Then run
|
||||||
#
|
#
|
||||||
# ginger spec
|
# ginger spec
|
||||||
#
|
#
|
||||||
Ginger.configure do |config|
|
Ginger.configure do |config|
|
||||||
rails_versions = ['2.0.2', '2.1.2', '2.2.2']
|
rails_versions = ['2.0.2', '2.1.2', '2.2.2', '2.3.3', '2.3.4', '2.3.5']
|
||||||
|
|
||||||
rails_versions.each do |v|
|
rails_versions.each do |v|
|
||||||
g = Ginger::Scenario.new
|
g = Ginger::Scenario.new("Rails #{v}")
|
||||||
g['rails'] = v
|
g['rails'] = v
|
||||||
config.scenarios << g.dup
|
config.scenarios << g.dup
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
|
||||||
|
|
||||||
describe ValidatesTimeliness::InstanceTag, :type => :helper do
|
|
||||||
|
|
||||||
before do
|
|
||||||
@person = Person.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should display invalid datetime as datetime_select values" do
|
|
||||||
@person.birth_date_and_time = "2008-02-30 12:00:22"
|
|
||||||
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
|
||||||
|
|
||||||
output.should have_tag('select[id=person_birth_date_and_time_1i]') do
|
|
||||||
with_tag('option[selected=selected]', '2008')
|
|
||||||
end
|
|
||||||
output.should have_tag('select[id=person_birth_date_and_time_2i]') do
|
|
||||||
with_tag('option[selected=selected]', 'February')
|
|
||||||
end
|
|
||||||
output.should have_tag('select[id=person_birth_date_and_time_3i]') do
|
|
||||||
with_tag('option[selected=selected]', '30')
|
|
||||||
end
|
|
||||||
output.should have_tag('select[id=person_birth_date_and_time_4i]') do
|
|
||||||
with_tag('option[selected=selected]', '12')
|
|
||||||
end
|
|
||||||
output.should have_tag('select[id=person_birth_date_and_time_5i]') do
|
|
||||||
with_tag('option[selected=selected]', '00')
|
|
||||||
end
|
|
||||||
output.should have_tag('select[id=person_birth_date_and_time_6i]') do
|
|
||||||
with_tag('option[selected=selected]', '22')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should display datetime_select when datetime value is nil" do
|
|
||||||
@person.birth_date_and_time = nil
|
|
||||||
output = datetime_select(:person, :birth_date_and_time, :include_blank => true, :include_seconds => true)
|
|
||||||
output.should have_tag('select', 6)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
|
||||||
|
|
||||||
describe ValidatesTimeliness::MultiparameterAttributes do
|
|
||||||
def obj
|
|
||||||
@obj ||= Person.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should convert array for datetime type into datetime string" do
|
|
||||||
time_string = obj.time_array_to_string([2000,2,1,9,10,11], :datetime)
|
|
||||||
time_string.should == "2000-02-01 09:10:11"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should convert array for date type into date string" do
|
|
||||||
time_string = obj.time_array_to_string([2000,2,1], :date)
|
|
||||||
time_string.should == "2000-02-01"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should convert array for time type into time string" do
|
|
||||||
time_string = obj.time_array_to_string([2000,1,1,9,10,11], :time)
|
|
||||||
time_string.should == "09:10:11"
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "execute_callstack_for_multiparameter_attributes" do
|
|
||||||
before do
|
|
||||||
@callstack = {
|
|
||||||
'birth_date_and_time' => [2000,2,1,9,10,11],
|
|
||||||
'birth_date' => [2000,2,1,9,10,11],
|
|
||||||
'birth_time' => [2000,2,1,9,10,11]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should store datetime string for datetime column" do
|
|
||||||
obj.should_receive(:birth_date_and_time=).once.with("2000-02-01 09:10:11")
|
|
||||||
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should store date string for a date column" do
|
|
||||||
obj.should_receive(:birth_date=).once.with("2000-02-01")
|
|
||||||
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
|
||||||
end
|
|
||||||
|
|
||||||
unless RAILS_VER < '2.1' # sqlite doesn't support :time attribute in rails 2.0.x
|
|
||||||
it "should store time string for a time column" do
|
|
||||||
obj.should_receive(:birth_time=).once.with("09:10:11")
|
|
||||||
obj.send(:execute_callstack_for_multiparameter_attributes, @callstack)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
61
spec/parser_spec.rb
Normal file
61
spec/parser_spec.rb
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||||
|
|
||||||
|
describe ValidatesTimeliness::Parser do
|
||||||
|
attr_accessor :person
|
||||||
|
|
||||||
|
describe "parse" do
|
||||||
|
it "should return time object for valid time string" do
|
||||||
|
parse("2000-01-01 12:13:14", :datetime).should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return nil for time string with invalid date part" do
|
||||||
|
parse("2000-02-30 12:13:14", :datetime).should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return nil for time string with invalid time part" do
|
||||||
|
parse("2000-02-01 25:13:14", :datetime).should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return Time object when passed a Time object" do
|
||||||
|
parse(Time.now, :datetime).should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
|
if RAILS_VER >= '2.1'
|
||||||
|
it "should convert time string into current timezone" do
|
||||||
|
Time.zone = 'Melbourne'
|
||||||
|
time = parse("2000-01-01 12:13:14", :datetime)
|
||||||
|
Time.zone.utc_offset.should == 10.hours
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return nil for invalid date string" do
|
||||||
|
parse("2000-02-30", :date).should be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse(*args)
|
||||||
|
ValidatesTimeliness::Parser.parse(*args)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "make_time" do
|
||||||
|
|
||||||
|
if RAILS_VER >= '2.1'
|
||||||
|
|
||||||
|
it "should create time using current timezone" do
|
||||||
|
Time.zone = 'Melbourne'
|
||||||
|
time = ValidatesTimeliness::Parser.make_time([2000,1,1,12,0,0])
|
||||||
|
time.zone.should == "EST"
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
it "should create time using default timezone" do
|
||||||
|
time = ValidatesTimeliness::Parser.make_time([2000,1,1,12,0,0])
|
||||||
|
time.zone.should == "UTC"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
19
spec/resources/sqlite_patch.rb
Normal file
19
spec/resources/sqlite_patch.rb
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# patches adapter in rails 2.0 which mistakenly made time attributes map to datetime column type
|
||||||
|
ActiveRecord::ConnectionAdapters::SQLiteAdapter.class_eval do
|
||||||
|
def native_database_types #:nodoc:
|
||||||
|
{
|
||||||
|
:primary_key => default_primary_key_type,
|
||||||
|
:string => { :name => "varchar", :limit => 255 },
|
||||||
|
:text => { :name => "text" },
|
||||||
|
:integer => { :name => "integer" },
|
||||||
|
:float => { :name => "float" },
|
||||||
|
:decimal => { :name => "decimal" },
|
||||||
|
:datetime => { :name => "datetime" },
|
||||||
|
:timestamp => { :name => "datetime" },
|
||||||
|
:time => { :name => "time" },
|
||||||
|
:date => { :name => "date" },
|
||||||
|
:binary => { :name => "blob" },
|
||||||
|
:boolean => { :name => "boolean" }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
245
spec/spec/rails/matchers/validate_timeliness_spec.rb
Normal file
245
spec/spec/rails/matchers/validate_timeliness_spec.rb
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
||||||
|
require 'validates_timeliness/matcher'
|
||||||
|
|
||||||
|
class NoValidation < Person
|
||||||
|
end
|
||||||
|
|
||||||
|
class WithValidation < Person
|
||||||
|
validates_date :birth_date,
|
||||||
|
:is_at => '2000-01-01',
|
||||||
|
:before => '2000-01-10',
|
||||||
|
:after => '2000-01-01',
|
||||||
|
:on_or_before => '2000-01-09',
|
||||||
|
:on_or_after => '2000-01-02',
|
||||||
|
:between => ['2000-01-01', '2000-01-03']
|
||||||
|
|
||||||
|
validates_time :birth_time,
|
||||||
|
:is_at => '09:00',
|
||||||
|
:before => '23:00',
|
||||||
|
:after => '09:00',
|
||||||
|
:on_or_before => '22:00',
|
||||||
|
:on_or_after => '10:00',
|
||||||
|
:between => ['09:00', '17:00']
|
||||||
|
|
||||||
|
validates_datetime :birth_date_and_time,
|
||||||
|
:is_at => '2000-01-01 09:00',
|
||||||
|
:before => '2000-01-10 23:00',
|
||||||
|
:after => '2000-01-01 09:00',
|
||||||
|
:on_or_before => '2000-01-09 23:00',
|
||||||
|
:on_or_after => '2000-01-02 09:00',
|
||||||
|
:between => ['2000-01-01 09:00', '2000-01-01 17:00']
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class CustomMessages < Person
|
||||||
|
validates_date :birth_date,
|
||||||
|
:invalid_date_message => 'is not really a date',
|
||||||
|
:before => '2000-01-10',
|
||||||
|
:before_message => 'is too late',
|
||||||
|
:after => '2000-01-01',
|
||||||
|
:after_message => 'is too early',
|
||||||
|
:on_or_before => '2000-01-09',
|
||||||
|
:on_or_before_message => 'is just too late',
|
||||||
|
:on_or_after => '2000-01-02',
|
||||||
|
:on_or_after_message => 'is just too early'
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "ValidateTimeliness matcher" do
|
||||||
|
attr_accessor :no_validation, :with_validation
|
||||||
|
|
||||||
|
@@attribute_for_type = { :date => :birth_date, :time => :birth_time, :datetime => :birth_date_and_time }
|
||||||
|
|
||||||
|
before do
|
||||||
|
@no_validation = NoValidation.new
|
||||||
|
@with_validation = WithValidation.new
|
||||||
|
end
|
||||||
|
|
||||||
|
[:date, :time, :datetime].each do |type|
|
||||||
|
|
||||||
|
it "should report that #{type} is validated" do
|
||||||
|
with_validation.should self.send("validate_#{type}", attribute_for_type(type))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated" do
|
||||||
|
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "with is_at option" do
|
||||||
|
test_values = {
|
||||||
|
:date => ['2000-01-01', '2000-01-02'],
|
||||||
|
:time => ['09:00', '09:01'],
|
||||||
|
:datetime => ['2000-01-01 09:00', '2000-01-01 09:01']
|
||||||
|
}
|
||||||
|
|
||||||
|
[:date, :time, :datetime].each do |type|
|
||||||
|
|
||||||
|
it "should report that #{type} is validated" do
|
||||||
|
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][0])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated when option value is incorrect" do
|
||||||
|
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][1])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated with option" do
|
||||||
|
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :is_at => test_values[type][0])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "with before option" do
|
||||||
|
test_values = {
|
||||||
|
:date => ['2000-01-10', '2000-01-11'],
|
||||||
|
:time => ['23:00', '22:59'],
|
||||||
|
:datetime => ['2000-01-10 23:00', '2000-01-10 22:59']
|
||||||
|
}
|
||||||
|
|
||||||
|
[:date, :time, :datetime].each do |type|
|
||||||
|
|
||||||
|
it "should report that #{type} is validated" do
|
||||||
|
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][0])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated when option value is incorrect" do
|
||||||
|
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][1])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated with option" do
|
||||||
|
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :before => test_values[type][0])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "with after option" do
|
||||||
|
test_values = {
|
||||||
|
:date => ['2000-01-01', '2000-01-02'],
|
||||||
|
:time => ['09:00', '09:01'],
|
||||||
|
:datetime => ['2000-01-01 09:00', '2000-01-01 09:01']
|
||||||
|
}
|
||||||
|
|
||||||
|
[:date, :time, :datetime].each do |type|
|
||||||
|
|
||||||
|
it "should report that #{type} is validated" do
|
||||||
|
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][0])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated when option value is incorrect" do
|
||||||
|
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][1])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated with option" do
|
||||||
|
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :after => test_values[type][0])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "with on_or_before option" do
|
||||||
|
test_values = {
|
||||||
|
:date => ['2000-01-09', '2000-01-08'],
|
||||||
|
:time => ['22:00', '21:59'],
|
||||||
|
:datetime => ['2000-01-09 23:00', '2000-01-09 22:59']
|
||||||
|
}
|
||||||
|
|
||||||
|
[:date, :time, :datetime].each do |type|
|
||||||
|
|
||||||
|
it "should report that #{type} is validated" do
|
||||||
|
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][0])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated when option value is incorrect" do
|
||||||
|
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][1])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated with option" do
|
||||||
|
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_before => test_values[type][0])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "with on_or_after option" do
|
||||||
|
test_values = {
|
||||||
|
:date => ['2000-01-02', '2000-01-03'],
|
||||||
|
:time => ['10:00', '10:01'],
|
||||||
|
:datetime => ['2000-01-02 09:00', '2000-01-02 09:01']
|
||||||
|
}
|
||||||
|
|
||||||
|
[:date, :time, :datetime].each do |type|
|
||||||
|
|
||||||
|
it "should report that #{type} is validated" do
|
||||||
|
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][0])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated when option value is incorrect" do
|
||||||
|
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][1])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated with option" do
|
||||||
|
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :on_or_after => test_values[type][0])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "between option" do
|
||||||
|
test_values = {
|
||||||
|
:date => [ ['2000-01-01', '2000-01-03'], ['2000-01-01', '2000-01-04'] ],
|
||||||
|
:time => [ ['09:00', '17:00'], ['09:00', '17:01'] ],
|
||||||
|
:datetime => [ ['2000-01-01 09:00', '2000-01-01 17:00'], ['2000-01-01 09:00', '2000-01-01 17:01'] ]
|
||||||
|
}
|
||||||
|
|
||||||
|
[:date, :time, :datetime].each do |type|
|
||||||
|
|
||||||
|
it "should report that #{type} is validated" do
|
||||||
|
with_validation.should self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][0])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated when option value is incorrect" do
|
||||||
|
with_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][1])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should report that #{type} is not validated with option" do
|
||||||
|
no_validation.should_not self.send("validate_#{type}", attribute_for_type(type), :between => test_values[type][0])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "custom messages" do
|
||||||
|
|
||||||
|
before do
|
||||||
|
@person = CustomMessages.new
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should match error message for invalid" do
|
||||||
|
@person.should validate_date(:birth_date, :invalid_date_message => 'is not really a date')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should match error message for before option" do
|
||||||
|
@person.should validate_date(:birth_date, :before => '2000-01-10',
|
||||||
|
:invalid_date_message => 'is not really a date',
|
||||||
|
:before_message => 'is too late')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should match error message for after option" do
|
||||||
|
@person.should validate_date(:birth_date, :after => '2000-01-01',
|
||||||
|
:invalid_date_message => 'is not really a date',
|
||||||
|
:after_message => 'is too early')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should match error message for on_or_before option" do
|
||||||
|
@person.should validate_date(:birth_date, :on_or_before => '2000-01-09',
|
||||||
|
:invalid_date_message => 'is not really a date',
|
||||||
|
:on_or_before_message => 'is just too late')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should match error message for on_or_after option" do
|
||||||
|
@person.should validate_date(:birth_date, :on_or_after => '2000-01-02',
|
||||||
|
:invalid_date_message => 'is not really a date',
|
||||||
|
:on_or_after_message => 'is just too early')
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def attribute_for_type(type)
|
||||||
|
@@attribute_for_type[type.to_sym]
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -2,10 +2,10 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
|
|||||||
$:.unshift(File.dirname(__FILE__))
|
$:.unshift(File.dirname(__FILE__))
|
||||||
$:.unshift(File.dirname(__FILE__) + '/resources')
|
$:.unshift(File.dirname(__FILE__) + '/resources')
|
||||||
|
|
||||||
ENV['RAILS_ENV'] = 'test'
|
RAILS_ENV = ENV['RAILS_ENV'] = 'test'
|
||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'spec'
|
require 'spec/autorun'
|
||||||
|
|
||||||
vendored_rails = File.dirname(__FILE__) + '/../../../../vendor/rails'
|
vendored_rails = File.dirname(__FILE__) + '/../../../../vendor/rails'
|
||||||
|
|
||||||
@@ -13,10 +13,14 @@ if vendored = File.exists?(vendored_rails)
|
|||||||
Dir.glob(vendored_rails + "/**/lib").each { |dir| $:.unshift dir }
|
Dir.glob(vendored_rails + "/**/lib").each { |dir| $:.unshift dir }
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
require 'ginger'
|
require 'ginger'
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
end
|
end
|
||||||
gem 'rails'
|
if ENV['VERSION']
|
||||||
|
gem 'rails', ENV['VERSION']
|
||||||
|
else
|
||||||
|
gem 'rails'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
RAILS_ROOT = File.dirname(__FILE__)
|
RAILS_ROOT = File.dirname(__FILE__)
|
||||||
@@ -26,26 +30,29 @@ require 'active_record'
|
|||||||
require 'active_record/version'
|
require 'active_record/version'
|
||||||
require 'action_controller'
|
require 'action_controller'
|
||||||
require 'action_view'
|
require 'action_view'
|
||||||
|
require 'action_mailer'
|
||||||
ActiveSupport::Deprecation.silenced = true
|
|
||||||
|
|
||||||
require 'spec/rails'
|
require 'spec/rails'
|
||||||
require 'time_travel/time_travel'
|
require 'time_travel/time_travel'
|
||||||
require 'validates_timeliness'
|
|
||||||
|
|
||||||
RAILS_VER = Rails::VERSION::STRING
|
|
||||||
|
|
||||||
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{RAILS_VER} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
|
|
||||||
|
|
||||||
ActiveRecord::Base.default_timezone = :utc
|
ActiveRecord::Base.default_timezone = :utc
|
||||||
|
RAILS_VER = Rails::VERSION::STRING
|
||||||
|
puts "Using #{vendored ? 'vendored' : 'gem'} Rails version #{RAILS_VER} (ActiveRecord version #{ActiveRecord::VERSION::STRING})"
|
||||||
|
|
||||||
if RAILS_VER >= '2.1'
|
if RAILS_VER >= '2.1'
|
||||||
Time.zone_default = ActiveSupport::TimeZone['UTC']
|
Time.zone_default = ActiveSupport::TimeZone['UTC']
|
||||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
require 'validates_timeliness'
|
||||||
|
require 'validates_timeliness/matcher'
|
||||||
|
|
||||||
|
ValidatesTimeliness.enable_datetime_select_extension!
|
||||||
|
|
||||||
ActiveRecord::Migration.verbose = false
|
ActiveRecord::Migration.verbose = false
|
||||||
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
|
||||||
|
|
||||||
|
require 'sqlite_patch' if RAILS_VER < '2.1'
|
||||||
|
|
||||||
require 'schema'
|
require 'schema'
|
||||||
require 'person'
|
require 'person'
|
||||||
|
|||||||
@@ -1,177 +0,0 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
|
||||||
|
|
||||||
describe "ValidateTimeliness matcher" do
|
|
||||||
attr_accessor :no_validation, :with_validation
|
|
||||||
|
|
||||||
before do
|
|
||||||
class NoValidation < Person
|
|
||||||
alias_attribute :birth_datetime, :birth_date_and_time
|
|
||||||
end
|
|
||||||
|
|
||||||
class WithValidation < Person
|
|
||||||
validates_date :birth_date,
|
|
||||||
:before => '2000-01-10', :after => '2000-01-01',
|
|
||||||
:on_or_before => '2000-01-09', :on_or_after => '2000-01-02'
|
|
||||||
validates_time :birth_time,
|
|
||||||
:before => '23:00', :after => '09:00',
|
|
||||||
:on_or_before => '22:00', :on_or_after => '10:00'
|
|
||||||
validates_datetime :birth_date_and_time,
|
|
||||||
:before => '2000-01-10 23:00', :after => '2000-01-01 09:00',
|
|
||||||
:on_or_before => '2000-01-09 23:00', :on_or_after => '2000-01-02 09:00'
|
|
||||||
|
|
||||||
alias_attribute :birth_datetime, :birth_date_and_time
|
|
||||||
end
|
|
||||||
@no_validation = NoValidation.new
|
|
||||||
@with_validation = WithValidation.new
|
|
||||||
end
|
|
||||||
|
|
||||||
[:date, :time, :datetime].each do |type|
|
|
||||||
attribute = type == :datetime ? :date_and_time : type
|
|
||||||
|
|
||||||
it "should report that #{type} is validated" do
|
|
||||||
with_validation.should self.send("validate_#{type}", "birth_#{attribute}".to_sym)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated" do
|
|
||||||
no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}".to_sym)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with before option" do
|
|
||||||
test_values = {
|
|
||||||
:date => ['2000-01-10', '2000-01-11'],
|
|
||||||
:time => ['23:00', '22:59'],
|
|
||||||
:datetime => ['2000-01-10 23:00', '2000-01-10 22:59']
|
|
||||||
}
|
|
||||||
|
|
||||||
[:date, :time, :datetime].each do |type|
|
|
||||||
attribute = type == :datetime ? :date_and_time : type
|
|
||||||
|
|
||||||
it "should report that #{type} is validated" do
|
|
||||||
with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :before => test_values[type][0])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated when option value is incorrect" do
|
|
||||||
with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :before => test_values[type][1])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated with option" do
|
|
||||||
no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :before => test_values[type][0])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with after option" do
|
|
||||||
test_values = {
|
|
||||||
:date => ['2000-01-01', '2000-01-02'],
|
|
||||||
:time => ['09:00', '09:01'],
|
|
||||||
:datetime => ['2000-01-01 09:00', '2000-01-01 09:01']
|
|
||||||
}
|
|
||||||
|
|
||||||
[:date, :time, :datetime].each do |type|
|
|
||||||
attribute = type == :datetime ? :date_and_time : type
|
|
||||||
|
|
||||||
it "should report that #{type} is validated" do
|
|
||||||
with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][0])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated when option value is incorrect" do
|
|
||||||
with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][1])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated with option" do
|
|
||||||
no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :after => test_values[type][0])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with on_or_before option" do
|
|
||||||
test_values = {
|
|
||||||
:date => ['2000-01-09', '2000-01-08'],
|
|
||||||
:time => ['22:00', '21:59'],
|
|
||||||
:datetime => ['2000-01-09 23:00', '2000-01-09 22:59']
|
|
||||||
}
|
|
||||||
|
|
||||||
[:date, :time, :datetime].each do |type|
|
|
||||||
attribute = type == :datetime ? :date_and_time : type
|
|
||||||
|
|
||||||
it "should report that #{type} is validated" do
|
|
||||||
with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :on_or_before => test_values[type][0])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated when option value is incorrect" do
|
|
||||||
with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :on_or_before => test_values[type][1])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated with option" do
|
|
||||||
no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :on_or_before => test_values[type][0])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with on_or_after option" do
|
|
||||||
test_values = {
|
|
||||||
:date => ['2000-01-02', '2000-01-03'],
|
|
||||||
:time => ['10:00', '10:01'],
|
|
||||||
:datetime => ['2000-01-02 09:00', '2000-01-02 09:01']
|
|
||||||
}
|
|
||||||
|
|
||||||
[:date, :time, :datetime].each do |type|
|
|
||||||
attribute = type == :datetime ? :date_and_time : type
|
|
||||||
|
|
||||||
it "should report that #{type} is validated" do
|
|
||||||
with_validation.should self.send("validate_#{type}", "birth_#{attribute}", :on_or_after => test_values[type][0])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated when option value is incorrect" do
|
|
||||||
with_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :on_or_after => test_values[type][1])
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should report that #{type} is not validated with option" do
|
|
||||||
no_validation.should_not self.send("validate_#{type}", "birth_#{attribute}", :on_or_after => test_values[type][0])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "custom messages" do
|
|
||||||
before do
|
|
||||||
class CustomMessages < Person
|
|
||||||
validates_date :birth_date, :invalid_date_message => 'is not really a date',
|
|
||||||
:before => '2000-01-10', :before_message => 'is too late',
|
|
||||||
:after => '2000-01-01', :after_message => 'is too early',
|
|
||||||
:on_or_before=> '2000-01-09', :on_or_before_message => 'is just too late',
|
|
||||||
:on_or_after => '2000-01-02', :on_or_after_message => 'is just too early'
|
|
||||||
end
|
|
||||||
@person = CustomMessages.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should match error message for invalid" do
|
|
||||||
@person.should validate_date(:birth_date, :invalid_date_message => 'is not really a date')
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should match error message for before option" do
|
|
||||||
@person.should validate_date(:birth_date, :before => '2000-01-10',
|
|
||||||
:invalid_date_message => 'is not really a date',
|
|
||||||
:before_message => 'is too late')
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should match error message for after option" do
|
|
||||||
@person.should validate_date(:birth_date, :after => '2000-01-01',
|
|
||||||
:invalid_date_message => 'is not really a date',
|
|
||||||
:after_message => 'is too early')
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should match error message for on_or_before option" do
|
|
||||||
@person.should validate_date(:birth_date, :on_or_before => '2000-01-09',
|
|
||||||
:invalid_date_message => 'is not really a date',
|
|
||||||
:on_or_before_message => 'is just too late')
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should match error message for on_or_after option" do
|
|
||||||
@person.should validate_date(:birth_date, :on_or_after => '2000-01-02',
|
|
||||||
:invalid_date_message => 'is not really a date',
|
|
||||||
:on_or_after_message => 'is just too early')
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,507 +0,0 @@
|
|||||||
require File.dirname(__FILE__) + '/spec_helper'
|
|
||||||
|
|
||||||
describe ValidatesTimeliness::Validations do
|
|
||||||
attr_accessor :person
|
|
||||||
|
|
||||||
before :all do
|
|
||||||
# freezes time using time_travel plugin
|
|
||||||
Time.now = Time.utc(2000, 1, 1, 0, 0, 0)
|
|
||||||
end
|
|
||||||
|
|
||||||
after :all do
|
|
||||||
Time.now = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "parse_date_time" do
|
|
||||||
it "should return time object for valid time string" do
|
|
||||||
parse_method("2000-01-01 12:13:14", :datetime).should be_kind_of(Time)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return nil for time string with invalid date part" do
|
|
||||||
parse_method("2000-02-30 12:13:14", :datetime).should be_nil
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return nil for time string with invalid time part" do
|
|
||||||
parse_method("2000-02-01 25:13:14", :datetime).should be_nil
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return Time object when passed a Time object" do
|
|
||||||
parse_method(Time.now, :datetime).should be_kind_of(Time)
|
|
||||||
end
|
|
||||||
|
|
||||||
if RAILS_VER >= '2.1'
|
|
||||||
it "should convert time string into current timezone" do
|
|
||||||
Time.zone = 'Melbourne'
|
|
||||||
time = parse_method("2000-01-01 12:13:14", :datetime)
|
|
||||||
Time.zone.utc_offset.should == 10.hours
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return nil for invalid date string" do
|
|
||||||
parse_method("2000-02-30", :date).should be_nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_method(*args)
|
|
||||||
ActiveRecord::Base.parse_date_time(*args)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "timeliness_restriction_value" do
|
|
||||||
it "should return Time object when restriction is Time object" do
|
|
||||||
restriction_value(Time.now, person, :datetime).should be_kind_of(Time)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return Time object when restriction is string" do
|
|
||||||
restriction_value("2007-01-01 12:00", person, :datetime).should be_kind_of(Time)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return Time object when restriction is method symbol which returns Time object" do
|
|
||||||
person.stub!(:datetime_attr).and_return(Time.now)
|
|
||||||
restriction_value(:datetime_attr, person, :datetime).should be_kind_of(Time)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return Time object when restriction is method symbol which returns string" do
|
|
||||||
person.stub!(:datetime_attr).and_return("2007-01-01 12:00")
|
|
||||||
restriction_value(:datetime_attr, person, :datetime).should be_kind_of(Time)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return Time object when restriction is proc which returns Time object" do
|
|
||||||
restriction_value(lambda { Time.now }, person, :datetime).should be_kind_of(Time)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should return Time object when restriction is proc which returns string" do
|
|
||||||
restriction_value(lambda {"2007-01-01 12:00"}, person, :datetime).should be_kind_of(Time)
|
|
||||||
end
|
|
||||||
|
|
||||||
def restriction_value(*args)
|
|
||||||
ActiveRecord::Base.send(:timeliness_restriction_value, *args)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with no restrictions" do
|
|
||||||
before :all do
|
|
||||||
class BasicValidation < Person
|
|
||||||
validates_datetime :birth_date_and_time, :allow_blank => true
|
|
||||||
validates_date :birth_date, :allow_blank => true
|
|
||||||
validates_time :birth_time, :allow_blank => true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = BasicValidation.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error for invalid date component for datetime column" do
|
|
||||||
person.birth_date_and_time = "2000-01-32 01:02:03"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should == "is not a valid datetime"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error for invalid time component for datetime column" do
|
|
||||||
person.birth_date_and_time = "2000-01-01 25:02:03"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should == "is not a valid datetime"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error for invalid date value for date column" do
|
|
||||||
person.birth_date = "2000-01-32"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date).should == "is not a valid date"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error for invalid time value for time column" do
|
|
||||||
person.birth_time = "25:00"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_time).should == "is not a valid time"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have same value for before_type_cast after failed validation" do
|
|
||||||
person.birth_date_and_time = "2000-01-01 25:02:03"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.birth_date_and_time_before_type_cast.should == "2000-01-01 25:02:03"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid with valid values" do
|
|
||||||
person.birth_date_and_time = "2000-01-01 12:12:12"
|
|
||||||
person.birth_date = "2000-01-31"
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid with value out of range for Time class" do
|
|
||||||
person.birth_date_and_time = "1890-01-01 12:12:12"
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid with nil values when allow_blank is true" do
|
|
||||||
person.birth_date_and_time = nil
|
|
||||||
person.birth_date = nil
|
|
||||||
person.birth_time = nil
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "for datetime type" do
|
|
||||||
|
|
||||||
describe "with before and after restrictions" do
|
|
||||||
before :all do
|
|
||||||
class DateTimeBeforeAfter < Person
|
|
||||||
validates_datetime :birth_date_and_time,
|
|
||||||
:before => lambda { Time.now }, :after => lambda { 1.day.ago}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = DateTimeBeforeAfter.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when past :before restriction" do
|
|
||||||
person.birth_date_and_time = 1.minute.from_now
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/must be before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when before :after restriction" do
|
|
||||||
person.birth_date_and_time = 2.days.ago
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/must be after/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when on boundary of :before restriction" do
|
|
||||||
person.birth_date_and_time = Time.now
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/must be before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when on boundary of :after restriction" do
|
|
||||||
person.birth_date_and_time = 1.day.ago
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/must be after/)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with on_or_before and on_or_after restrictions" do
|
|
||||||
before :all do
|
|
||||||
class DateTimeOnOrBeforeAndAfter < Person
|
|
||||||
validates_datetime :birth_date_and_time, :type => :datetime,
|
|
||||||
:on_or_before => lambda { Time.now.at_midnight },
|
|
||||||
:on_or_after => lambda { 1.day.ago }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before do
|
|
||||||
@person = DateTimeOnOrBeforeAndAfter.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when past :on_or_before restriction" do
|
|
||||||
person.birth_date_and_time = Time.now.at_midnight + 1
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/must be on or before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when before :on_or_after restriction" do
|
|
||||||
person.birth_date_and_time = 1.days.ago - 1
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/must be on or after/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid when value equal to :on_or_before restriction" do
|
|
||||||
person.birth_date_and_time = Time.now.at_midnight
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid when value equal to :on_or_after restriction" do
|
|
||||||
person.birth_date_and_time = 1.day.ago
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "for date type" do
|
|
||||||
|
|
||||||
describe "with before and after restrictions" do
|
|
||||||
before :all do
|
|
||||||
class DateBeforeAfter < Person
|
|
||||||
validates_date :birth_date,
|
|
||||||
:before => 1.day.from_now,
|
|
||||||
:after => 1.day.ago
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = DateBeforeAfter.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when past :before restriction" do
|
|
||||||
person.birth_date = 2.days.from_now
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date).should match(/must be before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when before :after restriction" do
|
|
||||||
person.birth_date = 2.days.ago
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date).should match(/must be after/)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with on_or_before and on_or_after restrictions" do
|
|
||||||
before :all do
|
|
||||||
class DateOnOrBeforeAndAfter < Person
|
|
||||||
validates_date :birth_date,
|
|
||||||
:on_or_before => 1.day.from_now,
|
|
||||||
:on_or_after => 1.day.ago
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = DateOnOrBeforeAndAfter.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when past :on_or_before restriction" do
|
|
||||||
person.birth_date = 2.days.from_now
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date).should match(/must be on or before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when before :on_or_after restriction" do
|
|
||||||
person.birth_date = 2.days.ago
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date).should match(/must be on or after/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid when value equal to :on_or_before restriction" do
|
|
||||||
person.birth_date = 1.day.from_now
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid when value equal to :on_or_after restriction" do
|
|
||||||
person.birth_date = 1.day.ago
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "for time type" do
|
|
||||||
|
|
||||||
describe "with before and after restrictions" do
|
|
||||||
before :all do
|
|
||||||
class TimeBeforeAfter < Person
|
|
||||||
validates_time :birth_time,
|
|
||||||
:before => "23:00",
|
|
||||||
:after => "06:00"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = TimeBeforeAfter.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when on boundary of :before restriction" do
|
|
||||||
person.birth_time = "23:00"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_time).should match(/must be before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when on boundary of :after restriction" do
|
|
||||||
person.birth_time = "06:00am"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_time).should match(/must be after/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when past :before restriction" do
|
|
||||||
person.birth_time = "23:01"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_time).should match(/must be before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when before :after restriction" do
|
|
||||||
person.birth_time = "05:59"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_time).should match(/must be after/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should not have error when before :before restriction" do
|
|
||||||
person.birth_time = "22:59"
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when before :after restriction" do
|
|
||||||
person.birth_time = "06:01"
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with on_or_before and on_or_after restrictions" do
|
|
||||||
before :all do
|
|
||||||
class TimeOnOrBeforeAndAfter < Person
|
|
||||||
validates_time :birth_time,
|
|
||||||
:on_or_before => "23:00",
|
|
||||||
:on_or_after => "06:00"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = TimeOnOrBeforeAndAfter.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when past :on_or_before restriction" do
|
|
||||||
person.birth_time = "23:01"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_time).should match(/must be on or before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have error when before :on_or_after restriction" do
|
|
||||||
person.birth_time = "05:59"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_time).should match(/must be on or after/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid when on boundary of :on_or_before restriction" do
|
|
||||||
person.birth_time = "23:00"
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be valid when on boundary of :on_or_after restriction" do
|
|
||||||
person.birth_time = "06:00"
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "with mixed value and restriction types" do
|
|
||||||
before :all do
|
|
||||||
|
|
||||||
class MixedBeforeAndAfter < Person
|
|
||||||
validates_datetime :birth_date_and_time,
|
|
||||||
:before => Date.new(2000,1,2),
|
|
||||||
:after => lambda { "2000-01-01" }
|
|
||||||
validates_date :birth_date,
|
|
||||||
:on_or_before => lambda { "2000-01-01" },
|
|
||||||
:on_or_after => :birth_date_and_time
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = MixedBeforeAndAfter.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should correctly validate time attribute with Date restriction" do
|
|
||||||
person.birth_date_and_time = "2000-01-03 00:00:00"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/must be before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should correctly validate with proc restriction" do
|
|
||||||
person.birth_date_and_time = "2000-01-01 00:00:00"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/must be after/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should correctly validate date attribute with DateTime restriction" do
|
|
||||||
person.birth_date = "2000-01-03"
|
|
||||||
person.birth_date_and_time = "1890-01-01 00:00:00"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date).should match(/must be on or before/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should correctly validate date attribute with symbol restriction" do
|
|
||||||
person.birth_date = "2000-01-01"
|
|
||||||
person.birth_date_and_time = "2000-01-02 12:00:00"
|
|
||||||
person.should_not be_valid
|
|
||||||
person.errors.on(:birth_date).should match(/must be on or after/)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "ignoring restriction errors" do
|
|
||||||
before :all do
|
|
||||||
class BadRestriction < Person
|
|
||||||
validates_date :birth_date, :before => Proc.new { raise }
|
|
||||||
self.ignore_datetime_restriction_errors = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = BadRestriction.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have no errors when restriction is invalid" do
|
|
||||||
person.birth_date = '2000-01-01'
|
|
||||||
person.should be_valid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "restriction value error message" do
|
|
||||||
describe "default formats" do
|
|
||||||
before :all do
|
|
||||||
class DefaultFormats < Person
|
|
||||||
validates_datetime :birth_date_and_time, :allow_blank => true, :after => 1.day.from_now
|
|
||||||
validates_date :birth_date, :allow_blank => true, :after => 1.day.from_now
|
|
||||||
validates_time :birth_time, :allow_blank => true, :after => '23:59:59'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = DefaultFormats.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should format datetime value of restriction" do
|
|
||||||
person.birth_date_and_time = Time.now
|
|
||||||
person.save
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/after \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\Z/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should format date value of restriction" do
|
|
||||||
person.birth_date = Time.now
|
|
||||||
person.save
|
|
||||||
person.errors.on(:birth_date).should match(/after \d{4}-\d{2}-\d{2}\Z/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should format time value of restriction" do
|
|
||||||
person.birth_time = '12:00:00'
|
|
||||||
person.save
|
|
||||||
person.errors.on(:birth_time).should match(/after \d{2}:\d{2}:\d{2}\Z/)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "custom formats" do
|
|
||||||
before :all do
|
|
||||||
class CustomFormats < Person
|
|
||||||
validates_datetime :birth_date_and_time, :allow_blank => true, :after => 1.day.from_now
|
|
||||||
validates_date :birth_date, :allow_blank => true, :after => 1.day.from_now
|
|
||||||
validates_time :birth_time, :allow_blank => true, :after => '23:59:59'
|
|
||||||
end
|
|
||||||
|
|
||||||
ActiveRecord::Errors.date_time_error_value_formats = {
|
|
||||||
:time => '%H:%M %p',
|
|
||||||
:date => '%d-%m-%Y',
|
|
||||||
:datetime => '%d-%m-%Y %H:%M %p'
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
before :each do
|
|
||||||
@person = CustomFormats.new
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should format datetime value of restriction" do
|
|
||||||
person.birth_date_and_time = Time.now
|
|
||||||
person.save
|
|
||||||
person.errors.on(:birth_date_and_time).should match(/after \d{2}-\d{2}-\d{4} \d{2}:\d{2} (AM|PM)\Z/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should format date value of restriction" do
|
|
||||||
person.birth_date = Time.now
|
|
||||||
person.save
|
|
||||||
person.errors.on(:birth_date).should match(/after \d{2}-\d{2}-\d{4}\Z/)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should format time value of restriction" do
|
|
||||||
person.birth_time = '12:00:00'
|
|
||||||
person.save
|
|
||||||
person.errors.on(:birth_time).should match(/after \d{2}:\d{2} (AM|PM)\Z/)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
713
spec/validator_spec.rb
Normal file
713
spec/validator_spec.rb
Normal file
@@ -0,0 +1,713 @@
|
|||||||
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||||
|
|
||||||
|
describe ValidatesTimeliness::Validator do
|
||||||
|
attr_accessor :person, :validator
|
||||||
|
|
||||||
|
before :all do
|
||||||
|
# freezes time using time_travel plugin
|
||||||
|
Time.now = Time.utc(2000, 1, 1, 0, 0, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
after :all do
|
||||||
|
Time.now = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def person
|
||||||
|
@person ||= Person.new
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "option keys validation" do
|
||||||
|
before(:all) do
|
||||||
|
ActiveSupport::Deprecation.silenced = true
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
keys = ValidatesTimeliness::Validator::VALID_OPTION_KEYS - [:invalid_date_message, :invalid_time_message, :with_date, :with_time]
|
||||||
|
@valid_options = keys.inject({}) {|hash, opt| hash[opt] = nil; hash }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should raise error if invalid option key passed" do
|
||||||
|
@valid_options.update(:invalid_key => 'will not open lock')
|
||||||
|
lambda { Person.validates_datetime(@valid_options) }.should raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not raise error if option keys are valid" do
|
||||||
|
lambda { Person.validates_datetime(@valid_options) }.should_not raise_error(ArgumentError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should display deprecation notice for :equal_to" do
|
||||||
|
::ActiveSupport::Deprecation.should_receive(:warn)
|
||||||
|
Person.validates_datetime :equal_to => Time.now
|
||||||
|
end
|
||||||
|
|
||||||
|
after(:all) do
|
||||||
|
ActiveSupport::Deprecation.silenced = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "evaluate_option_value" do
|
||||||
|
it "should return Time object when restriction is Time object" do
|
||||||
|
evaluate_option_value(Time.now, :datetime).should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return Time object when restriction is string" do
|
||||||
|
evaluate_option_value("2007-01-01 12:00", :datetime).should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return Time object when restriction is method and method returns Time object" do
|
||||||
|
person.stub!(:datetime_attr).and_return(Time.now)
|
||||||
|
evaluate_option_value(:datetime_attr, :datetime).should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return Time object when restriction is method and method returns string" do
|
||||||
|
person.stub!(:datetime_attr).and_return("2007-01-01 12:00")
|
||||||
|
evaluate_option_value(:datetime_attr, :datetime).should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return Time object when restriction is proc which returns Time object" do
|
||||||
|
evaluate_option_value(lambda { Time.now }, :datetime).should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return Time object when restriction is proc which returns string" do
|
||||||
|
evaluate_option_value(lambda {"2007-01-01 12:00"}, :datetime).should be_kind_of(Time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return array of Time objects when restriction is array of Time objects" do
|
||||||
|
time1, time2 = Time.now, 1.day.ago
|
||||||
|
evaluate_option_value([time1, time2], :datetime).should == [time2, time1]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return array of Time objects when restriction is array of strings" do
|
||||||
|
time1, time2 = "2000-01-02", "2000-01-01"
|
||||||
|
evaluate_option_value([time1, time2], :datetime).should == [parse(time2, :datetime), parse(time1, :datetime)]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return array of Time objects when restriction is Range of Time objects" do
|
||||||
|
time1, time2 = Time.now, 1.day.ago
|
||||||
|
evaluate_option_value(time1..time2, :datetime).should == [time2, time1]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return array of Time objects when restriction is Range of time strings" do
|
||||||
|
time1, time2 = "2000-01-02", "2000-01-01"
|
||||||
|
evaluate_option_value(time1..time2, :datetime).should == [parse(time2, :datetime), parse(time1, :datetime)]
|
||||||
|
end
|
||||||
|
def evaluate_option_value(restriction, type)
|
||||||
|
configure_validator(:type => type)
|
||||||
|
validator.class.send(:evaluate_option_value, restriction, type, person)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with defaults" do
|
||||||
|
|
||||||
|
describe "for datetime type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:type => :datetime)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have invalid error when date component is invalid" do
|
||||||
|
validate_with(:birth_date_and_time, "2000-01-32 01:02:03")
|
||||||
|
should_have_error(:birth_date_and_time, :invalid_datetime)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have invalid error when time component is invalid" do
|
||||||
|
validate_with(:birth_date_and_time, "2000-01-01 25:02:03")
|
||||||
|
should_have_error(:birth_date_and_time, :invalid_datetime)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have blank error when value is nil" do
|
||||||
|
validate_with(:birth_date_and_time, nil)
|
||||||
|
should_have_error(:birth_date_and_time, :blank)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have no errors when value is valid" do
|
||||||
|
validate_with(:birth_date_and_time, "2000-01-01 12:00:00")
|
||||||
|
should_have_no_error(:birth_date_and_time, :invalid_datetime)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for date type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:type => :date)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have invalid error when value is invalid" do
|
||||||
|
validate_with(:birth_date, "2000-01-32")
|
||||||
|
should_have_error(:birth_date, :invalid_date)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have blank error when value is nil" do
|
||||||
|
validate_with(:birth_date, nil)
|
||||||
|
should_have_error(:birth_date, :blank)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have no error when value is valid" do
|
||||||
|
validate_with(:birth_date, "2000-01-31")
|
||||||
|
should_have_no_error(:birth_date, :invalid_date)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for time type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:type => :time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have invalid error when value is invalid" do
|
||||||
|
validate_with(:birth_time, "25:00")
|
||||||
|
should_have_error(:birth_time, :invalid_time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have blank error when value is nil" do
|
||||||
|
validate_with(:birth_time, nil)
|
||||||
|
should_have_error(:birth_time, :blank)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have no errors when value is valid" do
|
||||||
|
validate_with(:birth_date_and_time, "12:00")
|
||||||
|
should_have_no_error(:birth_time, :invalid_time)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with before and after restrictions" do
|
||||||
|
|
||||||
|
describe "for datetime type" do
|
||||||
|
before :each do
|
||||||
|
configure_validator(:before => lambda { Time.now }, :after => lambda { 1.day.ago})
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have before error when value is past :before restriction" do
|
||||||
|
validate_with(:birth_date_and_time, 1.minute.from_now)
|
||||||
|
should_have_error(:birth_date_and_time, :before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have before error when value is on boundary of :before restriction" do
|
||||||
|
validate_with(:birth_date_and_time, Time.now)
|
||||||
|
should_have_error(:birth_date_and_time, :before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have after error when value is before :after restriction" do
|
||||||
|
validate_with(:birth_date_and_time, 2.days.ago)
|
||||||
|
should_have_error(:birth_date_and_time, :after)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have after error when value is on boundary of :after restriction" do
|
||||||
|
validate_with(:birth_date_and_time, 1.day.ago)
|
||||||
|
should_have_error(:birth_date_and_time, :after)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for date type" do
|
||||||
|
before :each do
|
||||||
|
configure_validator(:before => 1.day.from_now, :after => 1.day.ago, :type => :date)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is past :before restriction" do
|
||||||
|
validate_with(:birth_date, 2.days.from_now)
|
||||||
|
should_have_error(:birth_date, :before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is before :after restriction" do
|
||||||
|
validate_with(:birth_date, 2.days.ago)
|
||||||
|
should_have_error(:birth_date, :after)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have no error when value is before :before restriction" do
|
||||||
|
validate_with(:birth_date, Time.now)
|
||||||
|
should_have_no_error(:birth_date, :before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have no error when value is after :after restriction" do
|
||||||
|
validate_with(:birth_date, Time.now)
|
||||||
|
should_have_no_error(:birth_date, :after)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for time type" do
|
||||||
|
before :each do
|
||||||
|
configure_validator(:before => "23:00", :after => "06:00", :type => :time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is on boundary of :before restriction" do
|
||||||
|
validate_with(:birth_time, "23:00")
|
||||||
|
should_have_error(:birth_time, :before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is on boundary of :after restriction" do
|
||||||
|
validate_with(:birth_time, "06:00")
|
||||||
|
should_have_error(:birth_time, :after)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is past :before restriction" do
|
||||||
|
validate_with(:birth_time, "23:01")
|
||||||
|
should_have_error(:birth_time, :before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is before :after restriction" do
|
||||||
|
validate_with(:birth_time, "05:59")
|
||||||
|
should_have_error(:birth_time, :after)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not have error when value is before :before restriction" do
|
||||||
|
validate_with(:birth_time, "22:59")
|
||||||
|
should_have_no_error(:birth_time, :before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is before :after restriction" do
|
||||||
|
validate_with(:birth_time, "06:01")
|
||||||
|
should_have_no_error(:birth_time, :before)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with between restriction" do
|
||||||
|
|
||||||
|
describe "for datetime type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:between => [1.day.ago.at_midnight, 1.day.from_now.at_midnight])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is before earlist :between restriction" do
|
||||||
|
validate_with(:birth_date_and_time, 2.days.ago)
|
||||||
|
should_have_error(:birth_date_and_time, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is after latest :between restriction" do
|
||||||
|
validate_with(:birth_date_and_time, 2.days.from_now)
|
||||||
|
should_have_error(:birth_date_and_time, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to earliest :between restriction" do
|
||||||
|
validate_with(:birth_date_and_time, 1.day.ago.at_midnight)
|
||||||
|
should_have_no_error(:birth_date_and_time, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to latest :between restriction" do
|
||||||
|
validate_with(:birth_date_and_time, 1.day.from_now.at_midnight)
|
||||||
|
should_have_no_error(:birth_date_and_time, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow a range for between restriction" do
|
||||||
|
configure_validator(:type => :datetime, :between => (1.day.ago.at_midnight)..(1.day.from_now.at_midnight))
|
||||||
|
validate_with(:birth_date_and_time, 1.day.from_now.at_midnight)
|
||||||
|
should_have_no_error(:birth_date_and_time, :between)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for date type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:type => :date, :between => [1.day.ago.to_date, 1.day.from_now.to_date])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is before earlist :between restriction" do
|
||||||
|
validate_with(:birth_date, 2.days.ago.to_date)
|
||||||
|
should_have_error(:birth_date, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is after latest :between restriction" do
|
||||||
|
validate_with(:birth_date, 2.days.from_now.to_date)
|
||||||
|
should_have_error(:birth_date, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to earliest :between restriction" do
|
||||||
|
validate_with(:birth_date, 1.day.ago.to_date)
|
||||||
|
should_have_no_error(:birth_date, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to latest :between restriction" do
|
||||||
|
validate_with(:birth_date, 1.day.from_now.to_date)
|
||||||
|
should_have_no_error(:birth_date, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow a range for between restriction" do
|
||||||
|
configure_validator(:type => :date, :between => (1.day.ago.to_date)..(1.day.from_now.to_date))
|
||||||
|
validate_with(:birth_date, 1.day.from_now.to_date)
|
||||||
|
should_have_no_error(:birth_date, :between)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for time type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:type => :time, :between => ["09:00", "17:00"])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is before earlist :between restriction" do
|
||||||
|
validate_with(:birth_time, "08:59")
|
||||||
|
should_have_error(:birth_time, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is after latest :between restriction" do
|
||||||
|
validate_with(:birth_time, "17:01")
|
||||||
|
should_have_error(:birth_time, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to earliest :between restriction" do
|
||||||
|
validate_with(:birth_time, "09:00")
|
||||||
|
should_have_no_error(:birth_time, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to latest :between restriction" do
|
||||||
|
validate_with(:birth_time, "17:00")
|
||||||
|
should_have_no_error(:birth_time, :between)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow a range for between restriction" do
|
||||||
|
configure_validator(:type => :time, :between => "09:00".."17:00")
|
||||||
|
validate_with(:birth_time, "17:00")
|
||||||
|
should_have_no_error(:birth_time, :between)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with :is_at restriction" do
|
||||||
|
|
||||||
|
describe "for datetime type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:is_at => Time.now)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value not equal to :is_at restriction" do
|
||||||
|
validate_with(:birth_date_and_time, Time.now + 1)
|
||||||
|
should_have_error(:birth_date_and_time, :is_at)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to :is_at restriction" do
|
||||||
|
validate_with(:birth_date_and_time, Time.now)
|
||||||
|
should_have_no_error(:birth_date_and_time, :is_at)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for date type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:type => :date, :is_at => Date.today)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is not equal to :is_at restriction" do
|
||||||
|
validate_with(:birth_date, Date.today + 1)
|
||||||
|
should_have_error(:birth_date, :is_at)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to :is_at restriction" do
|
||||||
|
validate_with(:birth_date, Date.today)
|
||||||
|
should_have_no_error(:birth_date, :is_at)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "for time type" do
|
||||||
|
before do
|
||||||
|
configure_validator(:type => :time, :is_at => "09:00:00")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have error when value is not equal to :is_at restriction" do
|
||||||
|
validate_with(:birth_time, "09:00:01")
|
||||||
|
should_have_error(:birth_time, :is_at)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be valid when value is equal to :is_at restriction" do
|
||||||
|
validate_with(:birth_time, "09:00:00")
|
||||||
|
should_have_no_error(:birth_time, :is_at)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with :ignore_usec option" do
|
||||||
|
|
||||||
|
it "should ignore usec on time values when evaluated" do
|
||||||
|
configure_validator(:is_at => Time.utc(2000, 1, 1, 0, 0, 0, 0), :ignore_usec => true)
|
||||||
|
validate_with(:birth_date_and_time, Time.utc(2000, 1, 1, 0, 0, 0, 500))
|
||||||
|
should_have_no_error(:birth_date_and_time, :is_at)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with :with_time option" do
|
||||||
|
|
||||||
|
it "should validate date attribute as datetime combining value of :with_time against restrictions " do
|
||||||
|
configure_validator(:type => :date, :with_time => '12:31', :on_or_before => Time.mktime(2000,1,1,12,30))
|
||||||
|
validate_with(:birth_date, "2000-01-01")
|
||||||
|
should_have_error(:birth_date, :on_or_before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should skip restriction validation if :with_time value is nil" do
|
||||||
|
configure_validator(:type => :date, :with_time => nil, :on_or_before => Time.mktime(2000,1,1,12,30))
|
||||||
|
validate_with(:birth_date, "2000-01-01")
|
||||||
|
should_have_no_error(:birth_date, :on_or_before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should should ignore usec value on combined value if :ignore_usec option is true" do
|
||||||
|
configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :is_at => Time.mktime(2000,1,1,12,30), :ignore_usec => true)
|
||||||
|
validate_with(:birth_date, "2000-01-01")
|
||||||
|
should_have_no_error(:birth_date, :is_at)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with :with_date option" do
|
||||||
|
|
||||||
|
it "should validate time attribute as datetime combining value of :with_date against restrictions " do
|
||||||
|
configure_validator(:type => :time, :with_date => '2009-01-01', :on_or_before => Time.mktime(2000,1,1,12,30))
|
||||||
|
validate_with(:birth_time, "12:30")
|
||||||
|
should_have_error(:birth_time, :on_or_before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should skip restriction validation if :with_date value is nil" do
|
||||||
|
configure_validator(:type => :time, :with_date => nil, :on_or_before => Time.mktime(2000,1,1,12,30))
|
||||||
|
validate_with(:birth_time, "12:30")
|
||||||
|
should_have_no_error(:birth_time, :on_or_before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should should ignore usec value on combined value if :ignore_usec option is true" do
|
||||||
|
configure_validator(:type => :time, :with_date => Date.new(2000,1,1), :on_or_before => Time.mktime(2000,1,1,12,30), :ignore_usec => true)
|
||||||
|
validate_with(:birth_time, Time.mktime(2000,1,1,12,30,0,50))
|
||||||
|
should_have_no_error(:birth_time, :on_or_before)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with mixed value and restriction types" do
|
||||||
|
|
||||||
|
it "should validate datetime attribute with Date restriction" do
|
||||||
|
configure_validator(:type => :datetime, :on_or_before => Date.new(2000,1,1))
|
||||||
|
validate_with(:birth_date_and_time, "2000-01-01 00:00:00")
|
||||||
|
should_have_no_error(:birth_date_and_time, :on_or_before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should validate date attribute with DateTime restriction value" do
|
||||||
|
configure_validator(:type => :date, :on_or_before => DateTime.new(2000, 1, 1, 0,0,0))
|
||||||
|
validate_with(:birth_date, "2000-01-01")
|
||||||
|
should_have_no_error(:birth_date, :on_or_before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should validate date attribute with Time restriction value" do
|
||||||
|
configure_validator(:type => :date, :on_or_before => Time.utc(2000, 1, 1, 0,0,0))
|
||||||
|
validate_with(:birth_date, "2000-01-01")
|
||||||
|
should_have_no_error(:birth_date, :on_or_before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should validate time attribute with DateTime restriction value" do
|
||||||
|
configure_validator(:type => :time, :on_or_before => DateTime.new(2000, 1, 1, 12,0,0))
|
||||||
|
validate_with(:birth_time, "12:00")
|
||||||
|
should_have_no_error(:birth_time, :on_or_before)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should validate time attribute with Time restriction value" do
|
||||||
|
configure_validator(:type => :time, :on_or_before => Time.utc(2000, 1, 1, 12,0,0))
|
||||||
|
validate_with(:birth_time, "12:00")
|
||||||
|
should_have_no_error(:birth_time, :on_or_before)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "instance with format option" do
|
||||||
|
it "should validate attribute when value matches format" do
|
||||||
|
configure_validator(:type => :time, :format => 'hh:nn:ss')
|
||||||
|
validate_with(:birth_time, "12:00:00")
|
||||||
|
should_have_no_error(:birth_time, :invalid_time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not validate attribute when value does not match format" do
|
||||||
|
configure_validator(:type => :time, :format => 'hh:nn:ss')
|
||||||
|
validate_with(:birth_time, "12:00")
|
||||||
|
should_have_error(:birth_time, :invalid_time)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if defined?(I18n)
|
||||||
|
|
||||||
|
describe "localized error messages" do
|
||||||
|
before(:all) do
|
||||||
|
translations = {
|
||||||
|
:activerecord => {:errors => {:messages => { :after => 'retfa {{restriction}}' }}},
|
||||||
|
:validates_timeliness => {:error_value_formats => {}}
|
||||||
|
}
|
||||||
|
I18n.backend.store_translations 'zz', translations
|
||||||
|
I18n.locale = :zz
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be used if defined" do
|
||||||
|
configure_validator(:type => :date, :after => Date.today)
|
||||||
|
validate_with(:birth_date, 1.day.ago)
|
||||||
|
person.errors.on(:birth_date).should match(/retfa/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should use I18n translation missing message when not defined" do
|
||||||
|
configure_validator(:type => :date, :on_or_after => Date.today)
|
||||||
|
validate_with(:birth_date, 1.day.ago)
|
||||||
|
person.errors.on(:birth_date).should match(/translation missing/)
|
||||||
|
end
|
||||||
|
|
||||||
|
after(:all) do
|
||||||
|
I18n.locale = :en
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "custom_error_messages" do
|
||||||
|
it "should return hash of custom error messages from configuration with _message truncated from keys" do
|
||||||
|
configure_validator(:type => :date, :invalid_date_message => 'thats no date')
|
||||||
|
validator.send(:custom_error_messages)[:invalid_date].should == 'thats no date'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return empty hash if no custom error messages in configuration" do
|
||||||
|
configure_validator(:type => :date)
|
||||||
|
validator.send(:custom_error_messages).should be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "interpolation_values" do
|
||||||
|
if defined?(I18n)
|
||||||
|
it "should return hash of interpolation keys with restriction values" do
|
||||||
|
before = '1900-01-01'
|
||||||
|
configure_validator(:type => :date, :before => before)
|
||||||
|
validator.send(:interpolation_values, :before, before.to_date).should == {:restriction => before}
|
||||||
|
end
|
||||||
|
else
|
||||||
|
it "should return array of interpolation values" do
|
||||||
|
before = '1900-01-01'
|
||||||
|
configure_validator(:type => :date, :before => before)
|
||||||
|
validator.send(:interpolation_values, :before, before.to_date).should == [before]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "restriction errors" do
|
||||||
|
before :each do
|
||||||
|
configure_validator(:type => :date, :before => lambda { raise })
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be added by default for invalid restriction" do
|
||||||
|
ValidatesTimeliness::Validator.ignore_restriction_errors = false
|
||||||
|
validate_with(:birth_date, Date.today)
|
||||||
|
person.errors.on(:birth_date).should match(/restriction 'before' value was invalid/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not be added when ignore switch is true and restriction is invalid" do
|
||||||
|
ValidatesTimeliness::Validator.ignore_restriction_errors = true
|
||||||
|
person.should be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
after :all do
|
||||||
|
ValidatesTimeliness::Validator.ignore_restriction_errors = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "restriction value error message" do
|
||||||
|
|
||||||
|
describe "default formats" do
|
||||||
|
|
||||||
|
it "should format datetime value of restriction" do
|
||||||
|
configure_validator(:type => :datetime, :after => 1.day.from_now)
|
||||||
|
validate_with(:birth_date_and_time, Time.now)
|
||||||
|
person.errors.on(:birth_date_and_time).should match(/after \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\Z/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should format date value of restriction" do
|
||||||
|
configure_validator(:type => :date, :after => 1.day.from_now)
|
||||||
|
validate_with(:birth_date, Time.now)
|
||||||
|
person.errors.on(:birth_date).should match(/after \d{4}-\d{2}-\d{2}\Z/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should format time value of restriction" do
|
||||||
|
configure_validator(:type => :time, :after => '12:00')
|
||||||
|
validate_with(:birth_time, '11:59')
|
||||||
|
person.errors.on(:birth_time).should match(/after \d{2}:\d{2}:\d{2}\Z/)
|
||||||
|
end
|
||||||
|
|
||||||
|
if defined?(I18n)
|
||||||
|
|
||||||
|
describe "I18n" do
|
||||||
|
it "should use global default if locale format missing" do
|
||||||
|
I18n.backend.store_translations 'zz', :activerecord => {:errors => {:messages => { :after => 'after {{restriction}}' }}}
|
||||||
|
I18n.locale = :zz
|
||||||
|
configure_validator(:type => :datetime, :after => 1.day.from_now)
|
||||||
|
validate_with(:birth_date_and_time, Time.now)
|
||||||
|
person.errors.on(:birth_date_and_time).should match(/after \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\Z/)
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
I18n.locale = :en
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "custom formats" do
|
||||||
|
|
||||||
|
before :all do
|
||||||
|
custom = {
|
||||||
|
:time => '%H:%M %p',
|
||||||
|
:date => '%d-%m-%Y',
|
||||||
|
:datetime => '%d-%m-%Y %H:%M %p'
|
||||||
|
}
|
||||||
|
|
||||||
|
if defined?(I18n)
|
||||||
|
I18n.backend.store_translations 'en', :validates_timeliness => { :error_value_formats => custom }
|
||||||
|
else
|
||||||
|
@@formats = ValidatesTimeliness::Validator.error_value_formats
|
||||||
|
ValidatesTimeliness::Validator.error_value_formats = custom
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should format datetime value of restriction" do
|
||||||
|
configure_validator(:type => :datetime, :after => 1.day.from_now)
|
||||||
|
validate_with(:birth_date_and_time, Time.now)
|
||||||
|
person.errors.on(:birth_date_and_time).should match(/after \d{2}-\d{2}-\d{4} \d{2}:\d{2} (AM|PM)\Z/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should format date value of restriction" do
|
||||||
|
configure_validator(:type => :date, :after => 1.day.from_now)
|
||||||
|
validate_with(:birth_date, Time.now)
|
||||||
|
person.errors.on(:birth_date).should match(/after \d{2}-\d{2}-\d{4}\Z/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should format time value of restriction" do
|
||||||
|
configure_validator(:type => :time, :after => '12:00')
|
||||||
|
validate_with(:birth_time, '11:59')
|
||||||
|
person.errors.on(:birth_time).should match(/after \d{2}:\d{2} (AM|PM)\Z/)
|
||||||
|
end
|
||||||
|
|
||||||
|
after :all do
|
||||||
|
if defined?(I18n)
|
||||||
|
I18n.reload!
|
||||||
|
else
|
||||||
|
ValidatesTimeliness::Validator.error_value_formats = @@formats
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse(*args)
|
||||||
|
ValidatesTimeliness::Parser.parse(*args)
|
||||||
|
end
|
||||||
|
|
||||||
|
def configure_validator(options={})
|
||||||
|
@validator = ValidatesTimeliness::Validator.new(options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_with(attr_name, value)
|
||||||
|
person.send("#{attr_name}=", value)
|
||||||
|
validator.call(person, attr_name, value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def should_have_error(attr_name, error)
|
||||||
|
message = error_messages[error]
|
||||||
|
person.errors.on(attr_name).should match(/#{message}/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def should_have_no_error(attr_name, error)
|
||||||
|
message = error_messages[error]
|
||||||
|
errors = person.errors.on(attr_name)
|
||||||
|
if errors
|
||||||
|
errors.should_not match(/#{message}/)
|
||||||
|
else
|
||||||
|
errors.should be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def error_messages
|
||||||
|
return @error_messages if defined?(@error_messages)
|
||||||
|
messages = defined?(I18n) ? I18n.t('activerecord.errors.messages') : validator.send(:error_messages)
|
||||||
|
@error_messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub(/ (\%s|\{\{\w*\}\}).*/, ''); h }
|
||||||
|
end
|
||||||
|
end
|
||||||
30
validates_timeliness.gemspec
Normal file
30
validates_timeliness.gemspec
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
Gem::Specification.new do |s|
|
||||||
|
s.name = %q{validates_timeliness}
|
||||||
|
s.version = "2.3.1"
|
||||||
|
|
||||||
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||||
|
s.authors = ["Adam Meehan"]
|
||||||
|
s.autorequire = %q{validates_timeliness}
|
||||||
|
s.date = %q{2010-03-19}
|
||||||
|
s.description = %q{Date and time validation plugin for Rails 2.x which allows custom formats}
|
||||||
|
s.email = %q{adam.meehan@gmail.com}
|
||||||
|
s.extra_rdoc_files = ["README.rdoc", "LICENSE", "TODO", "CHANGELOG"]
|
||||||
|
s.files = ["LICENSE", "README.rdoc", "Rakefile", "TODO", "CHANGELOG", "lib/validates_timeliness", "lib/validates_timeliness/action_view", "lib/validates_timeliness/action_view/instance_tag.rb", "lib/validates_timeliness/active_record", "lib/validates_timeliness/active_record/attribute_methods.rb", "lib/validates_timeliness/active_record/multiparameter_attributes.rb", "lib/validates_timeliness/formats.rb", "lib/validates_timeliness/locale", "lib/validates_timeliness/locale/en.yml", "lib/validates_timeliness/matcher.rb", "lib/validates_timeliness/parser.rb", "lib/validates_timeliness/spec", "lib/validates_timeliness/spec/rails", "lib/validates_timeliness/spec/rails/matchers", "lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb", "lib/validates_timeliness/validation_methods.rb", "lib/validates_timeliness/validator.rb", "lib/validates_timeliness/version.rb", "lib/validates_timeliness.rb", "spec/action_view", "spec/action_view/instance_tag_spec.rb", "spec/active_record", "spec/active_record/attribute_methods_spec.rb", "spec/active_record/multiparameter_attributes_spec.rb", "spec/formats_spec.rb", "spec/ginger_scenarios.rb", "spec/parser_spec.rb", "spec/resources", "spec/resources/application.rb", "spec/resources/person.rb", "spec/resources/schema.rb", "spec/resources/sqlite_patch.rb", "spec/spec", "spec/spec/rails", "spec/spec/rails/matchers", "spec/spec/rails/matchers/validate_timeliness_spec.rb", "spec/spec_helper.rb", "spec/time_travel", "spec/time_travel/MIT-LICENSE", "spec/time_travel/time_extensions.rb", "spec/time_travel/time_travel.rb", "spec/validator_spec.rb"]
|
||||||
|
s.homepage = %q{http://github.com/adzap/validates_timeliness}
|
||||||
|
s.require_paths = ["lib"]
|
||||||
|
s.rubyforge_project = %q{validatestime}
|
||||||
|
s.rubygems_version = %q{1.3.5}
|
||||||
|
s.summary = %q{Date and time validation plugin for Rails 2.x which allows custom formats}
|
||||||
|
|
||||||
|
if s.respond_to? :specification_version then
|
||||||
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
||||||
|
s.specification_version = 3
|
||||||
|
|
||||||
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
||||||
|
else
|
||||||
|
end
|
||||||
|
else
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user