use setup block to config which classes to extend

This commit is contained in:
Adam Meehan
2010-08-01 23:27:25 +10:00
parent 6acf61aa88
commit d5124f32b2
3 changed files with 29 additions and 19 deletions

View File

@@ -14,6 +14,10 @@ require 'active_support/core_ext/date_time/zones'
module ValidatesTimeliness
# Add validation helpers to these classes
mattr_accessor :extend_classes
@@extend_classes = []
# Set the dummy date part for a time type values.
mattr_accessor :dummy_date_for_time_type
@@dummy_date_for_time_type = [ 2000, 1, 1 ]
@@ -21,6 +25,12 @@ module ValidatesTimeliness
# Ignore errors when restriction options are evaluated
mattr_accessor :ignore_restriction_errors
@@ignore_restriction_errors = false
# Setup method for plugin configuration
def self.setup
yield self
extend_classes.each {|klass| klass.send(:include, ValidatesTimeliness::HelperMethods) }
end
end
require 'validates_timeliness/conversion'

View File

@@ -1,19 +1,19 @@
module ValidatesTimeliness
module HelperMethods
def validates_date(*attr_names)
validates_with Validator, _merge_attributes(attr_names).merge(:type => :date)
end
extend ActiveSupport::Concern
def validates_time(*attr_names)
validates_with Validator, _merge_attributes(attr_names).merge(:type => :time)
end
module ClassMethods
def validates_date(*attr_names)
validates_with Validator, _merge_attributes(attr_names).merge(:type => :date)
end
def validates_datetime(*attr_names)
validates_with Validator, _merge_attributes(attr_names).merge(:type => :datetime)
def validates_time(*attr_names)
validates_with Validator, _merge_attributes(attr_names).merge(:type => :time)
end
def validates_datetime(*attr_names)
validates_with Validator, _merge_attributes(attr_names).merge(:type => :datetime)
end
end
end
end
module ActiveModel::Validations::HelperMethods
include ValidatesTimeliness::HelperMethods
end