mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 15:22:58 +00:00
refactored to get attribute type from model types not validation type
reverts to behaviour of old version which allows you to define validations of any type for sake of the values it is validating against
This commit is contained in:
@@ -4,17 +4,20 @@ module ValidatesTimeliness
|
||||
|
||||
module ClassMethods
|
||||
|
||||
protected
|
||||
|
||||
def define_timeliness_methods(before_type_cast=false)
|
||||
return if timeliness_validated_attributes.blank?
|
||||
timeliness_validated_attributes.each do |attr_name, type|
|
||||
define_timeliness_write_method(attr_name, type, timeliness_attribute_timezone_aware?(attr_name))
|
||||
timeliness_validated_attributes.each do |attr_name|
|
||||
define_timeliness_write_method(attr_name)
|
||||
define_timeliness_before_type_cast_method(attr_name) if before_type_cast
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def define_timeliness_write_method(attr_name)
|
||||
type = timeliness_attribute_type(attr_name)
|
||||
timezone_aware = timeliness_attribute_timezone_aware?(attr_name)
|
||||
|
||||
def define_timeliness_write_method(attr_name, type, timezone_aware)
|
||||
method_body, line = <<-EOV, __LINE__ + 1
|
||||
def #{attr_name}=(value)
|
||||
@attributes_cache ||= {}
|
||||
@@ -35,12 +38,16 @@ module ValidatesTimeliness
|
||||
class_eval(method_body, __FILE__, line)
|
||||
end
|
||||
|
||||
public
|
||||
|
||||
# Override in ORM shim
|
||||
def timeliness_attribute_timezone_aware?(attr_name)
|
||||
false
|
||||
end
|
||||
|
||||
# Override in ORM shim
|
||||
def timeliness_attribute_type(attr_name)
|
||||
:datetime
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
||||
@@ -5,8 +5,8 @@ module ValidatesTimeliness
|
||||
included do
|
||||
include ValidationMethods
|
||||
extend ValidationMethods
|
||||
class_inheritable_hash :timeliness_validated_attributes
|
||||
self.timeliness_validated_attributes = {}
|
||||
class_inheritable_accessor :timeliness_validated_attributes
|
||||
self.timeliness_validated_attributes = []
|
||||
end
|
||||
|
||||
module ValidationMethods
|
||||
@@ -23,14 +23,9 @@ module ValidatesTimeliness
|
||||
end
|
||||
|
||||
def timeliness_validation_for(attr_names, type)
|
||||
options = _merge_attributes(attr_names)
|
||||
options[:type] = type
|
||||
attributes = attr_names.inject({}) {|validated, attr_name|
|
||||
attr_name = attr_name.to_s
|
||||
validated[attr_name] = type
|
||||
validated
|
||||
}
|
||||
self.timeliness_validated_attributes = attributes
|
||||
options = _merge_attributes(attr_names).merge(:type => type)
|
||||
self.timeliness_validated_attributes ||= []
|
||||
self.timeliness_validated_attributes += (attr_names - self.timeliness_validated_attributes)
|
||||
validates_with Validator, options
|
||||
end
|
||||
|
||||
|
||||
@@ -2,13 +2,19 @@ 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
|
||||
class << self
|
||||
def 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])
|
||||
def timeliness_attribute_timezone_aware?(attr_name)
|
||||
create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
|
||||
end
|
||||
|
||||
def timeliness_attribute_type(attr_name)
|
||||
columns_hash[attr_name.to_s].type
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,12 +7,17 @@ module ValidatesTimeliness
|
||||
# field value in Mongoid. Parser will return nil rather than error.
|
||||
|
||||
module ClassMethods
|
||||
# Mongoid has no bulk attribute method definition hook. It defines
|
||||
# them with each field definition. So we likewise define them after
|
||||
# each validation is defined.
|
||||
#
|
||||
def timeliness_validation_for(attr_names, type)
|
||||
super
|
||||
attr_names.each { |attr_name| define_timeliness_write_method(attr_name, type, false) }
|
||||
attr_names.each { |attr_name| define_timeliness_write_method(attr_name) }
|
||||
end
|
||||
|
||||
def define_timeliness_write_method(attr_name, type, timezone_aware)
|
||||
def define_timeliness_write_method(attr_name)
|
||||
type = timeliness_attribute_type(attr_name)
|
||||
method_body, line = <<-EOV, __LINE__ + 1
|
||||
def #{attr_name}=(value)
|
||||
@attributes_cache ||= {}
|
||||
@@ -23,7 +28,16 @@ module ValidatesTimeliness
|
||||
EOV
|
||||
class_eval(method_body, __FILE__, line)
|
||||
end
|
||||
|
||||
def timeliness_attribute_type(attr_name)
|
||||
{
|
||||
Date => :date,
|
||||
Time => :datetime,
|
||||
DateTime => :datetime
|
||||
}[fields[attr_name.to_s].type] || :datetime
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user