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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +21 -0
  3. data/CHANGELOG.rdoc +21 -1
  4. data/README.rdoc +20 -22
  5. data/Rakefile +5 -1
  6. data/gemfiles/rails_4_0.gemfile +19 -0
  7. data/gemfiles/rails_4_1.gemfile +19 -0
  8. data/gemfiles/rails_4_2.gemfile +19 -0
  9. data/lib/generators/validates_timeliness/install_generator.rb +0 -1
  10. data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +3 -3
  11. data/lib/validates_timeliness/attribute_methods.rb +42 -26
  12. data/lib/validates_timeliness/extensions/date_time_select.rb +2 -9
  13. data/lib/validates_timeliness/extensions/multiparameter_handler.rb +59 -54
  14. data/lib/validates_timeliness/extensions.rb +3 -4
  15. data/lib/validates_timeliness/helper_methods.rb +7 -6
  16. data/lib/validates_timeliness/orm/active_record.rb +70 -15
  17. data/lib/validates_timeliness/railtie.rb +2 -1
  18. data/lib/validates_timeliness/validator.rb +29 -4
  19. data/lib/validates_timeliness/version.rb +1 -1
  20. data/lib/validates_timeliness.rb +13 -13
  21. data/spec/spec_helper.rb +10 -5
  22. data/spec/support/config_helper.rb +10 -0
  23. data/spec/support/model_helpers.rb +4 -4
  24. data/spec/support/tag_matcher.rb +35 -0
  25. data/spec/support/test_model.rb +0 -1
  26. data/spec/validates_timeliness/attribute_methods_spec.rb +22 -7
  27. data/spec/validates_timeliness/conversion_spec.rb +41 -41
  28. data/spec/validates_timeliness/extensions/date_time_select_spec.rb +5 -4
  29. data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +28 -16
  30. data/spec/validates_timeliness/helper_methods_spec.rb +12 -12
  31. data/spec/validates_timeliness/orm/active_record_spec.rb +178 -44
  32. data/spec/validates_timeliness/validator/after_spec.rb +3 -3
  33. data/spec/validates_timeliness/validator/before_spec.rb +3 -3
  34. data/spec/validates_timeliness/validator/is_at_spec.rb +4 -4
  35. data/spec/validates_timeliness/validator/on_or_after_spec.rb +3 -3
  36. data/spec/validates_timeliness/validator/on_or_before_spec.rb +3 -3
  37. data/spec/validates_timeliness/validator_spec.rb +66 -25
  38. data/spec/validates_timeliness_spec.rb +8 -8
  39. data/validates_timeliness.gemspec +2 -2
  40. metadata +41 -60
  41. data/lib/validates_timeliness/orm/mongoid.rb +0 -54
  42. data/spec/validates_timeliness/orm/mongoid_spec.rb +0 -113
@@ -4,36 +4,91 @@ module ValidatesTimeliness
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  module ClassMethods
7
- def define_attribute_methods
8
- super
9
- # Define write method and before_type_cast method
10
- use_before_type_cast = ::ActiveRecord::VERSION::STRING < '3.1.0'
11
- define_timeliness_methods(use_before_type_cast)
12
- end
7
+ public
13
8
 
14
9
  def timeliness_attribute_timezone_aware?(attr_name)
15
- attr_name = attr_name.to_s
16
- create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
10
+ create_time_zone_conversion_attribute?(attr_name, timeliness_column_for_attribute(attr_name))
17
11
  end
18
12
 
19
13
  def timeliness_attribute_type(attr_name)
20
- columns_hash[attr_name.to_s].type
14
+ timeliness_column_for_attribute(attr_name).type
15
+ end
16
+
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
38
+ end
39
+ end
40
+
41
+ def define_attribute_methods
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
+ }
21
49
  end
22
50
 
23
- def timeliness_type_cast_code(attr_name, var_name)
24
- type = timeliness_attribute_type(attr_name)
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
68
+
69
+ def write_timeliness_attribute(attr_name, value)
70
+ @timeliness_cache ||= {}
71
+ @timeliness_cache[attr_name] = value
25
72
 
26
- <<-END
27
- #{super}
28
- #{var_name} = #{var_name}.to_date if #{var_name} && :#{type} == :date
29
- END
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
30
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)
31
85
  end
32
86
 
33
87
  def reload(*args)
34
88
  _clear_timeliness_cache
35
89
  super
36
90
  end
91
+
37
92
  end
38
93
  end
