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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 942eb4506928d381dae7a7356206a045a57d8085cc9ef8ea6e96801186274bea
4
- data.tar.gz: 6991d619ce2c4872c16e1c757f6de78c8f1761c6b5194f10b0312158431aba15
3
+ metadata.gz: dc002013bf4a78cba246d4a99133e7cf50a4728125f6472621f2b8a563ea0857
4
+ data.tar.gz: fd59680e5636166eeff6848fb3a2560878c583a5fc60dbea9aba9d52fa53e0de
5
5
  SHA512:
6
- metadata.gz: abf74b01270b55f495fd10ca1e5abcdfe899742499dcb8c7af17b11f68aa04143b843d3b65c9cbafbd6eae1f56a4a9d4959b9bd8af6ec42a1c7bcc65055689d1
7
- data.tar.gz: afb9c7e4657292e244749548a8669222da167410d7a28831c665f6a86fa88e0415e3daecbd4580f8c2eec24e754c59cfe7ba2a232256c7a769b0ff2805aea6a5
6
+ metadata.gz: b80b655d4df7e324fc42bdd2b3006e7c5cf8a0fb4fd6d0d27155a94a4ce4e9b93e7ffa1e9eb0f79fb7635a6a0bc75d34112d25a425cd553b0be2a641fb93b626
7
+ data.tar.gz: 1ad906a88d910f0dae8ef4f0d09431776b43c9db85ab587b8f35ea18fd8cdb75a6dcbea666b745b7cc17dbc998f6e3274429ecc1e8fb5c47c5beabfb4ac42d23
@@ -0,0 +1,7 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: weekly
7
+ open-pull-requests-limit: 10
@@ -0,0 +1,41 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ tests:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ fail-fast: false
8
+ matrix:
9
+ include:
10
+ - gemfile: rails_6_0
11
+ ruby: 2.6
12
+ - gemfile: rails_6_0
13
+ ruby: 2.7
14
+ - gemfile: rails_6_0
15
+ ruby: 3.0
16
+ - gemfile: rails_6_1
17
+ ruby: 2.6
18
+ - gemfile: rails_6_1
19
+ ruby: 2.7
20
+ - gemfile: rails_6_1
21
+ ruby: 3.0
22
+
23
+ name: ${{ matrix.gemfile }}, ruby ${{ matrix.ruby }}
24
+
25
+ steps:
26
+ - uses: actions/checkout@v2
27
+
28
+ - name: Set up Ruby
29
+ uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby }}
32
+ bundler-cache: false
33
+
34
+ - name: Bundle install
35
+ run: |
36
+ bundle config set gemfile "${GITHUB_WORKSPACE}/gemfiles/${{ matrix.gemfile }}.gemfile"
37
+ bundle install --jobs 4 --retry 3
38
+
39
+ - name: Run specs
40
+ run: |
41
+ bundle exec rspec
data/CHANGELOG.rdoc CHANGED
@@ -1,9 +1,17 @@
1
- = 4.1.1 [2019-08-06]
2
- * Add initializer to ensure Timeliness default ambigiuous date handling config
3
- in Timeliness v0.4.1+ is set correctly when using `use_us_formats` or
4
- `use_euro_formats` switcher to set default.
5
- * Removed build support for Ruby 2.3 and Rails 4.0 and 4.1 to EOL official
6
- support for those.
1
+ = 5.0.0 [2021-04-03]
2
+ * Fix DateTimeSelect extension support (AquisTech)
3
+ * Relaxed Timeliness dependency version which allows for >= 0.4.0 with
4
+ threadsafety fix for use_us_formats and use_euro_formats for hot switching
5
+ in a request.
6
+ * Add initializer to ensure Timeliness v0.4+ ambiguous date config is set
7
+ correctly when using `use_euro_formats` or `remove_use_formats'.
8
+ * Add Ruby 3 compatibility
9
+ * Add Rails 6.1 compatibility
10
+
11
+ Breaking Changes
12
+ * Update Multiparameter extension to use ActiveRecord type classes with multiparameter handling
13
+ which stores a hash of multiparamter values as the value before type cast, no longer a mushed datetime string
14
+ * Removed all custom plugin attribute methods and method overrides in favour using ActiveModel type system
7
15
 
8
16
  = 4.1.0 [2019-06-11]
9
17
  * Relaxed Timeliness dependency version to >= 0.3.10 and < 1, which allows
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008-2010 Adam Meehan
1
+ Copyright (c) 2008-2021 Adam Meehan
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md ADDED
@@ -0,0 +1,321 @@
1
+ # ValidatesTimeliness [![build](https://github.com/adzap/validates_timeliness/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/adzap/validates_timeliness/actions/workflows/ci.yml)
2
+
3
+ * Source: https://github.com/adzap/validates_timeliness
4
+ * Issues: https://github.com/adzap/validates_timeliness/issues
5
+
6
+
7
+ ## Description
8
+
9
+ Complete validation of dates, times and datetimes for Rails 6.x and
10
+ ActiveModel.
11
+
12
+ Old Rails versions:
13
+
14
+ * Rails 4.x: [https://github.com/adzap/validates_timeliness/tree/4-0-stable]
15
+
16
+ * Rails 5.x: [https://github.com/adzap/validates_timeliness/tree/5-0-stable]
17
+
18
+
19
+ ## Features
20
+
21
+ * Adds validation for dates, times and datetimes to ActiveModel
22
+
23
+ * Handles timezones and type casting of values for you
24
+
25
+ * Only Rails date/time validation plugin offering complete validation (See
26
+ ORM/ODM support)
27
+
28
+ * Uses extensible date/time parser (Using [timeliness
29
+ gem](https://github.com/adzap/timeliness). See Plugin Parser)
30
+
31
+ * Adds extensions to fix Rails date/time select issues (See Extensions)
32
+
33
+ * Supports I18n for the error messages. For multi-language support try
34
+ [timeliness-i18n gem](https://github.com/pedrofurtado/timeliness-i18n).
35
+
36
+ * Supports all the Rubies (that any sane person would be using in
37
+ production).
38
+
39
+
40
+ ## Installation
41
+
42
+ # in Gemfile
43
+ gem 'validates_timeliness', '~> 6.0.0.alpha1'
44
+
45
+ # Run bundler
46
+ $ bundle install
47
+
48
+ Then run
49
+
50
+ $ rails generate validates_timeliness:install
51
+
52
+ This creates configuration initializer and locale files. In the initializer,
53
+ there are a number of config options to customize the plugin.
54
+
55
+ NOTE: You may wish to enable the plugin parser and the extensions to start.
56
+ Please read those sections first.
57
+
58
+ ## Examples
59
+
60
+ validates_datetime :occurred_at
61
+
62
+ validates_date :date_of_birth, before: lambda { 18.years.ago },
63
+ before_message: "must be at least 18 years old"
64
+
65
+ validates_datetime :finish_time, after: :start_time # Method symbol
66
+
67
+ validates_date :booked_at, on: :create, on_or_after: :today # See Restriction Shorthand.
68
+
69
+ validates_time :booked_at, between: ['9:00am', '5:00pm'] # On or after 9:00AM and on or before 5:00PM
70
+ validates_time :booked_at, between: '9:00am'..'5:00pm' # The same as previous example
71
+ validates_time :booked_at, between: '9:00am'...'5:00pm' # On or after 9:00AM and strictly before 5:00PM
72
+
73
+ validates_time :breakfast_time, on_or_after: '6:00am',
74
+ on_or_after_message: 'must be after opening time',
75
+ before: :lunchtime,
76
+ before_message: 'must be before lunch time'
77
+
78
+ ## Usage
79
+
80
+ To validate a model with a date, time or datetime attribute you just use the
81
+ validation method
82
+
83
+ class Person < ActiveRecord::Base
84
+ validates_date :date_of_birth, on_or_before: lambda { Date.current }
85
+ # or
86
+ validates :date_of_birth, timeliness: {on_or_before: lambda { Date.current }, type: :date}
87
+ end
88
+
89
+ or even on a specific record, per ActiveModel API.
90
+
91
+ @person.validates_date :date_of_birth, on_or_before: lambda { Date.current }
92
+
93
+ The list of validation methods available are as follows:
94
+ validates_date - validate value as date
95
+ validates_time - validate value as time only i.e. '12:20pm'
96
+ validates_datetime - validate value as a full date and time
97
+ validates - use the :timeliness key and set the type in the hash.
98
+
99
+ The validation methods take the usual options plus some specific ones to
100
+ restrict the valid range of dates or times allowed
101
+
102
+ Temporal options (or restrictions):
103
+ :is_at - Attribute must be equal to value to be valid
104
+ :before - Attribute must be before this value to be valid
105
+ :on_or_before - Attribute must be equal to or before this value to be valid
106
+ :after - Attribute must be after this value to be valid
107
+ :on_or_after - Attribute must be equal to or after this value to be valid
108
+ :between - Attribute must be between the values to be valid. Range or Array of 2 values.
109
+
110
+ Regular validation options:
111
+ :allow_nil - Allow a nil value to be valid
112
+ :allow_blank - Allows a nil or empty string value to be valid
113
+ :if - Execute validation when :if evaluates true
114
+ :unless - Execute validation when :unless evaluates false
115
+ :on - Specify validation context e.g :save, :create or :update. Default is :save.
116
+
117
+ Special options:
118
+ :ignore_usec - Ignores microsecond value on datetime restrictions
119
+ :format - Limit validation to a single format for special cases. Requires plugin parser.
120
+
121
+ The temporal restrictions can take 4 different value types:
122
+
123
+ * Date, Time, or DateTime object value
124
+ * Proc or lambda object which may take an optional parameter, being the
125
+ record object
126
+ * A symbol matching a method name in the model
127
+ * String value
128
+
129
+
130
+ When an attribute value is compared to temporal restrictions, they are
131
+ compared as the same type as the validation method type. So using
132
+ validates_date means all values are compared as dates.
133
+
134
+ ## Configuration
135
+
136
+ ### ORM/ODM Support
137
+
138
+ The plugin adds date/time validation to ActiveModel for any ORM/ODM that
139
+ supports the ActiveModel validations component. However, there is an issue
140
+ with most ORM/ODMs which does not allow 100% date/time validation by default.
141
+ Specifically, when you assign an invalid date/time value to an attribute, most
142
+ ORM/ODMs will only store a nil value for the attribute. This causes an issue
143
+ for date/time validation, since we need to know that a value was assigned but
144
+ was invalid. To fix this, we need to cache the original invalid value to know
145
+ that the attribute is not just nil.
146
+
147
+ Each ORM/ODM requires a specific shim to fix it. The plugin includes a shim
148
+ for ActiveRecord and Mongoid. You can activate them like so
149
+
150
+ ValidatesTimeliness.setup do |config|
151
+
152
+ # Extend ORM/ODMs for full support (:active_record).
153
+ config.extend_orms = [ :active_record ]
154
+
155
+ end
156
+
157
+ By default the plugin extends ActiveRecord if loaded. If you wish to extend
158
+ another ORM then look at the [wiki
159
+ page](https://github.com/adzap/validates_timeliness/wiki/ORM-Support) for more
160
+ information.
161
+
162
+ It is not required that you use a shim, but you will not catch errors when the
163
+ attribute value is invalid and evaluated to nil.
164
+
165
+ ### Error Messages
166
+
167
+ Using the I18n system to define new defaults:
168
+
169
+ en:
170
+ errors:
171
+ messages:
172
+ invalid_date: "is not a valid date"
173
+ invalid_time: "is not a valid time"
174
+ invalid_datetime: "is not a valid datetime"
175
+ is_at: "must be at %{restriction}"
176
+ before: "must be before %{restriction}"
177
+ on_or_before: "must be on or before %{restriction}"
178
+ after: "must be after %{restriction}"
179
+ on_or_after: "must be on or after %{restriction}"
180
+
181
+ The %{restriction} signifies where the interpolation value for the restriction
182
+ will be inserted.
183
+
184
+ You can also use validation options for custom error messages. The following
185
+ option keys are available:
186
+
187
+ :invalid_date_message
188
+ :invalid_time_message
189
+ :invalid_datetime_message
190
+ :is_at_message
191
+ :before_message
192
+ :on_or_before_message
193
+ :after_message
194
+ :on_or_after_message
195
+
196
+ Note: There is no :between_message option. The between error message should be
197
+ defined using the :on_or_after and :on_or_before (:before in case when
198
+ :between argument is a Range with excluded high value, see Examples) messages.
199
+
200
+ It is highly recommended you use the I18n system for error messages.
201
+
202
+ ### Plugin Parser
203
+
204
+ The plugin uses the [timeliness gem](https://github.com/adzap/timeliness) as a
205
+ fast, configurable and extensible date and time parser. You can add or remove
206
+ valid formats for dates, times, and datetimes. It is also more strict than the
207
+ Ruby parser, which means it won't accept day of the month if it's not a valid
208
+ number for the month.
209
+
210
+ By default the parser is disabled. To enable it:
211
+
212
+ # in the setup block
213
+ config.use_plugin_parser = true
214
+
215
+ Enabling the parser will mean that strings assigned to attributes validated
216
+ with the plugin will be parsed using the gem. See the
217
+ [wiki](https://github.com/adzap/validates_timeliness/wiki/Plugin-Parser) for
218
+ more details about the parser configuration.
219
+
220
+ ### Restriction Shorthand
221
+
222
+ It is common to restrict an attribute to being on or before the current time
223
+ or current day. To specify this you need to use a lambda as an option value
224
+ e.g. `lambda { Time.current }`. This can be tedious noise amongst your
225
+ validations for something so common. To combat this the plugin allows you to
226
+ use shorthand symbols for often used relative times or dates.
227
+
228
+ Just provide the symbol as the option value like so:
229
+
230
+ validates_date :birth_date, on_or_before: :today
231
+
232
+ The :today symbol is evaluated as `lambda { Date.current }`. The :now and
233
+ :today symbols are pre-configured. Configure your own like so:
234
+
235
+ # in the setup block
236
+ config.restriction_shorthand_symbols.update(
237
+ yesterday: lambda { 1.day.ago }
238
+ )
239
+
240
+ ### Default Timezone
241
+
242
+ The plugin needs to know the default timezone you are using when parsing or
243
+ type casting values. If you are using ActiveRecord then the default is
244
+ automatically set to the same default zone as ActiveRecord. If you are using
245
+ another ORM you may need to change this setting.
246
+
247
+ # in the setup block
248
+ config.default_timezone = :utc
249
+
250
+ By default it will be UTC if ActiveRecord is not loaded.
251
+
252
+ ### Dummy Date For Time Types
253
+
254
+ Given that Ruby has no support for a time-only type, all time type columns are
255
+ evaluated as a regular Time class objects with a dummy date value set. Rails
256
+ defines the dummy date as 2000-01-01. So a time of '12:30' is evaluated as a
257
+ Time value of '2000-01-01 12:30'. If you need to customize this for some
258
+ reason you can do so as follows
259
+
260
+ # in the setup block
261
+ config.dummy_date_for_time_type = [2009, 1, 1]
262
+
263
+ The value should be an array of 3 values being year, month and day in that
264
+ order.
265
+
266
+ ### Temporal Restriction Errors
267
+
268
+ When using the validation temporal restrictions there are times when the
269
+ restriction option value itself may be invalid. This will add an error to the
270
+ model such as 'Error occurred validating birth_date for :before restriction'.
271
+ These can be annoying in development or production as you most likely just
272
+ want to skip the option if no valid value was returned. By default these
273
+ errors are displayed in Rails test mode.
274
+
275
+ To turn them on/off:
276
+
277
+ # in the setup block
278
+ config.ignore_restriction_errors = true
279
+
280
+ ## Extensions
281
+
282
+ ### Strict Parsing for Select Helpers
283
+
284
+ When using date/time select helpers, the component values are handled by
285
+ ActiveRecord using the Time class to instantiate them into a time value. This
286
+ means that some invalid dates, such as 31st June, are shifted forward and
287
+ treated as valid. To handle these cases in a strict way, you can enable the
288
+ plugin extension to treat them as invalid dates.
289
+
290
+ To activate it, uncomment this line in the initializer:
291
+
292
+ # in the setup block
293
+ config.enable_multiparameter_extension!
294
+
295
+ ### Display Invalid Values in Select Helpers
296
+
297
+ The plugin offers an extension for ActionView to allowing invalid date and
298
+ time values to be redisplayed to the user as feedback, instead of a blank
299
+ field which happens by default in Rails. Though the date helpers make this a
300
+ pretty rare occurrence, given the select dropdowns for each date/time
301
+ component, but it may be something of interest.
302
+
303
+ To activate it, uncomment this line in the initializer:
304
+
305
+ # in the setup block
306
+ config.enable_date_time_select_extension!
307
+
308
+ ## Contributors
309
+
310
+ To see the generous people who have contributed code, take a look at the
311
+ [contributors
312
+ list](https://github.com/adzap/validates_timeliness/contributors).
313
+
314
+ ## Maintainers
315
+
316
+ * [Adam Meehan](https://github.com/adzap)
317
+
318
+
319
+ ## License
320
+
321
+ Copyright (c) 2008 Adam Meehan, released under the MIT license
data/Rakefile CHANGED
@@ -5,7 +5,6 @@ require 'appraisal'
5
5
 
6
6
  Bundler::GemHelper.install_tasks
7
7
 
8
- require 'rdoc/task'
9
8
  require 'rspec/core/rake_task'
10
9
 
11
10
  desc "Run specs"
@@ -17,14 +16,5 @@ RSpec::Core::RakeTask.new(:coverage) do |t|
17
16
  t.rcov_opts = ['--exclude', 'spec']
18
17
  end
19
18
 
20
- desc 'Generate documentation for plugin.'
21
- Rake::RDocTask.new(:rdoc) do |rdoc|
22
- rdoc.rdoc_dir = 'rdoc'
23
- rdoc.title = 'ValidatesTimeliness'
24
- rdoc.options << '--line-numbers' << '--inline-source'
25
- rdoc.rdoc_files.include('README')
26
- rdoc.rdoc_files.include('lib/**/*.rb')
27
- end
28
-
29
19
  desc 'Default: run specs.'
30
20
  task :default => :spec
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 6.0.0"
6
+ gem "rspec"
7
+ gem "rspec-rails", "~> 3.7"
8
+ gem "sqlite3"
9
+ gem "timecop"
10
+ gem "byebug"
11
+ gem "appraisal"
12
+ gem "nokogiri", "~> 1.8"
13
+
14
+ gemspec path: "../"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "~> 6.1.0"
6
+ gem "rspec"
7
+ gem "rspec-rails", "~> 3.7"
8
+ gem "sqlite3"
9
+ gem "timecop"
10
+ gem "byebug"
11
+ gem "appraisal"
12
+ gem "nokogiri", "~> 1.8"
13
+
14
+ gemspec path: "../"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
6
+ gem "rspec"
7
+ gem "rspec-rails", "~> 3.7"
8
+ gem "sqlite3"
9
+ gem "timecop"
10
+ gem "byebug"
11
+ gem "appraisal"
12
+ gem "nokogiri", "~> 1.8"
13
+
14
+ gemspec path: "../"
@@ -6,83 +6,44 @@ module ValidatesTimeliness
6
6
  class_attribute :timeliness_validated_attributes
7
7
  self.timeliness_validated_attributes = []
8
8
  end
9
+ end
10
+ end
9
11
 
10
- module ClassMethods
11
-
12
- public
13
- # Override in ORM shim
14
- def timeliness_attribute_timezone_aware?(attr_name)
15
- false
16
- end
17
-
18
- # Override in ORM shim
19
- def timeliness_attribute_type(attr_name)
20
- :datetime
21
- end
22
-
23
- def define_timeliness_methods(before_type_cast=false)
24
- return if timeliness_validated_attributes.blank?
25
- timeliness_validated_attributes.each do |attr_name|
26
- define_attribute_timeliness_methods(attr_name, before_type_cast)
27
- end
28
- end
29
-
30
- def generated_timeliness_methods
31
- @generated_timeliness_methods ||= Module.new { |m|
32
- extend Mutex_m
33
- }.tap { |mod| include mod }
34
- end
35
-
36
- def undefine_timeliness_attribute_methods
37
- generated_timeliness_methods.module_eval do
38
- instance_methods.each { |m| undef_method(m) }
39
- end
40
- end
41
-
42
- protected
43
-
44
- def define_attribute_timeliness_methods(attr_name, before_type_cast=false)
45
- define_timeliness_write_method(attr_name)
46
- define_timeliness_before_type_cast_method(attr_name) if before_type_cast
47
- end
48
-
49
- def define_timeliness_write_method(attr_name)
50
- generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
51
- def #{attr_name}=(value)
52
- write_timeliness_attribute('#{attr_name}', value)
53
- end
54
- STR
55
- end
56
-
57
- def define_timeliness_before_type_cast_method(attr_name)
58
- generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
59
- def #{attr_name}_before_type_cast
60
- read_timeliness_attribute_before_type_cast('#{attr_name}')
61
- end
62
- STR
63
- end
12
+ ActiveModel::Type::Date.prepend Module.new {
13
+ def cast_value(value)
14
+ return super unless ValidatesTimeliness.use_plugin_parser
15
+
16
+ if value.is_a?(::String)
17
+ return if value.empty?
18
+ value = Timeliness::Parser.parse(value, :date)
19
+ value.to_date if value
20
+ elsif value.respond_to?(:to_date)
21
+ value.to_date
22
+ else
23
+ value
64
24
  end
25
+ end
26
+ }
65
27
 
66
- def write_timeliness_attribute(attr_name, value)
67
- @timeliness_cache ||= {}
68
- @timeliness_cache[attr_name] = value
69
-
70
- if ValidatesTimeliness.use_plugin_parser
71
- type = self.class.timeliness_attribute_type(attr_name)
72
- timezone = :current if self.class.timeliness_attribute_timezone_aware?(attr_name)
73
- value = Timeliness::Parser.parse(value, type, :zone => timezone)
74
- value = value.to_date if value && type == :date
75
- end
28
+ ActiveModel::Type::Time.prepend Module.new {
29
+ def user_input_in_time_zone(value)
30
+ return super unless ValidatesTimeliness.use_plugin_parser
76
31
 
77
- @attributes[attr_name] = value
32
+ if value.is_a?(String)
33
+ dummy_time_value = value.sub(/\A(\d\d\d\d-\d\d-\d\d |)/, Date.current.to_s + ' ')
34
+ Timeliness::Parser.parse(dummy_time_value, :datetime, zone: :current)
35
+ else
36
+ value.in_time_zone
78
37
  end
79
-
80
- def read_timeliness_attribute_before_type_cast(attr_name)
81
- @timeliness_cache && @timeliness_cache[attr_name] || @attributes[attr_name]
82
- end
83
-
84
- def _clear_timeliness_cache
85
- @timeliness_cache = {}
38
+ end
39
+ }
40
+
41
+ ActiveModel::Type::DateTime.prepend Module.new {
42
+ def user_input_in_time_zone(value)
43
+ if value.is_a?(String) && ValidatesTimeliness.use_plugin_parser
44
+ Timeliness::Parser.parse(value, :datetime, zone: :current)
45
+ else
46
+ value.in_time_zone
86
47
  end
87
48
  end
88
- end
49
+ }