validates_timeliness 4.1.1 → 6.0.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +7 -0
  3. data/.github/workflows/ci.yml +41 -0
  4. data/CHANGELOG.rdoc +14 -6
  5. data/LICENSE +1 -1
  6. data/README.md +321 -0
  7. data/Rakefile +0 -10
  8. data/gemfiles/rails_6_0.gemfile +14 -0
  9. data/gemfiles/rails_6_1.gemfile +14 -0
  10. data/gemfiles/rails_edge.gemfile +14 -0
  11. data/lib/validates_timeliness/attribute_methods.rb +34 -73
  12. data/lib/validates_timeliness/{conversion.rb → converter.rb} +26 -14
  13. data/lib/validates_timeliness/extensions/date_time_select.rb +23 -27
  14. data/lib/validates_timeliness/extensions/multiparameter_handler.rb +47 -66
  15. data/lib/validates_timeliness/extensions.rb +2 -2
  16. data/lib/validates_timeliness/orm/active_model.rb +53 -2
  17. data/lib/validates_timeliness/orm/active_record.rb +2 -84
  18. data/lib/validates_timeliness/railtie.rb +1 -1
  19. data/lib/validates_timeliness/validator.rb +21 -18
  20. data/lib/validates_timeliness/version.rb +1 -1
  21. data/lib/validates_timeliness.rb +3 -3
  22. data/spec/spec_helper.rb +2 -0
  23. data/spec/support/model_helpers.rb +1 -2
  24. data/spec/support/test_model.rb +6 -4
  25. data/spec/validates_timeliness/attribute_methods_spec.rb +0 -15
  26. data/spec/validates_timeliness/{conversion_spec.rb → converter_spec.rb} +64 -49
  27. data/spec/validates_timeliness/extensions/date_time_select_spec.rb +27 -27
  28. data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +10 -10
  29. data/spec/validates_timeliness/orm/active_record_spec.rb +72 -136
  30. data/validates_timeliness.gemspec +4 -3
  31. metadata +38 -16
  32. data/.travis.yml +0 -24
  33. data/README.rdoc +0 -300
  34. data/gemfiles/rails_4_2.gemfile +0 -14
