validates_timeliness 3.0.14 → 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.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +21 -0
  3. data/README.rdoc +12 -9
  4. data/Rakefile +1 -1
  5. data/gemfiles/rails_4_0.gemfile +19 -0
  6. data/gemfiles/rails_4_1.gemfile +19 -0
  7. data/gemfiles/rails_4_2.gemfile +19 -0
  8. data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +3 -3
  9. data/lib/validates_timeliness/attribute_methods.rb +36 -36
  10. data/lib/validates_timeliness/extensions/date_time_select.rb +2 -9
  11. data/lib/validates_timeliness/extensions/multiparameter_handler.rb +59 -65
  12. data/lib/validates_timeliness/extensions.rb +3 -4
  13. data/lib/validates_timeliness/helper_methods.rb +8 -2
  14. data/lib/validates_timeliness/orm/active_record.rb +59 -13
  15. data/lib/validates_timeliness/validator.rb +20 -6
  16. data/lib/validates_timeliness/version.rb +1 -1
  17. data/lib/validates_timeliness.rb +9 -13
  18. data/spec/spec_helper.rb +4 -2
  19. data/spec/support/model_helpers.rb +4 -4
  20. data/spec/support/tag_matcher.rb +35 -0
  21. data/spec/support/test_model.rb +0 -1
  22. data/spec/validates_timeliness/attribute_methods_spec.rb +10 -10
  23. data/spec/validates_timeliness/conversion_spec.rb +41 -41
  24. data/spec/validates_timeliness/extensions/date_time_select_spec.rb +5 -4
  25. data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +8 -7
  26. data/spec/validates_timeliness/helper_methods_spec.rb +12 -12
  27. data/spec/validates_timeliness/orm/active_record_spec.rb +70 -80
  28. data/spec/validates_timeliness/validator/after_spec.rb +3 -3
  29. data/spec/validates_timeliness/validator/before_spec.rb +3 -3
  30. data/spec/validates_timeliness/validator/is_at_spec.rb +4 -4
  31. data/spec/validates_timeliness/validator/on_or_after_spec.rb +3 -3
  32. data/spec/validates_timeliness/validator/on_or_before_spec.rb +3 -3
  33. data/spec/validates_timeliness/validator_spec.rb +40 -29
  34. data/spec/validates_timeliness_spec.rb +8 -8
  35. data/validates_timeliness.gemspec +1 -1
  36. metadata +41 -67
  37. data/gemfiles/mongoid_2_1.gemfile +0 -16
  38. data/gemfiles/mongoid_2_2.gemfile +0 -16
  39. data/gemfiles/mongoid_2_3.gemfile +0 -16
  40. data/gemfiles/mongoid_2_4.gemfile +0 -16
  41. data/gemfiles/rails_3_0.gemfile +0 -15
  42. data/gemfiles/rails_3_1.gemfile +0 -15
  43. data/gemfiles/rails_3_2.gemfile +0 -15
  44. data/lib/validates_timeliness/orm/mongoid.rb +0 -63
  45. data/spec/validates_timeliness/orm/mongoid_spec.rb +0 -189
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/README.rdoc CHANGED
@@ -5,9 +5,9 @@
5
5
 
6
6
  == Description
7
7
 