39
94
  end
@@ -3,7 +3,8 @@ module ValidatesTimeliness
3
3
  initializer "validates_timeliness.initialize_active_record", :after => 'active_record.initialize_timezone' do
4
4
  ActiveSupport.on_load(:active_record) do
5
5
  ValidatesTimeliness.default_timezone = ActiveRecord::Base.default_timezone
6
- ValidatesTimeliness.extend_orms = [ :active_record ]
6
+ ValidatesTimeliness.extend_orms << :active_record
7
+ ValidatesTimeliness.load_orms
7
8
  end
8
9
  end
9
10
 
@@ -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 => :==,
@@ -22,17 +23,41 @@ module ValidatesTimeliness
22
23
 
23
24
  RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
24
25
 
26
+ def self.kind
27
+ :timeliness
28
+ end
29
+
25
30
  def initialize(options)
26
31
  @type = options.delete(:type) || :datetime
27
32
  @allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
28
33
 
29
34
  if range = options.delete(:between)
30
35
  raise ArgumentError, ":between must be a Range or an Array" unless range.is_a?(Range) || range.is_a?(Array)
31
- 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
32
42
  end
33
43
 
34
44
  @restrictions_to_check = RESTRICTIONS.keys & options.keys
45
+
35
46
  super
47
+
48
+ setup_timeliness_validated_attributes(options[:class]) if options[:class]
49
+ end
50
+
51
+ def setup_timeliness_validated_attributes(model)
52
+ if model.respond_to?(:timeliness_validated_attributes)
53
+ model.timeliness_validated_attributes ||= []
54
+ model.timeliness_validated_attributes |= attributes
55
+ end
56
+ end
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)
36
61
  end
37
62
 
38
63
  def validate_each(record, attr_name, value)
@@ -76,8 +101,8 @@ module ValidatesTimeliness
76
101
  end
77
102
 
78
103
  def attribute_raw_value(record, attr_name)
79
- record.respond_to?(:_timeliness_raw_value_for) &&
80
- record._timeliness_raw_value_for(attr_name)
104
+ record.respond_to?(:read_timeliness_attribute_before_type_cast) &&
105
+ record.read_timeliness_attribute_before_type_cast(attr_name.to_s)
81
106
  end
82
107
 
83
108
  def timezone_aware?(record, attr_name)
@@ -1,3 +1,3 @@
1
1
  module ValidatesTimeliness
2
- VERSION = '3.0.8'
2
+ VERSION = '4.0.0'
3
3
  end
@@ -24,26 +24,24 @@ module ValidatesTimeliness
24
24
 
25
25
  class << self
26
26
  delegate :default_timezone, :default_timezone=, :dummy_date_for_time_type, :dummy_date_for_time_type=, :to => Timeliness
27
+
28
+ attr_accessor :extend_orms, :ignore_restriction_errors, :restriction_shorthand_symbols, :use_plugin_parser
27
29
  end
28
30
 
29
- # Extend ORM/ODMs for full support (:active_record, :mongoid).
30
- mattr_accessor :extend_orms
31
- @@extend_orms = []
31
+ # Extend ORM/ODMs for full support (:active_record).
32
+ self.extend_orms = []
32
33
 
33
34
  # Ignore errors when restriction options are evaluated
34
- mattr_accessor :ignore_restriction_errors
35
- @@ignore_restriction_errors = false
35
+ self.ignore_restriction_errors = false
36
36
 
37
37
  # Shorthand time and date symbols for restrictions