data/README.rdoc DELETED
@@ -1,300 +0,0 @@
1
- = ValidatesTimeliness
2
-
3
- * Source: http://github.com/adzap/validates_timeliness
4
- * Issues: http://github.com/adzap/validates_timeliness/issues
5
-
6
- == Description
7
-
8
- Complete validation of dates, times and datetimes for Rails 4.2.x and ActiveModel. Rails 4.0.x and 4.1.x may
9
- still work but official support has ended.
10
-
11
- If you a looking for the old version for Rails 3.x go here[http://github.com/adzap/validates_timeliness/tree/v3.x].
12
-
13
-
14
- == Features
15
-
16
- * Adds validation for dates, times and datetimes to ActiveModel
17
-
18
- * Handles timezones and type casting of values for you
19
-
20
- * Only Rails date/time validation plugin offering complete validation (See ORM/ODM support)
21
-
22
- * Uses extensible date/time parser (Using {timeliness gem}[http://github.com/adzap/timeliness]. See Plugin Parser)
23
-
24
- * Adds extensions to fix Rails date/time select issues (See Extensions)
25
-
26
- * Supports I18n for the error messages. For multi-language support try {timeliness-i18n gem}[https://github.com/pedrofurtado/timeliness-i18n].
27
-
28
- * Supports all the Rubies (that any sane person would be using in production).
29
-
30
-
31
- == Installation
32
-
33
- # in Gemfile
34
- gem 'validates_timeliness', '~> 4.1'
35
-
36
- # Run bundler
37
- $ bundle install
38
-
39
- Then run
40
-
41
- $ rails generate validates_timeliness:install
42
-
43
- This creates configuration initializer and locale files. In the initializer, there are a number of config
44
- options to customize the plugin.
45
-
46
- NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
47
-
48
-
49
- == Examples
50
-
51
- validates_datetime :occurred_at
52
-
53
- validates_date :date_of_birth, :before => lambda { 18.years.ago },
54
- :before_message => "must be at least 18 years old"
55
-
56
- validates_datetime :finish_time, :after => :start_time # Method symbol
57
-
58
- validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
59
-
60
- validates_time :booked_at, :between => ['9:00am', '5:00pm'] # On or after 9:00AM and on or before 5:00PM
61
- validates_time :booked_at, :between => '9:00am'..'5:00pm' # The same as previous example
62
- validates_time :booked_at, :between => '9:00am'...'5:00pm' # On or after 9:00AM and strictly before 5:00PM
63
-
64
- validates_time :breakfast_time, :on_or_after => '6:00am',
65
- :on_or_after_message => 'must be after opening time',
66
- :before => :lunchtime,
67
- :before_message => 'must be before lunch time'
68
-
69
-
70
- == Usage
71
-
72
- To validate a model with a date, time or datetime attribute you just use the
73
- validation method
74
-
75
- class Person < ActiveRecord::Base
76
- validates_date :date_of_birth, :on_or_before => lambda { Date.current }
77
- # or
78
- validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
79
- end
80
-
81
- or even on a specific record, per ActiveModel API.
82
-
83
- @person.validates_date :date_of_birth, :on_or_before => lambda { Date.current }
84
-
85
-
86
- The list of validation methods available are as follows:
87
- validates_date - validate value as date
88
- validates_time - validate value as time only i.e. '12:20pm'
89
- validates_datetime - validate value as a full date and time
90
- validates - use the :timeliness key and set the type in the hash.
91
-
92
- The validation methods take the usual options plus some specific ones to restrict
93
- the valid range of dates or times allowed
94
-
95
- Temporal options (or restrictions):
96
- :is_at - Attribute must be equal to value to be valid
97
- :before - Attribute must be before this value to be valid
98
- :on_or_before - Attribute must be equal to or before this value to be valid
99
- :after - Attribute must be after this value to be valid
100
- :on_or_after - Attribute must be equal to or after this value to be valid
101
- :between - Attribute must be between the values to be valid. Range or Array of 2 values.
102
-
103
- Regular validation options:
104
- :allow_nil - Allow a nil value to be valid
105
- :allow_blank - Allows a nil or empty string value to be valid
106
- :if - Execute validation when :if evaluates true
107
- :unless - Execute validation when :unless evaluates false
108
- :on - Specify validation context e.g :save, :create or :update. Default is :save.
109
-
110
- Special options:
111
- :ignore_usec - Ignores microsecond value on datetime restrictions
112
- :format - Limit validation to a single format for special cases. Requires plugin parser.
113
-
114
- The temporal restrictions can take 4 different value types:
115
-
116
- * Date, Time, or DateTime object value
117
- * Proc or lambda object which may take an optional parameter, being the record object
118
- * A symbol matching a method name in the model
119
- * String value
120
-
121
- When an attribute value is compared to temporal restrictions, they are compared as
122
- the same type as the validation method type. So using validates_date means all
123
- values are compared as dates.
124
-
125
-
126
- == Configuration
127
-
128
- === ORM/ODM Support
129
-
130
- The plugin adds date/time validation to ActiveModel for any ORM/ODM that supports the ActiveModel validations component.
131
- However, there is an issue with most ORM/ODMs which does not allow 100% date/time validation by default. Specifically, when you
132
- assign an invalid date/time value to an attribute, most ORM/ODMs will only store a nil value for the attribute. This causes an
133
- 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
134
- the original invalid value to know that the attribute is not just nil.
135
-
136
- Each ORM/ODM requires a specific shim to fix it. The plugin includes a shim for ActiveRecord and Mongoid. You can activate them
137
- like so
138
-
139
- ValidatesTimeliness.setup do |config|
140
-
141
- # Extend ORM/ODMs for full support (:active_record).
142
- config.extend_orms = [ :active_record ]
143
-
144
- end
145
-
146
- 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.
147
-
148
- 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.
149
-
150
-
151
- === Error Messages
152
-
153
- Using the I18n system to define new defaults:
154
-
155
- en:
156
- errors:
157
- messages:
158
- invalid_date: "is not a valid date"
159
- invalid_time: "is not a valid time"
160
- invalid_datetime: "is not a valid datetime"
161
- is_at: "must be at %{restriction}"
162
- before: "must be before %{restriction}"
163
- on_or_before: "must be on or before %{restriction}"
164
- after: "must be after %{restriction}"
165
- on_or_after: "must be on or after %{restriction}"
166
-
167
- The %{restriction} signifies where the interpolation value for the restriction will be inserted.
168
-
169
- You can also use validation options for custom error messages. The following option keys are available:
170
-
171
- :invalid_date_message
172
- :invalid_time_message
173
- :invalid_datetime_message
174
- :is_at_message
175
- :before_message
176
- :on_or_before_message
177
- :after_message
178
- :on_or_after_message
179
-
180
- Note: There is no :between_message option. The between error message should be defined using the :on_or_after and :on_or_before
181
- (:before in case when :between argument is a Range with excluded high value, see Examples) messages.
182
-
183
- It is highly recommended you use the I18n system for error messages.
184
-
185
-
186
- === Plugin Parser
187
-
188
- The plugin uses the {timeliness gem}[http://github.com/adzap/timeliness] as a fast, configurable and extensible date and time parser.
189
- You can add or remove valid formats for dates, times, and datetimes. It is also more strict than the
190
- Ruby parser, which means it won't accept day of the month if it's not a valid number for the month.
191
-
192
- By default the parser is disabled. To enable it:
193
-
194
- # in the setup block
195
- config.use_plugin_parser = true
196
-
197
- Enabling the parser will mean that strings assigned to attributes validated with the plugin will be parsed
198
- using the gem. See the wiki[http://github.com/adzap/validates_timeliness/wiki/Plugin-Parser] for more details about the parser configuration.
199
-
200
-
201
- === Restriction Shorthand
202
-
203
- It is common to restrict an attribute to being on or before the current time or current day.
204
- To specify this you need to use a lambda as an option value e.g. <tt>lambda { Time.current }</tt>.
205
- This can be tedious noise amongst your validations for something so common. To combat this the
206
- plugin allows you to use shorthand symbols for often used relative times or dates.
207
-
208
- Just provide the symbol as the option value like so:
209
-
210
- validates_date :birth_date, :on_or_before => :today
211
-
212
- The :today symbol is evaluated as <tt>lambda { Date.today }</tt>. The :now and :today
213
- symbols are pre-configured. Configure your own like so:
214
-
215
- # in the setup block
216
- config.restriction_shorthand_symbols.update(
217
- :yesterday => lambda { 1.day.ago }
218
- )
219
-
220
-
221
- === Default Timezone
222
-
223
- The plugin needs to know the default timezone you are using when parsing or type casting values. If you are using
224
- ActiveRecord then the default is automatically set to the same default zone as ActiveRecord. If you are using
225
- another ORM you may need to change this setting.
226
-
227
- # in the setup block
228
- config.default_timezone = :utc
229
-
230
- By default it will be UTC if ActiveRecord is not loaded.
231
-
232
-
233
- === Dummy Date For Time Types
234
-
235
- Given that Ruby has no support for a time-only type, all time type columns are evaluated
236
- as a regular Time class objects with a dummy date value set. Rails defines the dummy date as
237
- 2000-01-01. So a time of '12:30' is evaluated as a Time value of '2000-01-01 12:30'. If you
238
- need to customize this for some reason you can do so as follows
239
-
240
- # in the setup block
241
- config.dummy_date_for_time_type = [2009, 1, 1]
242
-
243
- The value should be an array of 3 values being year, month and day in that order.
244
-
245
-
246
- === Temporal Restriction Errors
247
-
248
- When using the validation temporal restrictions there are times when the restriction
249
- option value itself may be invalid. This will add an error to the model such as
250
- 'Error occurred validating birth_date for :before restriction'. These can be annoying
251
- in development or production as you most likely just want to skip the option if no
252
- valid value was returned. By default these errors are displayed in Rails test mode.
253
-
254
- To turn them on/off:
255
-
256
- # in the setup block
257
- config.ignore_restriction_errors = true
258
-
259
-
260
- == Extensions
261
-
262
- === Strict Parsing for Select Helpers
263
-
264
- When using date/time select helpers, the component values are handled by ActiveRecord using
265
- the Time class to instantiate them into a time value. This means that some invalid dates,
266
- such as 31st June, are shifted forward and treated as valid. To handle these cases in a strict
267
- way, you can enable the plugin extension to treat them as invalid dates.
268
-
269
- To activate it, uncomment this line in the initializer:
270
-
271
- # in the setup block
272
- config.enable_multiparameter_extension!
273
-
274
-
275
- === Display Invalid Values in Select Helpers
276
-
277
- The plugin offers an extension for ActionView to allowing invalid date and time values to be
278
- redisplayed to the user as feedback, instead of a blank field which happens by default in
279
- Rails. Though the date helpers make this a pretty rare occurrence, given the select dropdowns
280
- for each date/time component, but it may be something of interest.
281
-
282
- To activate it, uncomment this line in the initializer:
283
-
284
- # in the setup block
285
- config.enable_date_time_select_extension!
286
-
287
-
288
- == Contributors
289
-
290
- To see the generous people who have contributed code, take a look at the {contributors list}[http://github.com/adzap/validates_timeliness/contributors].
291
-
292
-
293
- == Maintainers
294
-
295
- * {Adam Meehan}[http://github.com/adzap]
296
-
297
-
298
- == License
299
-
300
- Copyright (c) 2008 Adam Meehan, released under the MIT license
@@ -1,14 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "rails", "~> 4.2.8"
6
- gem "rspec", "~> 3.6.0"
7
- gem "rspec-rails", "~> 3.6.0"
8
- gem "timecop"
9
- gem "byebug"
10
- gem "appraisal"
11
- gem "sqlite3", "~> 1.3.0"
12
- gem "nokogiri", "1.6.7"
13
-
14
- gemspec path: "../"