8
- Complete validation of dates, times and datetimes for Rails 3.x and ActiveModel.
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 2.x go here[http://github.com/adzap/validates_timeliness/tree/v2.3].
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
@@ -30,7 +30,7 @@ If you a looking for the old version for Rails 2.x go here[http://github.com/adz
30
30
  == Installation
31
31
 
32
32
  # in Gemfile
33
- gem 'validates_timeliness', '~> 3.0'
33
+ gem 'validates_timeliness', '~> 4.0'
34
34
 
35
35
  # Run bundler
36
36
  $ bundle install
@@ -39,7 +39,7 @@ Then run
39
39
 
40
40
  $ rails generate validates_timeliness:install
41
41
 
42
- This creates configuration initializer and locale files. In the initializer, you there are a number of config
42
+ This creates configuration initializer and locale files. In the initializer, there are a number of config
43
43
  options to customize the plugin.
44
44
 
45
45
  NOTE: You may wish to enable the plugin parser and the extensions to start. Please read those sections first.
@@ -55,7 +55,10 @@ NOTE: You may wish to enable the plugin parser and the extensions to start. Plea
55
55
  validates_datetime :finish_time, :after => :start_time # Method symbol
56
56
 
57
57
  validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand.
58
- validates_time :booked_at, :between => ['9.00am', '5:00pm']
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
59
62
 
60
63
  validates_time :breakfast_time, :on_or_after => '6:00am',
61
64
  :on_or_after_message => 'must be after opening time',
@@ -134,8 +137,8 @@ like so
134
137
 
135
138
  ValidatesTimeliness.setup do |config|
136
139
 
137
- # Extend ORM/ODMs for full support (:active_record, :mongoid).
138
- config.extend_orms = [ :mongoid ]
140
+ # Extend ORM/ODMs for full support (:active_record).
141
+ config.extend_orms = [ :active_record ]
139
142
 
140
143
  end
141
144
 
@@ -173,8 +176,8 @@ You can also use validation options for custom error messages. The following opt
173
176
  :after_message
174
177
  :on_or_after_message
175
178
 
176
- Note: There is no :between_message option. The between error message should be defined using the
177
- :on_or_before and :on_or_after messages.
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.
178
181
 
179
182
  It is highly recommended you use the I18n system for error messages.
180
183
 
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'appraisal'
5
5
 
6
6
  Bundler::GemHelper.install_tasks
7
7
 
8
- require 'rake/rdoctask'
8
+ require 'rdoc/task'
9
9
  require 'rspec/core/rake_task'
10
10
 
11
11
  desc "Run specs"
@@ -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 => "../"
@@ -1,6 +1,6 @@
1
1
  ValidatesTimeliness.setup do |config|
2
- # Extend ORM/ODMs for full support (:active_record, :mongoid).
3
- # config.extend_orms = [ :active_record ]
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 amiguous year threshold when parsing a 2 digit year
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,13 +20,6 @@ module ValidatesTimeliness
20
20
  :datetime
21
21
  end
22
22
 
23
- def undefine_attribute_methods
24
- super
25
- undefine_timeliness_attribute_methods
26
- end
27
-
28
- protected
29
-
30
23
  def define_timeliness_methods(before_type_cast=false)
31
24
  return if timeliness_validated_attributes.blank?
32
25
  timeliness_validated_attributes.each do |attr_name|
@@ -34,55 +27,62 @@ module ValidatesTimeliness
34
27
  end
35
28
  end
36
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
+
37
50
  def define_attribute_timeliness_methods(attr_name, before_type_cast=false)
38
51
  define_timeliness_write_method(attr_name)
39
52
  define_timeliness_before_type_cast_method(attr_name) if before_type_cast
40
53
  end
41
54
 
42
55
  def define_timeliness_write_method(attr_name)
43
- method_body, line = <<-EOV, __LINE__ + 1
56
+ generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
44
57
  def #{attr_name}=(value)
45
- original_value = value
46
- @timeliness_cache ||= {}
47
- @timeliness_cache["#{attr_name}"] = original_value
48
- #{ "if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend" if ValidatesTimeliness.use_plugin_parser }
49
-
50
- super(value)
58
+ write_timeliness_attribute('#{attr_name}', value)
51
59
  end
52
- EOV
53
- generated_timeliness_methods.module_eval(method_body, __FILE__, line)
60
+ STR
54
61
  end
55
62
 
56
63
  def define_timeliness_before_type_cast_method(attr_name)
57
- method_body, line = <<-EOV, __LINE__ + 1
64
+ generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
58
65
  def #{attr_name}_before_type_cast
59
- _timeliness_raw_value_for('#{attr_name}') || @attributes['#{attr_name}']
66
+ read_timeliness_attribute_before_type_cast('#{attr_name}')
60
67
  end
61
- EOV
62
- generated_timeliness_methods.module_eval(method_body, __FILE__, line)
68
+ STR
63
69
  end
70
+ end
64
71
 
65
- def timeliness_type_cast_code(attr_name, var_name)
66
- type = timeliness_attribute_type(attr_name)
67
- timezone_aware = timeliness_attribute_timezone_aware?(attr_name)
68
- timezone = :current if timezone_aware
69
-
70
- "#{var_name} = Timeliness::Parser.parse(#{var_name}, :#{type}, :zone => #{timezone.inspect})"
71
- end
72
+ def write_timeliness_attribute(attr_name, value)
73
+ @timeliness_cache ||= {}
74
+ @timeliness_cache[attr_name] = value
72
75
 
73
- def generated_timeliness_methods
74
- @generated_timeliness_methods ||= Module.new.tap { |m| include(m) }
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)
75
79
  end
76
80
 
77
- def undefine_timeliness_attribute_methods
78
- generated_timeliness_methods.module_eval do
79
- instance_methods.each { |m| undef_method(m) }
80
- end
81
- end
81
+ @attributes[attr_name] = value
82
82
  end
83
83
 
84
- def _timeliness_raw_value_for(attr_name)
85
- @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]
86
86
  end