38
- mattr_accessor :restriction_shorthand_symbols
39
- @@restriction_shorthand_symbols = {
38
+ self.restriction_shorthand_symbols = {
40
39
  :now => lambda { Time.current },
41
40
  :today => lambda { Date.current }
42
41
  }
43
42
 
44
43
  # Use the plugin date/time parser which is stricter and extensible
45
- mattr_accessor :use_plugin_parser
46
- @@use_plugin_parser = false
44
+ self.use_plugin_parser = false
47
45
 
48
46
  # Default timezone
49
47
  self.default_timezone = :utc
@@ -51,15 +49,17 @@ module ValidatesTimeliness
51
49
  # Set the dummy date part for a time type values.
52
50
  self.dummy_date_for_time_type = [ 2000, 1, 1 ]
53
51
 
54
- def self.parser
55
- Timeliness
56
- end
57
-
58
52
  # Setup method for plugin configuration
59
53
  def self.setup
60
54
  yield self
55
+ load_orms
56
+ end
57
+
58
+ def self.load_orms
61
59
  extend_orms.each {|orm| require "validates_timeliness/orm/#{orm}" }
62
60
  end
61
+
62
+ def self.parser; Timeliness end
63
63
  end
64
64
 
65
65
  require 'validates_timeliness/conversion'
data/spec/spec_helper.rb CHANGED
@@ -5,13 +5,13 @@ require 'active_model/validations'
5
5
  require 'active_record'
6
6
  require 'action_view'
7
7
  require 'timecop'
8
- require 'rspec_tag_matchers'
9
8
 
10
9
  require 'validates_timeliness'
11
10
 
12
11
  require 'support/test_model'
13
12
  require 'support/model_helpers'
14
13
  require 'support/config_helper'
14
+ require 'support/tag_matcher'
15
15
 
16
16
  ValidatesTimeliness.setup do |c|
17
17
  c.extend_orms = [ :active_record ]
@@ -24,6 +24,7 @@ Time.zone = 'Australia/Melbourne'
24
24
 
25
25
  LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/../lib/generators/validates_timeliness/templates/en.yml')
26
26
  I18n.load_path.unshift(LOCALE_PATH)
27
+ I18n.available_locales = ['en', 'es']
27
28
 
28
29
  # Extend TestModel as you would another ORM/ODM module
29
30
  module TestModelShim
@@ -57,6 +58,7 @@ class PersonWithShim < Person
57
58
  include TestModelShim
58
59
  end
59
60
 
61
+ ActiveRecord::Base.default_timezone = :utc
60
62
  ActiveRecord::Base.time_zone_aware_attributes = true
61
63
  ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'})
62
64
  ActiveRecord::Migration.verbose = false
@@ -84,12 +86,15 @@ end
84
86
 
85
87
  RSpec.configure do |c|
86
88
  c.mock_with :rspec
87
- c.include(RspecTagMatchers)
89
+ c.include(TagMatcher)
88
90
  c.include(ModelHelpers)
89
91
  c.include(ConfigHelper)
90
92
  c.before do
91
- Person.reset_callbacks(:validate)
92
- PersonWithShim.timeliness_validated_attributes = []
93
- Person._validators.clear
93
+ reset_validation_setup_for(Person)
94
+ reset_validation_setup_for(PersonWithShim)
94
95
  end
96
+
97
+ c.filter_run_excluding :active_record => lambda {|version|
98
+ !(::ActiveRecord::VERSION::STRING.to_s =~ /^#{version.to_s}/)
99
+ }
95
100
  end
@@ -10,6 +10,16 @@ module ConfigHelper
10
10
  ValidatesTimeliness.send(:"#{preference_name}=", old_value)
11
11
  end
12
12
 
13
+ def reset_validation_setup_for(model_class)
14
+ model_class.reset_callbacks(:validate)
15
+ model_class._validators.clear
16
+ model_class.timeliness_validated_attributes = [] if model_class.respond_to?(:timeliness_validated_attributes)
17
+ model_class.undefine_attribute_methods
18
+ # This is a hack to avoid a disabled super method error message after an undef
19
+ model_class.instance_variable_set(:@generated_attribute_methods, nil)
20
+ model_class.instance_variable_set(:@generated_timeliness_methods, nil)
21
+ end
22
+
13
23
  module ClassMethods
14
24
  def with_config(preference_name, temporary_value)
15
25
  original_config_value = ValidatesTimeliness.send(preference_name)
@@ -3,15 +3,15 @@ module ModelHelpers
3
3
  # Some test helpers from Rails source
4
4
  def invalid!(attr_name, values, error = nil)
5
5
  with_each_person_value(attr_name, values) do |record, value|
6
- record.should be_invalid
7
- record.errors[attr_name].size.should >= 1
8
- record.errors[attr_name].first.should == error if error
6
+ expect(record).to be_invalid
7
+ expect(record.errors[attr_name].size).to be >= 1
8
+ expect(record.errors[attr_name].first).to eq(error) if error
9
9
  end
10
10
  end
11
11
 
12
12
  def valid!(attr_name, values)
13
13
  with_each_person_value(attr_name, values) do |record, value|
14
- record.should be_valid
14
+ expect(record).to be_valid
15
15
  end
16
16
  end
17
17
 
@@ -0,0 +1,35 @@
1
+ require 'nokogiri'
2
+
3
+ module TagMatcher
4
+ extend RSpec::Matchers::DSL
5
+
6
+ matcher :have_tag do |selector|
7
+ match do |subject|
8
+ matches = doc(subject).search(selector)
9
+
10
+ if @inner_text
11
+ matches = matches.select { |element| element.inner_text == @inner_text }
12
+ end
13
+
14
+ matches.any?
15
+ end
16
+
17
+ chain :with_inner_text do |inner_text|
18
+ @inner_text = inner_text
19
+ end
20
+
21
+ private
22
+
23
+ def body(subject)
24
+ if subject.respond_to?(:body)
25
+ subject.body
26
+ else
27
+ subject.to_s
28
+ end
29
+ end
30
+
31
+ def doc(subject)
32
+ @doc ||= Nokogiri::HTML(body(subject))
33
+ end
34
+ end
35
+ end
@@ -5,7 +5,6 @@ module TestModel
5
5
  include ActiveModel::AttributeMethods
6
6
 
7
7
  included do
8
- attribute_method_suffix ""
9
8
  attribute_method_suffix "="
10
9
  cattr_accessor :model_attributes
11
10
  end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ValidatesTimeliness::AttributeMethods do
4
- it 'should define _timeliness_raw_value_for instance method' do
5
- PersonWithShim.new.should respond_to(:_timeliness_raw_value_for)
4
+ it 'should define read_timeliness_attribute_before_type_cast instance method' do
5
+ expect(PersonWithShim.new).to respond_to(:read_timeliness_attribute_before_type_cast)
6
6
  end
7
7
 
8
8
  describe ".timeliness_validated_attributes" do
@@ -12,7 +12,7 @@ describe ValidatesTimeliness::AttributeMethods do
12
12
  PersonWithShim.validates_time :birth_time
13
13
  PersonWithShim.validates_datetime :birth_datetime
14
14
 
15
- PersonWithShim.timeliness_validated_attributes.should == [ :birth_date, :birth_time, :birth_datetime ]
15
+ expect(PersonWithShim.timeliness_validated_attributes).to eq([ :birth_date, :birth_time, :birth_datetime ])
16
16
  end
17
17
  end
18
18
 
@@ -31,13 +31,28 @@ describe ValidatesTimeliness::AttributeMethods do
31
31
  it 'should cache attribute raw value' do
32
32
  r = PersonWithCache.new
33
33
  r.birth_datetime = date_string = '2010-01-01'
34
- r._timeliness_raw_value_for(:birth_datetime).should == date_string
34
+ expect(r.read_timeliness_attribute_before_type_cast('birth_datetime')).to eq(date_string)
35
35
  end
36
36
 
37
37
  it 'should not overwrite user defined methods' do
38
38
  e = Employee.new
39
39
  e.birth_date = '2010-01-01'
40
- e.redefined_birth_date_called.should be_true
40
+ expect(e.redefined_birth_date_called).to be_truthy
41
+ end
42
+
43
+ it 'should be undefined if model class has dynamic attribute methods reset' do
44
+ # Force method definitions
45
+ PersonWithShim.validates_date :birth_date
46
+ r = PersonWithShim.new
47
+ r.birth_date = Time.now
48
+
49
+ write_method = :birth_date=
50
+
51
+ expect(PersonWithShim.send(:generated_timeliness_methods).instance_methods).to include(write_method)
52
+
53
+ PersonWithShim.undefine_attribute_methods
54
+
55
+ expect(PersonWithShim.send(:generated_timeliness_methods).instance_methods).not_to include(write_method)
41
56
  end
42
57
 
43
58
  context "with plugin parser" do
@@ -55,7 +70,7 @@ describe ValidatesTimeliness::AttributeMethods do
55
70
  end
56
71
 
57
72
  it 'should parse a string value' do
58
- Timeliness::Parser.should_receive(:parse)
73
+ expect(Timeliness::Parser).to receive(:parse)
59
74
  r = PersonWithParser.new
60
75
  r.birth_date = '2010-01-01'
61
76
  end
@@ -65,7 +80,7 @@ describe ValidatesTimeliness::AttributeMethods do
65
80
 
66
81
  context "before_type_cast method" do
67
82
  it 'should not be defined if ORM does not support it' do
68
- PersonWithShim.new.should_not respond_to(:birth_datetime_before_type_cast)
83
+ expect(PersonWithShim.new).not_to respond_to(:birth_datetime_before_type_cast)
69
84
  end
70
85
  end
71
86
  end