From feb508f1710629c4a9108d9cf5d614d53a9a2f01 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 29 Dec 2015 14:57:56 +1100 Subject: [PATCH] Validator 4.x compat --- lib/validates_timeliness/helper_methods.rb | 4 +--- lib/validates_timeliness/validator.rb | 13 ++++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/validates_timeliness/helper_methods.rb b/lib/validates_timeliness/helper_methods.rb index 107ede6..6d3a9a4 100644 --- a/lib/validates_timeliness/helper_methods.rb +++ b/lib/validates_timeliness/helper_methods.rb @@ -21,9 +21,7 @@ module ActiveModel def timeliness_validation_for(attr_names, type=nil) options = _merge_attributes(attr_names) options.update(:type => type) if type - # Rails 4.0 and 4.1 compatibility for old #setup method with class as arg - options.update(:class => self) unless options.has_key?(:class) - validates_with TimelinessValidator, _merge_attributes(attr_names) + validates_with TimelinessValidator, options end end diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 651f503..0e2c8b4 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -45,14 +45,21 @@ module ValidatesTimeliness super - model = options[:class] + setup_timeliness_validated_attributes(options[:class]) if options[:class] + end + def setup_timeliness_validated_attributes(model) if model.respond_to?(:timeliness_validated_attributes) model.timeliness_validated_attributes ||= [] model.timeliness_validated_attributes |= attributes end end + # Rails 4.0 compatibility for old #setup method with class as arg + if ActiveModel.version <= Gem::Version.new('4.1') + alias_method(:setup, :setup_timeliness_validated_attributes) + end + def validate_each(record, attr_name, value) raw_value = attribute_raw_value(record, attr_name) || value return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?) @@ -94,8 +101,8 @@ module ValidatesTimeliness end def attribute_raw_value(record, attr_name) - record.respond_to?(:_timeliness_raw_value_for) && - record._timeliness_raw_value_for(attr_name.to_s) + record.respond_to?(:read_timeliness_attribute_before_type_cast) && + record.read_timeliness_attribute_before_type_cast(attr_name.to_s) end def timezone_aware?(record, attr_name)