87
87
 
88
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
- unless @timeliness_date_or_time_tag && @template_object.params[@object_name]
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,80 +1,74 @@
1
- module ValidatesTimeliness
2
- module Extensions
3
- module MultiparameterHandler
4
- extend ActiveSupport::Concern
1
+ ActiveRecord::AttributeAssignment::MultiparameterAttribute.class_eval do
2
+ private
5
3
 
6
- # Stricter handling of date and time values from multiparameter
7
- # assignment from the date/time select view helpers
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
- included do
10
- alias_method_chain :instantiate_time_object, :timeliness
11
- alias_method :execute_callstack_for_multiparameter_attributes, :execute_callstack_for_multiparameter_attributes_with_timeliness
12
- alias_method :read_value_from_parameter, :read_value_from_parameter_with_timeliness
13
- end
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
14
18
 
15
- private
19
+ def instantiate_time_object(set_values)
20
+ raise if set_values.any?(&:nil?)
16
21
 
17
- def invalid_multiparameter_date_or_time_as_string(values)
18
- value = [values[0], *values[1..2].map {|s| s.to_s.rjust(2,"0")} ].join("-")
19
- value += ' ' + values[3..5].map {|s| s.to_s.rjust(2, "0") }.join(":") unless values[3..5].empty?
20
- value
21
- end
22
+ validate_multiparameter_date_values(set_values) {
23
+ set_values = set_values.map {|v| v.is_a?(String) ? v.strip : v }
22
24
 
23
- def instantiate_time_object_with_timeliness(name, values)
24
- validate_multiparameter_date_values(values) {
25
- instantiate_time_object_without_timeliness(name, values)
26
- }
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)
27
29
  end
30
+ }
31
+ rescue
32
+ invalid_multiparameter_date_or_time_as_string(set_values)
33
+ end
28
34
 
29
- def instantiate_date_object(name, values)
30
- validate_multiparameter_date_values(values) {
31
- Date.new(*values)
32
- }
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
33
42
  end
43
+ end
34
44
 
35
- # Yield if date values are valid
36
- def validate_multiparameter_date_values(values)
37
- if values[0..2].all?{ |v| v.present? } && Date.valid_civil?(*values[0..2])
38
- yield
39
- else
40
- invalid_multiparameter_date_or_time_as_string(values)
41
- end
42
- end
45
+ max_position = extract_max_param(6)
46
+ set_values = values.values_at(*(1..max_position))
43
47
 
44
- def read_value_from_parameter_with_timeliness(name, values_from_param)
45
- klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
46
- values = values_from_param.is_a?(Hash) ? values_from_param.to_a.sort_by(&:first).map(&:last) : values_from_param
48
+ instantiate_time_object(set_values)
49
+ end
47
50
 
48
- if values.empty? || values.all?{ |v| v.nil? }
49
- nil
50
- elsif klass == Time
51
- instantiate_time_object(name, values)
52
- elsif klass == Date
53
- instantiate_date_object(name, values)
54
- else
55
- if respond_to?(:read_other_parameter_value)
56
- read_date_parameter_value(name, values_from_param)
57
- else
58
- klass.new(*values)
59
- end
60
- end
61
- end
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
62
64
 
