From bb830baea24c4f05b1fd4b75f225916a1f3305ed Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 10 Nov 2015 17:52:34 +1100 Subject: [PATCH] Rails 4.0 and 4.1 compat for #setup method removal --- lib/validates_timeliness/helper_methods.rb | 12 ++++++++++-- lib/validates_timeliness/validator.rb | 11 ++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/validates_timeliness/helper_methods.rb b/lib/validates_timeliness/helper_methods.rb index 122dfe3..107ede6 100644 --- a/lib/validates_timeliness/helper_methods.rb +++ b/lib/validates_timeliness/helper_methods.rb @@ -14,8 +14,16 @@ module ActiveModel timeliness_validation_for attr_names, :datetime end - def timeliness_validation_for(attr_names, type) - validates_with TimelinessValidator, _merge_attributes(attr_names).merge(:type => type) + def validates_timeliness_of(*attr_names) + timeliness_validation_for attr_names + end + + 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) end end diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 806dbe2..651f503 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -5,7 +5,7 @@ module ValidatesTimeliness class Validator < ActiveModel::EachValidator include Conversion - attr_reader :type + attr_reader :type, :attributes RESTRICTIONS = { :is_at => :==, @@ -42,13 +42,14 @@ module ValidatesTimeliness end @restrictions_to_check = RESTRICTIONS.keys & options.keys - super - end - def setup(model) + super + + model = options[:class] + if model.respond_to?(:timeliness_validated_attributes) model.timeliness_validated_attributes ||= [] - model.timeliness_validated_attributes |= @attributes + model.timeliness_validated_attributes |= attributes end end