validates_timeliness 3.0.8 → 4.0.0
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.
- checksums.yaml +7 -0
- data/.travis.yml +21 -0
- data/CHANGELOG.rdoc +21 -1
- data/README.rdoc +20 -22
- data/Rakefile +5 -1
- data/gemfiles/rails_4_0.gemfile +19 -0
- data/gemfiles/rails_4_1.gemfile +19 -0
- data/gemfiles/rails_4_2.gemfile +19 -0
- data/lib/generators/validates_timeliness/install_generator.rb +0 -1
- data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +3 -3
- data/lib/validates_timeliness/attribute_methods.rb +42 -26
- data/lib/validates_timeliness/extensions/date_time_select.rb +2 -9
- data/lib/validates_timeliness/extensions/multiparameter_handler.rb +59 -54
- data/lib/validates_timeliness/extensions.rb +3 -4
- data/lib/validates_timeliness/helper_methods.rb +7 -6
- data/lib/validates_timeliness/orm/active_record.rb +70 -15
- data/lib/validates_timeliness/railtie.rb +2 -1
- data/lib/validates_timeliness/validator.rb +29 -4
- data/lib/validates_timeliness/version.rb +1 -1
- data/lib/validates_timeliness.rb +13 -13
- data/spec/spec_helper.rb +10 -5
- data/spec/support/config_helper.rb +10 -0
- data/spec/support/model_helpers.rb +4 -4
- data/spec/support/tag_matcher.rb +35 -0
- data/spec/support/test_model.rb +0 -1
- data/spec/validates_timeliness/attribute_methods_spec.rb +22 -7
- data/spec/validates_timeliness/conversion_spec.rb +41 -41
- data/spec/validates_timeliness/extensions/date_time_select_spec.rb +5 -4
- data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +28 -16
- data/spec/validates_timeliness/helper_methods_spec.rb +12 -12
- data/spec/validates_timeliness/orm/active_record_spec.rb +178 -44
- data/spec/validates_timeliness/validator/after_spec.rb +3 -3
- data/spec/validates_timeliness/validator/before_spec.rb +3 -3
- data/spec/validates_timeliness/validator/is_at_spec.rb +4 -4
- data/spec/validates_timeliness/validator/on_or_after_spec.rb +3 -3
- data/spec/validates_timeliness/validator/on_or_before_spec.rb +3 -3
- data/spec/validates_timeliness/validator_spec.rb +66 -25
- data/spec/validates_timeliness_spec.rb +8 -8
- data/validates_timeliness.gemspec +2 -2
- metadata +41 -60
- data/lib/validates_timeliness/orm/mongoid.rb +0 -54
- data/spec/validates_timeliness/orm/mongoid_spec.rb +0 -113
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c730e7683c67e1fce0a8a2e0b6dcba331b812e36
|
|
4
|
+
data.tar.gz: 904f0cfe44ca9d764e051d1d656cf34cbb6a5dba
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fd5326ac226351463fab7d317fadfb96bab6858b0231cfa1ee2b3fb49b974e6aeacf9cff498a22088132cd675d823054db6f0b622ea6db034d0884f0cdfab1e1
|
|
7
|
+
data.tar.gz: fe62be44d7d398c17d5762f91335e11187afdd8e2cb3f15e69de95feb8b764b03773f038c009ca72e299a793378ee680e8ea7ebcb62aaec341ef17b02fa1f747
|
data/.travis.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
language: ruby
|
|
2
|
+
cache: bundler
|
|
3
|
+
|
|
4
|
+
gemfile:
|
|
5
|
+
- gemfiles/rails_4_0.gemfile
|
|
6
|
+
- gemfiles/rails_4_1.gemfile
|
|
7
|
+
- gemfiles/rails_4_2.gemfile
|
|
8
|
+
|
|
9
|
+
rvm:
|
|
10
|
+
- "2.0.0"
|
|
11
|
+
- "2.2.0"
|
|
12
|
+
- "2.3.0"
|
|
13
|
+
|
|
14
|
+
script: 'bundle exec rake'
|
|
15
|
+
|
|
16
|
+
notifications:
|
|
17
|
+
email:
|
|
18
|
+
recipients:
|
|
19
|
+
- adam.meehan@gmail.com
|
|
20
|
+
on_failure: change
|
|
21
|
+
on_success: never
|
data/CHANGELOG.rdoc
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
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
|
+
|
|
1
21
|
= 3.0.8 [2011-12-24]
|
|
2
|
-
* Remove deprecated InstanceMethods module when using AS::Concern
|
|
22
|
+
* Remove deprecated InstanceMethods module when using AS::Concern (carlosantoniodasilva)
|
|
3
23
|
* Update Mongoid shim for v2.3 compatability.
|
|
4
24
|
|
|
5
25
|
= 3.0.7 [2011-09-21]
|
data/README.rdoc
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
= ValidatesTimeliness
|
|
2
2
|
|
|
3
3
|
* Source: http://github.com/adzap/validates_timeliness
|
|
4
|
-
*
|
|
4
|
+
* Issues: http://github.com/adzap/validates_timeliness/issues
|
|
5
5
|
|
|
6
6
|
== Description
|
|
7
7
|
|
|
8
|
-
Complete validation of dates, times and datetimes for Rails
|
|
8
|
+
Complete validation of dates, times and datetimes for Rails 4.x and ActiveModel.
|
|
9
9
|
|
|
10
|
-
If you a looking for the old version for Rails
|
|
10
|
+
If you a looking for the old version for Rails 3.x go here[http://github.com/adzap/validates_timeliness/tree/v3.x].
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
== Features
|
|
@@ -18,24 +18,19 @@ If you a looking for the old version for Rails 2.x go here[http://github.com/adz
|
|
|
18
18
|
|
|
19
19
|
* Only Rails date/time validation plugin offering complete validation (See ORM/ODM support)
|
|
20
20
|
|
|
21
|
-
* Adds extensions to fix Rails date/time select issues (See Extensions)
|
|
22
|
-
|
|
23
21
|
* Uses extensible date/time parser (Using {timeliness gem}[http://github.com/adzap/timeliness]. See Plugin Parser)
|
|
24
22
|
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
* Supports Ruby 1.8.x, 1.9.x and Rubinius.
|
|
23
|
+
* Adds extensions to fix Rails date/time select issues (See Extensions)
|
|
28
24
|
|
|
29
|
-
|
|
25
|
+
* Supports I18n for the error messages
|
|
30
26
|
|
|
31
|
-
|
|
27
|
+
* Supports all the Rubies (that any sane person would be using in production).
|
|
32
28
|
|
|
33
|
-
rails plugin install git://github.com/adzap/validates_timeliness.git
|
|
34
29
|
|
|
35
|
-
|
|
30
|
+
== Installation
|
|
36
31
|
|
|
37
32
|
# in Gemfile
|
|
38
|
-
gem 'validates_timeliness', '~>
|
|
33
|
+
gem 'validates_timeliness', '~> 4.0'
|
|
39
34
|
|
|
40
35
|
# Run bundler
|
|
41
36
|
$ bundle install
|
|
@@ -44,7 +39,7 @@ Then run
|
|
|
44
39
|
|
|
45
40
|
$ rails generate validates_timeliness:install
|
|
46
41
|
|
|
47
|
-
This creates configuration initializer and locale files. In the initializer,
|
|
42
|
+
This creates configuration initializer and locale files. In the initializer, there are a number of config
|
|
48
43
|
options to customize the plugin.
|
|
49
44
|
|
|
50
45
|
NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
|
|
@@ -55,12 +50,15 @@ NOTE: You may wish to enable the plugin parser and the extensions to start. Plea
|
|
|
55
50
|
validates_datetime :occurred_at
|
|
56
51
|
|
|
57
52
|
validates_date :date_of_birth, :before => lambda { 18.years.ago },
|
|
58
|
-
|
|
53
|
+
:before_message => "must be at least 18 years old"
|
|
59
54
|
|
|
60
55
|
validates_datetime :finish_time, :after => :start_time # Method symbol
|
|
61
56
|
|
|
62
57
|
validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
|
|
63
|
-
|
|
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
|
|
64
62
|
|
|
65
63
|
validates_time :breakfast_time, :on_or_after => '6:00am',
|
|
66
64
|
:on_or_after_message => 'must be after opening time',
|
|
@@ -79,7 +77,7 @@ validation method
|
|
|
79
77
|
validates :date_of_birth, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}
|
|
80
78
|
end
|
|
81
79
|
|
|
82
|
-
|
|
80
|
+
or even on a specific record, per ActiveModel API.
|
|
83
81
|
|
|
84
82
|
@person.validates_date :date_of_birth, :on_or_before => lambda { Date.current }
|
|
85
83
|
|
|
@@ -139,8 +137,8 @@ like so
|
|
|
139
137
|
|
|
140
138
|
ValidatesTimeliness.setup do |config|
|
|
141
139
|
|
|
142
|
-
# Extend ORM/ODMs for full support (:active_record
|
|
143
|
-
config.extend_orms = [ :
|
|
140
|
+
# Extend ORM/ODMs for full support (:active_record).
|
|
141
|
+
config.extend_orms = [ :active_record ]
|
|
144
142
|
|
|
145
143
|
end
|
|
146
144
|
|
|
@@ -178,8 +176,8 @@ You can also use validation options for custom error messages. The following opt
|
|
|
178
176
|
:after_message
|
|
179
177
|
:on_or_after_message
|
|
180
178
|
|
|
181
|
-
Note: There is no :between_message option. The between error message should be defined using the
|
|
182
|
-
:
|
|
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.
|
|
183
181
|
|
|
184
182
|
It is highly recommended you use the I18n system for error messages.
|
|
185
183
|
|
|
@@ -298,4 +296,4 @@ To see the generous people who have contributed code, take a look at the {contri
|
|
|
298
296
|
|
|
299
297
|
== License
|
|
300
298
|
|
|
301
|
-
Copyright (c) 2008
|
|
299
|
+
Copyright (c) 2008 Adam Meehan, released under the MIT license
|
data/Rakefile
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "http://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "rails", "~> 4.0.0"
|
|
6
|
+
gem "rspec", "~> 3.0.0"
|
|
7
|
+
gem "rspec-rails", "~> 3.0.0"
|
|
8
|
+
gem "timecop"
|
|
9
|
+
gem "rspec_tag_matchers"
|
|
10
|
+
gem "byebug"
|
|
11
|
+
gem "appraisal"
|
|
12
|
+
gem "sqlite3"
|
|
13
|
+
gem "nokogiri", "1.6.7"
|
|
14
|
+
|
|
15
|
+
group :active_record do
|
|
16
|
+
gem "sqlite3-ruby", :require => "sqlite3"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
gemspec :path => "../"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "http://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "rails", "~> 4.1.0"
|
|
6
|
+
gem "rspec", "~> 3.0.0"
|
|
7
|
+
gem "rspec-rails", "~> 3.0.0"
|
|
8
|
+
gem "timecop"
|
|
9
|
+
gem "rspec_tag_matchers"
|
|
10
|
+
gem "byebug"
|
|
11
|
+
gem "appraisal"
|
|
12
|
+
gem "sqlite3"
|
|
13
|
+
gem "nokogiri", "1.6.7"
|
|
14
|
+
|
|
15
|
+
group :active_record do
|
|
16
|
+
gem "sqlite3-ruby", :require => "sqlite3"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
gemspec :path => "../"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "http://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "rails", "~> 4.2.0"
|
|
6
|
+
gem "rspec", "~> 3.0.0"
|
|
7
|
+
gem "rspec-rails", "~> 3.0.0"
|
|
8
|
+
gem "timecop"
|
|
9
|
+
gem "rspec_tag_matchers"
|
|
10
|
+
gem "byebug"
|
|
11
|
+
gem "appraisal"
|
|
12
|
+
gem "sqlite3"
|
|
13
|
+
gem "nokogiri", "1.6.7"
|
|
14
|
+
|
|
15
|
+
group :active_record do
|
|
16
|
+
gem "sqlite3-ruby", :require => "sqlite3"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
gemspec :path => "../"
|
|
@@ -3,7 +3,6 @@ module ValidatesTimeliness
|
|
|
3
3
|
class InstallGenerator < Rails::Generators::Base
|
|
4
4
|
desc "Copy ValidatesTimeliness default files"
|
|
5
5
|
source_root File.expand_path('../templates', __FILE__)
|
|
6
|
-
class_option :template_engine
|
|
7
6
|
|
|
8
7
|
def copy_initializers
|
|
9
8
|
copy_file 'validates_timeliness.rb', 'config/initializers/validates_timeliness.rb'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
ValidatesTimeliness.setup do |config|
|
|
2
|
-
# Extend ORM/ODMs for full support (:active_record
|
|
3
|
-
|
|
2
|
+
# Extend ORM/ODMs for full support (:active_record included).
|
|
3
|
+
config.extend_orms = [ :active_record ]
|
|
4
4
|
#
|
|
5
5
|
# Default timezone
|
|
6
6
|
# config.default_timezone = :utc
|
|
@@ -32,7 +32,7 @@ ValidatesTimeliness.setup do |config|
|
|
|
32
32
|
# Remove one or more formats making them invalid. e.g. remove_formats(:date, 'dd/mm/yyy')
|
|
33
33
|
# config.parser.remove_formats()
|
|
34
34
|
#
|
|
35
|
-
# Change the
|
|
35
|
+
# Change the ambiguous year threshold when parsing a 2 digit year
|
|
36
36
|
# config.parser.ambiguous_year_threshold = 30
|
|
37
37
|
#
|
|
38
38
|
# Treat ambiguous dates, such as 01/02/1950, as a Non-US date.
|
|
@@ -20,53 +20,69 @@ module ValidatesTimeliness
|
|
|
20
20
|
:datetime
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
protected
|
|
24
|
-
|
|
25
23
|
def define_timeliness_methods(before_type_cast=false)
|
|
26
24
|
return if timeliness_validated_attributes.blank?
|
|
27
25
|
timeliness_validated_attributes.each do |attr_name|
|
|
28
|
-
|
|
29
|
-
define_timeliness_before_type_cast_method(attr_name) if before_type_cast
|
|
26
|
+
define_attribute_timeliness_methods(attr_name, before_type_cast)
|
|
30
27
|
end
|
|
31
28
|
end
|
|
32
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_attribute_methods
|
|
37
|
+
super.tap { undefine_timeliness_attribute_methods }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def undefine_timeliness_attribute_methods
|
|
41
|
+
generated_timeliness_methods.synchronize do
|
|
42
|
+
generated_timeliness_methods.module_eval do
|
|
43
|
+
instance_methods.each { |m| undef_method(m) }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
protected
|
|
49
|
+
|
|
50
|
+
def define_attribute_timeliness_methods(attr_name, before_type_cast=false)
|
|
51
|
+
define_timeliness_write_method(attr_name)
|
|
52
|
+
define_timeliness_before_type_cast_method(attr_name) if before_type_cast
|
|
53
|
+
end
|
|
54
|
+
|
|
33
55
|
def define_timeliness_write_method(attr_name)
|
|
34
|
-
|
|
56
|
+
generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
|
|
35
57
|
def #{attr_name}=(value)
|
|
36
|
-
|
|
37
|
-
@timeliness_cache["#{attr_name}"] = value
|
|
38
|
-
|
|
39
|
-
#{ "if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend" if ValidatesTimeliness.use_plugin_parser }
|
|
40
|
-
super
|
|
58
|
+
write_timeliness_attribute('#{attr_name}', value)
|
|
41
59
|
end
|
|
42
|
-
|
|
43
|
-
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
|
60
|
+
STR
|
|
44
61
|
end
|
|
45
62
|
|
|
46
63
|
def define_timeliness_before_type_cast_method(attr_name)
|
|
47
|
-
|
|
64
|
+
generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
|
|
48
65
|
def #{attr_name}_before_type_cast
|
|
49
|
-
|
|
66
|
+
read_timeliness_attribute_before_type_cast('#{attr_name}')
|
|
50
67
|
end
|
|
51
|
-
|
|
52
|
-
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
|
68
|
+
STR
|
|
53
69
|
end
|
|
70
|
+
end
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
timezone = :current if timezone_aware
|
|
72
|
+
def write_timeliness_attribute(attr_name, value)
|
|
73
|
+
@timeliness_cache ||= {}
|
|
74
|
+
@timeliness_cache[attr_name] = value
|
|
59
75
|
|
|
60
|
-
|
|
76
|
+
if ValidatesTimeliness.use_plugin_parser
|
|
77
|
+
timezone = :current if self.class.timeliness_attribute_timezone_aware?(attr_name)
|
|
78
|
+
value = Timeliness::Parser.parse(value, self.class.timeliness_attribute_type(attr_name), :zone => timezone)
|
|
61
79
|
end
|
|
62
80
|
|
|
63
|
-
|
|
64
|
-
@generated_timeliness_methods ||= Module.new.tap { |m| include(m) }
|
|
65
|
-
end
|
|
81
|
+
@attributes[attr_name] = value
|
|
66
82
|
end
|
|
67
83
|
|
|
68
|
-
def
|
|
69
|
-
@timeliness_cache && @timeliness_cache[attr_name
|
|
84
|
+
def read_timeliness_attribute_before_type_cast(attr_name)
|
|
85
|
+
@timeliness_cache && @timeliness_cache[attr_name] || @attributes[attr_name]
|
|
70
86
|
end
|
|
71
87
|
|
|
72
88
|
def _clear_timeliness_cache
|
|
@@ -9,7 +9,6 @@ module ValidatesTimeliness
|
|
|
9
9
|
# It's a minor usability improvement which is rarely an issue for the user.
|
|
10
10
|
|
|
11
11
|
included do
|
|
12
|
-
alias_method_chain :datetime_selector, :timeliness
|
|
13
12
|
alias_method_chain :value, :timeliness
|
|
14
13
|
end
|
|
15
14
|
|
|
@@ -33,15 +32,8 @@ module ValidatesTimeliness
|
|
|
33
32
|
end
|
|
34
33
|
end
|
|
35
34
|
|
|
36
|
-
def datetime_selector_with_timeliness(*args)
|
|
37
|
-
@timeliness_date_or_time_tag = true
|
|
38
|
-
datetime_selector_without_timeliness(*args)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
35
|
def value_with_timeliness(object)
|
|
42
|
-
|
|
43
|
-
return value_without_timeliness(object)
|
|
44
|
-
end
|
|
36
|
+
return value_without_timeliness(object) unless @template_object.params[@object_name]
|
|
45
37
|
|
|
46
38
|
@template_object.params[@object_name]
|
|
47
39
|
|
|
@@ -56,6 +48,7 @@ module ValidatesTimeliness
|
|
|
56
48
|
|
|
57
49
|
TimelinessDateTime.new(*values)
|
|
58
50
|
end
|
|
51
|
+
|
|
59
52
|
end
|
|
60
53
|
end
|
|
61
54
|
end
|
|
@@ -1,69 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
module MultiparameterHandler
|
|
4
|
-
extend ActiveSupport::Concern
|
|
1
|
+
ActiveRecord::AttributeAssignment::MultiparameterAttribute.class_eval do
|
|
2
|
+
private
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
# Yield if date values are valid
|
|
5
|
+
def validate_multiparameter_date_values(set_values)
|
|
6
|
+
if set_values[0..2].all?{ |v| v.present? } && Date.valid_civil?(*set_values[0..2])
|
|
7
|
+
yield
|
|
8
|
+
else
|
|
9
|
+
invalid_multiparameter_date_or_time_as_string(set_values)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
def invalid_multiparameter_date_or_time_as_string(values)
|
|
14
|
+
value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-")
|
|
15
|
+
value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty?
|
|
16
|
+
value
|
|
17
|
+
end
|
|
13
18
|
|
|
14
|
-
|
|
19
|
+
def instantiate_time_object(set_values)
|
|
20
|
+
raise if set_values.any?(&:nil?)
|
|
15
21
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty?
|
|
19
|
-
value
|
|
20
|
-
end
|
|
22
|
+
validate_multiparameter_date_values(set_values) {
|
|
23
|
+
set_values = set_values.map {|v| v.is_a?(String) ? v.strip : v }
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
invalid_multiparameter_date_or_time_as_string(values)
|
|
27
|
-
end
|
|
25
|
+
if object.class.send(:create_time_zone_conversion_attribute?, name, cast_type_or_column)
|
|
26
|
+
Time.zone.local(*set_values)
|
|
27
|
+
else
|
|
28
|
+
Time.send(object.class.default_timezone, *set_values)
|
|
28
29
|
end
|
|
30
|
+
}
|
|
31
|
+
rescue
|
|
32
|
+
invalid_multiparameter_date_or_time_as_string(set_values)
|
|
33
|
+
end
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
def read_time
|
|
36
|
+
# If column is a :time (and not :date or :timestamp) there is no need to validate if
|
|
37
|
+
# there are year/month/day fields
|
|
38
|
+
if cast_type_or_column.type == :time
|
|
39
|
+
# if the column is a time set the values to their defaults as January 1, 1970, but only if they're nil
|
|
40
|
+
{ 1 => 1970, 2 => 1, 3 => 1 }.each do |key,value|
|
|
41
|
+
values[key] ||= value
|
|
35
42
|
end
|
|
43
|
+
end
|
|
36
44
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
callstack.each do |name, values_with_empty_parameters|
|
|
40
|
-
begin
|
|
41
|
-
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
|
|
42
|
-
values = values_with_empty_parameters.reject { |v| v.nil? }
|
|
45
|
+
max_position = extract_max_param(6)
|
|
46
|
+
set_values = values.values_at(*(1..max_position))
|
|
43
47
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
else
|
|
48
|
+
instantiate_time_object(set_values)
|
|
49
|
+
end
|
|
47
50
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
def read_date
|
|
52
|
+
set_values = values.values_at(1,2,3).map {|v| v.is_a?(String) ? v.strip : v }
|
|
53
|
+
|
|
54
|
+
if set_values.any? { |v| v.is_a?(String) }
|
|
55
|
+
Timeliness.parse(set_values.join('-'), :date).try(:to_date) or raise TypeError
|
|
56
|
+
else
|
|
57
|
+
Date.new(*set_values)
|
|
58
|
+
end
|
|
59
|
+
rescue TypeError, ArgumentError, NoMethodError => ex # if Date.new raises an exception on an invalid date
|
|
60
|
+
# Date.new with nil values throws NoMethodError
|
|
61
|
+
raise ex if ex.is_a?(NoMethodError) && ex.message !~ /undefined method `div' for/
|
|
62
|
+
invalid_multiparameter_date_or_time_as_string(set_values)
|
|
63
|
+
end
|
|
55
64
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
unless errors.empty?
|
|
63
|
-
raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
|
64
|
-
end
|
|
65
|
-
end
|
|
65
|
+
# Cast type is v4.2 and column before
|
|
66
|
+
def cast_type_or_column
|
|
67
|
+
@cast_type || @column
|
|
68
|
+
end
|
|
66
69
|
|
|
67
|
-
|
|
70
|
+
def timezone_conversion_attribute?
|
|
71
|
+
object.class.send(:create_time_zone_conversion_attribute?, name, column)
|
|
68
72
|
end
|
|
73
|
+
|
|
69
74
|
end
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
module ValidatesTimeliness
|
|
2
2
|
module Extensions
|
|
3
|
-
autoload :DateTimeSelect,
|
|
4
|
-
autoload :MultiparameterHandler, 'validates_timeliness/extensions/multiparameter_handler'
|
|
3
|
+
autoload :DateTimeSelect, 'validates_timeliness/extensions/date_time_select'
|
|
5
4
|
end
|
|
6
5
|
|
|
7
6
|
def self.enable_date_time_select_extension!
|
|
8
|
-
::ActionView::Helpers::
|
|
7
|
+
::ActionView::Helpers::Tags::DateSelect.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect)
|
|
9
8
|
end
|
|
10
9
|
|
|
11
10
|
def self.enable_multiparameter_extension!
|
|
12
|
-
|
|
11
|
+
require 'validates_timeliness/extensions/multiparameter_handler'
|
|
13
12
|
end
|
|
14
13
|
end
|
|
@@ -14,12 +14,13 @@ module ActiveModel
|
|
|
14
14
|
timeliness_validation_for attr_names, :datetime
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
def validates_timeliness_of(*attr_names)
|
|
18
|
+
timeliness_validation_for attr_names
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def timeliness_validation_for(attr_names, type=nil)
|
|
22
|
+
options = _merge_attributes(attr_names)
|
|
23
|
+
options.update(:type => type) if type
|
|
23
24
|
validates_with TimelinessValidator, options
|
|
24
25
|
end
|
|
25
26
|
end
|