63
- def execute_callstack_for_multiparameter_attributes_with_timeliness(callstack)
64
- errors = []
65
- callstack.each do |name, values_with_empty_parameters|
66
- begin
67
- send(name + "=", read_value_from_parameter(name, values_with_empty_parameters))
68
- rescue => ex
69
- values = values_with_empty_parameters.is_a?(Hash) ? values_with_empty_parameters.values : values_with_empty_parameters
70
- errors << ActiveRecord::AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
71
- end
72
- end
73
- unless errors.empty?
74
- raise ActiveRecord::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
75
- end
76
- end
65
+ # Cast type is v4.2 and column before
66
+ def cast_type_or_column
67
+ @cast_type || @column
68
+ end
77
69
 
78
- end
70
+ def timezone_conversion_attribute?
71
+ object.class.send(:create_time_zone_conversion_attribute?, name, column)
79
72
  end
73
+
80
74
  end
@@ -1,14 +1,13 @@
1
1
  module ValidatesTimeliness
2
2
  module Extensions
3
- autoload :DateTimeSelect, 'validates_timeliness/extensions/date_time_select'
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::InstanceTag.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect)
7
+ ::ActionView::Helpers::Tags::DateSelect.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect)
9
8
  end
10
9
 
11
10
  def self.enable_multiparameter_extension!
12
- ::ActiveRecord::Base.send(:include, ValidatesTimeliness::Extensions::MultiparameterHandler)
11
+ require 'validates_timeliness/extensions/multiparameter_handler'
13
12
  end
14
13
  end
@@ -14,8 +14,14 @@ module ActiveModel
14
14
  timeliness_validation_for attr_names, :datetime
15
15
  end
16
16
 
17
- def timeliness_validation_for(attr_names, type)
18
- validates_with TimelinessValidator, _merge_attributes(attr_names).merge(:type => type)
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
24
+ validates_with TimelinessValidator, options
19
25
  end
20
26
  end
21
27
 
@@ -14,28 +14,74 @@ module ValidatesTimeliness
14
14
  timeliness_column_for_attribute(attr_name).type
15
15
  end
16
16
 
17
- def timeliness_column_for_attribute(attr_name)
18
- columns_hash.fetch(attr_name.to_s) do |attr_name|
19
- validation_type = _validators[attr_name.to_sym].find {|v| v.kind == :timeliness }.type
20
- ::ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, validation_type.to_s)
17
+ if ActiveModel.version >= Gem::Version.new('4.2')
18
+ def timeliness_column_for_attribute(attr_name)
19
+ columns_hash.fetch(attr_name.to_s) do |attr_name|
20
+ validation_type = _validators[attr_name.to_sym].find {|v| v.kind == :timeliness }.type.to_s
21
+ ::ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, lookup_cast_type(validation_type), validation_type)
22
+ end
23
+ end
24
+
25
+ def lookup_cast_type(sql_type)
26
+ case sql_type
27
+ when 'datetime' then ::ActiveRecord::Type::DateTime.new
28
+ when 'date' then ::ActiveRecord::Type::Date.new
29
+ when 'time' then ::ActiveRecord::Type::Time.new
30
+ end
31
+ end
32
+ else
33
+ def timeliness_column_for_attribute(attr_name)
34
+ columns_hash.fetch(attr_name.to_s) do |attr_name|
35
+ validation_type = _validators[attr_name.to_sym].find {|v| v.kind == :timeliness }.type.to_s
36
+ ::ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, validation_type)
37
+ end
21
38
  end
22
39
  end
23
40
 
24
41
  def define_attribute_methods
25
- super.tap do |attribute_methods_generated|
26
- define_timeliness_methods true
27
- end
42
+ super.tap {
43
+ generated_timeliness_methods.synchronize do
44
+ return if @timeliness_methods_generated
45
+ define_timeliness_methods true
46
+ @timeliness_methods_generated = true
47
+ end
48
+ }
28
49
  end
29
50
 
30
- protected
51
+ def undefine_attribute_methods
52
+ super.tap {
53
+ generated_timeliness_methods.synchronize do
54
+ return unless @timeliness_methods_generated
55
+ undefine_timeliness_attribute_methods
56
+ @timeliness_methods_generated = true
57
+ end
58
+ }
59
+ end
60
+ # Override to overwrite methods in ActiveRecord attribute method module because in AR 4+
61
+ # there is curious code which calls the method directly from the generated methods module
62
+ # via bind inside method_missing. This means our method in the formerly custom timeliness
63
+ # methods module was never reached.
64
+ def generated_timeliness_methods
65
+ generated_attribute_methods
66
+ end
67
+ end
31
68
 
