mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
change ORM attribute generation and extension mechanism
now using shim since the attribute matcher is not required for AM
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
ValidatesTimeliness.setup do |config|
|
||||
# Add validation helpers to these classes
|
||||
# config.extend_classes = [ ActiveRecord::Base ]
|
||||
# Add plugin to supported ORMs (only :active_record for now)
|
||||
# config.extend_orms = [ :active_record ]
|
||||
#
|
||||
# Set the dummy date part for a time type values.
|
||||
# config.dummy_date_for_time_type = [ 2000, 1, 1 ]
|
||||
|
||||
@@ -15,9 +15,9 @@ require 'active_support/core_ext/date_time/zones'
|
||||
module ValidatesTimeliness
|
||||
autoload :VERSION, 'validates_timeliness/version'
|
||||
|
||||
# Add validation helpers to these classes
|
||||
mattr_accessor :extend_classes
|
||||
@@extend_classes = [ defined?(ActiveRecord) && ActiveRecord::Base ].compact
|
||||
# Add plugin to supported ORMs (only :active_record for now)
|
||||
mattr_accessor :extend_orms
|
||||
@@extend_orms = [ defined?(ActiveRecord) && :active_record ].compact
|
||||
|
||||
# Set the dummy date part for a time type values.
|
||||
mattr_accessor :dummy_date_for_time_type
|
||||
@@ -37,10 +37,7 @@ module ValidatesTimeliness
|
||||
# Setup method for plugin configuration
|
||||
def self.setup
|
||||
yield self
|
||||
extend_classes.each {|klass|
|
||||
klass.send(:include, ValidatesTimeliness::HelperMethods)
|
||||
klass.send(:include, ValidatesTimeliness::AttributeMethods)
|
||||
}
|
||||
extend_orms.each {|orm| require "validates_timeliness/orms/#{orm}" }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -2,28 +2,39 @@ module ValidatesTimeliness
|
||||
module AttributeMethods
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
if attribute_method_matchers.any? {|m| m.suffix == "_before_type_cast" && m.prefix.blank? }
|
||||
extend BeforeTypeCastMethods
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
||||
def define_timeliness_methods(before_type_cast=false)
|
||||
timeliness_validated_attributes.each do |attr_name, type|
|
||||
define_timeliness_write_method(attr_name, type, timeliness_attribute_timezone_aware?(attr_name))
|
||||
define_timeliness_before_type_cast_method(attr_name) if before_type_cast
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def define_method_attribute=(attr_name)
|
||||
if timeliness_validated_attributes.include?(attr_name)
|
||||
method_body, line = <<-EOV, __LINE__ + 1
|
||||
def #{attr_name}=(value)
|
||||
@attributes_cache ||= {}
|
||||
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
||||
super
|
||||
end
|
||||
EOV
|
||||
class_eval(method_body, __FILE__, line)
|
||||
end
|
||||
super rescue(NoMethodError)
|
||||
def define_timeliness_write_method(attr_name, type, timezone_aware)
|
||||
method_body, line = <<-EOV, __LINE__ + 1
|
||||
def #{attr_name}=(value)
|
||||
@attributes_cache ||= {}
|
||||
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
||||
super
|
||||
end
|
||||
EOV
|
||||
class_eval(method_body, __FILE__, line)
|
||||
end
|
||||
|
||||
def define_timeliness_before_type_cast_method(attr_name)
|
||||
method_body, line = <<-EOV, __LINE__ + 1
|
||||
def #{attr_name}_before_type_cast
|
||||
_timeliness_raw_value_for('#{attr_name}')
|
||||
end
|
||||
EOV
|
||||
class_eval(method_body, __FILE__, line)
|
||||
end
|
||||
|
||||
def timeliness_attribute_timezone_aware?(attr_name)
|
||||
false
|
||||
end
|
||||
|
||||
end
|
||||
@@ -36,22 +47,5 @@ module ValidatesTimeliness
|
||||
|
||||
end
|
||||
|
||||
module BeforeTypeCastMethods
|
||||
|
||||
def define_method_attribute_before_type_cast(attr_name)
|
||||
if timeliness_validated_attributes.include?(attr_name)
|
||||
method_body, line = <<-EOV, __LINE__ + 1
|
||||
def #{attr_name}_before_type_cast
|
||||
_timeliness_raw_value_for('#{attr_name}')
|
||||
end
|
||||
EOV
|
||||
class_eval(method_body, __FILE__, line)
|
||||
else
|
||||
super rescue(NoMethodError)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,30 +5,37 @@ module ValidatesTimeliness
|
||||
included do
|
||||
include ValidationMethods
|
||||
extend ValidationMethods
|
||||
class_inheritable_accessor :timeliness_validated_attributes
|
||||
self.timeliness_validated_attributes = {}
|
||||
end
|
||||
|
||||
module ValidationMethods
|
||||
def validates_timeliness_of(*attr_names)
|
||||
options = _merge_attributes(attr_names)
|
||||
attributes = options[:attributes].inject({}) {|validated, attr_name|
|
||||
attr_name = attr_name.to_s
|
||||
validated[attr_name] = options[:type]
|
||||
validated
|
||||
}
|
||||
timeliness_validated_attributes.update(attributes)
|
||||
validates_with Validator, options
|
||||
end
|
||||
|
||||
def validates_date(*attr_names)
|
||||
validates_with Validator, _merge_attributes(attr_names).merge(:type => :date)
|
||||
options = attr_names.extract_options!
|
||||
validates_timeliness_of *(attr_names << options.merge(:type => :date))
|
||||
end
|
||||
|
||||
def validates_time(*attr_names)
|
||||
validates_with Validator, _merge_attributes(attr_names).merge(:type => :time)
|
||||
options = attr_names.extract_options!
|
||||
validates_timeliness_of *(attr_names << options.merge(:type => :time))
|
||||
end
|
||||
|
||||
def validates_datetime(*attr_names)
|
||||
validates_with Validator, _merge_attributes(attr_names).merge(:type => :datetime)
|
||||
options = attr_names.extract_options!
|
||||
validates_timeliness_of *(attr_names << options.merge(:type => :datetime))
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def timeliness_validated_attributes
|
||||
@timeliness_validated_attributes ||= begin
|
||||
_validators.map do |attr_name, validators|
|
||||
attr_name.to_s if validators.any? {|v| v.is_a?(ValidatesTimeliness::Validator) }
|
||||
end.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
14
lib/validates_timeliness/orms/active_record.rb
Normal file
14
lib/validates_timeliness/orms/active_record.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
class ActiveRecord::Base
|
||||
include ValidatesTimeliness::HelperMethods
|
||||
include ValidatesTimeliness::AttributeMethods
|
||||
|
||||
def self.define_attribute_methods
|
||||
super
|
||||
# Define write method and before_type_cast method
|
||||
define_timeliness_methods(true)
|
||||
end
|
||||
|
||||
def self.timeliness_attribute_timezone_aware?(attr_name)
|
||||
create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
|
||||
end
|
||||
end
|
||||
@@ -18,9 +18,13 @@ module ValidatesTimeliness
|
||||
:timeliness
|
||||
end
|
||||
|
||||
def setup(klass)
|
||||
@klass = klass
|
||||
end
|
||||
|
||||
def initialize(options)
|
||||
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
|
||||
@type = options.delete(:type) || :datetime
|
||||
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
|
||||
@restrictions_to_check = RESTRICTIONS.keys & options.keys
|
||||
|
||||
if range = options.delete(:between)
|
||||
|
||||
Reference in New Issue
Block a user