jc-validates_timeliness 3.1.0

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