32
- def timeliness_type_cast_code(attr_name, var_name)
33
- type = timeliness_attribute_type(attr_name)
69
+ def write_timeliness_attribute(attr_name, value)
70
+ @timeliness_cache ||= {}
71
+ @timeliness_cache[attr_name] = value
34
72
 
35
- method_body = super
36
- method_body << "\n#{var_name} = #{var_name}.to_date if #{var_name}" if type == :date
37
- method_body
73
+ if ValidatesTimeliness.use_plugin_parser
74
+ type = self.class.timeliness_attribute_type(attr_name)
75
+ timezone = :current if self.class.timeliness_attribute_timezone_aware?(attr_name)
76
+ value = Timeliness::Parser.parse(value, type, :zone => timezone)
77
+ value = value.to_date if value && type == :date
38
78
  end
79
+
80
+ write_attribute(attr_name, value)
81
+ end
82
+
83
+ def read_timeliness_attribute_before_type_cast(attr_name)
84
+ @timeliness_cache && @timeliness_cache[attr_name] || read_attribute_before_type_cast(attr_name)
39
85
  end
40
86
 
41
87
  def reload(*args)
@@ -1,10 +1,11 @@
1
+ require 'active_model'
1
2
  require 'active_model/validator'
2
3
 
3
4
  module ValidatesTimeliness
4
5
  class Validator < ActiveModel::EachValidator
5
6
  include Conversion
6
7
 
7
- attr_reader :type
8
+ attr_reader :type, :attributes
8
9
 
9
10
  RESTRICTIONS = {
10
11
  :is_at => :==,
@@ -32,20 +33,33 @@ module ValidatesTimeliness
32
33
 
33
34
  if range = options.delete(:between)
34
35
  raise ArgumentError, ":between must be a Range or an Array" unless range.is_a?(Range) || range.is_a?(Array)
35
- options[:on_or_after], options[:on_or_before] = range.first, range.last
36
+ options[:on_or_after] = range.first
37
+ if range.is_a?(Range) && range.exclude_end?
38
+ options[:before] = range.last
39
+ else
40
+ options[:on_or_before] = range.last
41
+ end
36
42
  end
37
43
 
38
44
  @restrictions_to_check = RESTRICTIONS.keys & options.keys
45
+
39
46
  super
47
+
48
+ setup_timeliness_validated_attributes(options[:class]) if options[:class]
40
49
  end
41
50
 
42
- def setup(model)
51
+ def setup_timeliness_validated_attributes(model)
43
52
  if model.respond_to?(:timeliness_validated_attributes)
44
53
  model.timeliness_validated_attributes ||= []
45
- model.timeliness_validated_attributes |= @attributes
54
+ model.timeliness_validated_attributes |= attributes
46
55
  end
47
56
  end
48
57
 
58
+ # Rails 4.0 compatibility for old #setup method with class as arg
59
+ if ActiveModel.version <= Gem::Version.new('4.1')
60
+ alias_method(:setup, :setup_timeliness_validated_attributes)
61
+ end
62
+
49
63
  def validate_each(record, attr_name, value)
50
64
  raw_value = attribute_raw_value(record, attr_name) || value
51
65
  return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?)
@@ -87,8 +101,8 @@ module ValidatesTimeliness
87
101
  end
88
102
 
89
103
  def attribute_raw_value(record, attr_name)
90
- record.respond_to?(:_timeliness_raw_value_for) &&
91
- record._timeliness_raw_value_for(attr_name.to_s)
104
+ record.respond_to?(:read_timeliness_attribute_before_type_cast) &&
105
+ record.read_timeliness_attribute_before_type_cast(attr_name.to_s)
92
106
  end
93
107
 
94
108
  def timezone_aware?(record, attr_name)
@@ -1,3 +1,3 @@
1
1
  module ValidatesTimeliness
2
- VERSION = '3.0.14'
2
+ VERSION = '4.0.0'
3
3
  end