Merge branch 'date_convert'

This commit is contained in:
Adam Meehan 2008-07-22 11:51:04 +10:00
commit 2464540264
4 changed files with 167 additions and 125 deletions

View File

@ -11,32 +11,21 @@ module ValidatesTimeliness
# To do this we must cache the user entered values on write and store the raw # To do this we must cache the user entered values on write and store the raw
# value in the attributes hash for later retrieval and possibly validation. # value in the attributes hash for later retrieval and possibly validation.
# Any value from the database will not be in the attribute cache on first # Any value from the database will not be in the attribute cache on first
# read so will be considered in UTC time and then converted to local time. # read so will be considered in default timezone and converted to local time.
# It is then stored back in the attributes hash and cached to avoid the need # It is then stored back in the attributes hash and cached to avoid the need
# for any subsequent differentiation. # for any subsequent differentiation.
# #
# The wholesale replacement of the Rails time type casting is not done to # The wholesale replacement of the Rails time type casting is not done to
# preserve the quick conversion for timestamp columns and also any value which # preserve the quickest conversion for timestamp columns and also any value
# is never touched during the life of the record object. # which is never changed during the life of the record object.
#
# Dates are also handled but only write to cache value converted by plugin
# parser. Default read method will retrieve from cache or do default
# conversion
module AttributeMethods module AttributeMethods
def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
if Rails::VERSION::STRING < '2.1'
base.class_eval do
class << self
alias_method :define_read_method_for_time, :define_read_method_for_time_zone_conversion
alias_method :define_write_method_for_time, :define_write_method_for_time_zone_conversion
end
end
base.extend ClassMethodsOld
end
end
# Does strict time type cast checking for Rails 2.1 timezone handling
def strict_time_type_cast(time)
time = self.class.parse_date_time(time, :datetime)
time_in_time_zone(time)
end end
# Handles timezone shift if Rails 2.1 # Handles timezone shift if Rails 2.1
@ -46,7 +35,8 @@ module ValidatesTimeliness
# Adds check for cached time attributes which have been type cast already # Adds check for cached time attributes which have been type cast already
# and value can be used from cache. This prevents the raw time value # and value can be used from cache. This prevents the raw time value
# from being type cast using default Rails type casting. # from being type cast using default Rails type casting when writing values
# to the database.
def read_attribute(attr_name) def read_attribute(attr_name)
attr_name = attr_name.to_s attr_name = attr_name.to_s
if !(value = @attributes[attr_name]).nil? if !(value = @attributes[attr_name]).nil?
@ -68,8 +58,39 @@ module ValidatesTimeliness
module ClassMethods module ClassMethods
# Define time attribute write method to store time value as is without # Modified from AR to define Date and Time attribute reader and writer
# conversion and then convert time with strict conversion and cache it. # methods with strict time type casting.
def define_attribute_methods
return if generated_methods?
columns_hash.each do |name, column|
unless instance_method_already_implemented?(name)
if self.serialized_attributes[name]
define_read_method_for_serialized_attribute(name)
elsif column.klass == Time
define_read_method_for_time_zone_conversion(name.to_sym)
else
define_read_method(name.to_sym, name, column)
end
end
unless instance_method_already_implemented?("#{name}=")
if column.klass == Time
define_write_method_for_time_zone_conversion(name.to_sym)
elsif column.klass == Date
define_write_method_for_date(name.to_sym)
else
define_write_method(name.to_sym)
end
end
unless instance_method_already_implemented?("#{name}?")
define_question_method(name)
end
end
end
# Define time attribute write method to store raw time value in
# attributes hash, then convert it with parser and cache it.
# #
# If Rails 2.1 dirty attributes is enabled then the value is added to # If Rails 2.1 dirty attributes is enabled then the value is added to
# changed attributes if changed. Can't use the default dirty checking # changed attributes if changed. Can't use the default dirty checking
@ -81,7 +102,7 @@ module ValidatesTimeliness
old = read_attribute('#{attr_name}') if defined?(ActiveRecord::Dirty) old = read_attribute('#{attr_name}') if defined?(ActiveRecord::Dirty)
@attributes['#{attr_name}'] = time @attributes['#{attr_name}'] = time
unless time.acts_like?(:time) unless time.acts_like?(:time)
time = strict_time_type_cast(time) time = self.class.parse_date_time(time, :datetime)
end end
time = time_in_time_zone(time) time = time_in_time_zone(time)
if defined?(ActiveRecord::Dirty) && !changed_attributes.include?('#{attr_name}') && old != time if defined?(ActiveRecord::Dirty) && !changed_attributes.include?('#{attr_name}') && old != time
@ -94,7 +115,7 @@ module ValidatesTimeliness
end end
# Define time attribute reader. If reloading then check if cached, # Define time attribute reader. If reloading then check if cached,
# which means its in local time. If local, do strict type cast as local # which means its in local time. If local, convert with parser as local
# timezone, otherwise use read_attribute method for quick default type # timezone, otherwise use read_attribute method for quick default type
# cast of values from database using default timezone. # cast of values from database using default timezone.
def define_read_method_for_time_zone_conversion(attr_name) def define_read_method_for_time_zone_conversion(attr_name)
@ -104,7 +125,7 @@ module ValidatesTimeliness
return cached if @attributes_cache.has_key?('#{attr_name}') && !reload return cached if @attributes_cache.has_key?('#{attr_name}') && !reload
if @attributes_cache.has_key?('#{attr_name}') if @attributes_cache.has_key?('#{attr_name}')
time = read_attribute_before_type_cast('#{attr_name}') time = read_attribute_before_type_cast('#{attr_name}')
time = strict_time_type_cast(time) time = self.class.parse_date_time(date, :datetime)
else else
time = read_attribute('#{attr_name}') time = read_attribute('#{attr_name}')
@attributes['#{attr_name}'] = time_in_time_zone(time) @attributes['#{attr_name}'] = time_in_time_zone(time)
@ -115,40 +136,16 @@ module ValidatesTimeliness
evaluate_attribute_method attr_name, method_body evaluate_attribute_method attr_name, method_body
end end
def define_write_method_for_date(attr_name)
method_body = <<-EOV
def #{attr_name}=(date)
@attributes_cache['#{attr_name}'] ||= self.class.parse_date_time(date, :date)
@attributes['#{attr_name}'] = date
end
EOV
evaluate_attribute_method attr_name, method_body
end end
# Only for Rails 2.0.x. Checks for time attributes to define special reader
# and writer methods.
module ClassMethodsOld
# Modified from AR to define Time attribute reader and writer methods with
# strict time type casting.
def define_attribute_methods
return if generated_methods?
columns_hash.each do |name, column|
unless instance_method_already_implemented?(name)
if self.serialized_attributes[name]
define_read_method_for_serialized_attribute(name)
elsif column.klass == Time
define_read_method_for_time(name.to_sym)
else
define_read_method(name.to_sym, name, column)
end
end
unless instance_method_already_implemented?("#{name}=")
if column.klass == Time
define_write_method_for_time(name.to_sym)
else
define_write_method(name.to_sym)
end
end
unless instance_method_already_implemented?("#{name}?")
define_question_method(name)
end
end
end
end end
end end

View File

@ -25,23 +25,20 @@ module ValidatesTimeliness
# Override this method to use any date parsing algorithm you like such as # Override this method to use any date parsing algorithm you like such as
# Chronic. Just return nil for an invalid value and a Time object for a # Chronic. Just return nil for an invalid value and a Time object for a
# valid parsed value. # valid parsed value.
#
def timeliness_date_time_parse(raw_value, type, strict=true) def timeliness_date_time_parse(raw_value, type, strict=true)
return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date) return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
time_array = ValidatesTimeliness::Formats.parse(raw_value, type, strict) time_array = ValidatesTimeliness::Formats.parse(raw_value, type, strict)
raise if time_array.nil? raise if time_array.nil?
if type == :time
# Rails dummy time date part is defined as 2000-01-01 # Rails dummy time date part is defined as 2000-01-01
time_array[0..2] = 2000, 1, 1 time_array[0..2] = 2000, 1, 1 if type == :time
end
# Date.new enforces days per month, unlike Time # Date.new enforces days per month, unlike Time
Date.new(*time_array[0..2]) unless type == :time Date.new(*time_array[0..2]) unless type == :time
# Check time part, and return time object # Create time object which checks time part, and return time object
Time.local(*time_array) rescue DateTime.new(*time_array[0..5]) make_time(time_array)
rescue rescue
nil nil
end end
@ -107,7 +104,7 @@ module ValidatesTimeliness
private private
# Validate value against the temoSpral restrictions. Restriction values # Validate value against the temopral restrictions. Restriction values
# maybe of mixed type, so the are evaluated as a common type, which may # maybe of mixed type, so the are evaluated as a common type, which may
# require conversion. The type used is defined by validation type. # require conversion. The type used is defined by validation type.
def validate_timeliness_restrictions(record, attr_name, value, configuration) def validate_timeliness_restrictions(record, attr_name, value, configuration)
@ -153,6 +150,22 @@ module ValidatesTimeliness
end end
end end
# Create time in correct timezone. For Rails 2.1 that is value in
# Time.zone. Rails 2.0 should be default_timezone.
def make_time(time_array)
if Time.respond_to?(:zone)
Time.zone.local(*time_array)
else
begin
Time.send(ActiveRecord::Base.default_timezone, *time_array)
rescue ArgumentError, TypeError
zone_offset = ActiveRecord::Base.default_timezone == :local ? DateTime.local_offset : 0
time_array.pop # remove microseconds
DateTime.civil(*(time_array << zone_offset))
end
end
end
end end
end end
end end

View File

@ -8,55 +8,39 @@ describe ValidatesTimeliness::AttributeMethods do
@person = Person.new @person = Person.new
end end
describe "strict_time_type_cast" do it "should call parser on write for time attribute" do
it "should return time object for valid time string" do @person.class.should_receive(:parse_date_time).once
strict_time_type_cast("2000-01-01 12:13:14").should be_kind_of(Time) @person.birth_date_and_time = "2000-06-01 02:03:04"
end end
it "should return nil for time string with invalid date part" do it "should call parser on write for date attribute" do
strict_time_type_cast("2000-02-30 12:13:14").should be_nil @person.class.should_receive(:parse_date_time).once
@person.birth_date = "2000-06-01"
end end
it "should return nil for time string with invalid time part" do it "should return raw string value for attribute_before_type_cast when written as string" do
strict_time_type_cast("2000-02-01 25:13:14").should be_nil time_string = "2000-06-01 02:03:04"
end
it "should return Time object when passed a Time object" do
strict_time_type_cast(Time.now).should be_kind_of(Time)
end
if RAILS_VER >= '2.1'
it "should convert time string into current timezone" do
Time.zone = 'Melbourne'
time = strict_time_type_cast("2000-01-01 12:13:14")
Time.zone.utc_offset.should == 10.hours
end
end
end
it "should return string value for attribute_before_type_cast when written as string" do
time_string = "2000-06-01 01:02:03"
@person.birth_date_and_time = time_string @person.birth_date_and_time = time_string
@person.birth_date_and_time_before_type_cast.should == time_string @person.birth_date_and_time_before_type_cast.should == time_string
end end
it "should return Time object for attribute_before_type_cast when written as Time" do it "should return Time object for attribute_before_type_cast when written as Time" do
@person.birth_date_and_time = Time.mktime(2000, 06, 01, 1, 2, 3) @person.birth_date_and_time = Time.mktime(2000, 6, 1, 2, 3, 4)
@person.birth_date_and_time_before_type_cast.should be_kind_of(Time) @person.birth_date_and_time_before_type_cast.should be_kind_of(Time)
end end
it "should return Time object using attribute read method when written with string" do it "should return Time object using attribute read method when written with string" do
@person.birth_date_and_time = "2000-06-01 01:02:03" @person.birth_date_and_time = "2000-06-01 02:03:04"
@person.birth_date_and_time.should be_kind_of(Time) @person.birth_date_and_time.should be_kind_of(Time)
end end
it "should return nil when time is invalid" do it "should return nil when time is invalid" do
@person.birth_date_and_time = "2000-02-30 01:02:03" @person.birth_date_and_time = "2000-01-32 02:03:04"
@person.birth_date_and_time.should be_nil @person.birth_date_and_time.should be_nil
end end
it "should not save invalid date value to database" do it "should not save invalid date value to database" do
time_string = "2000-02-30 09:00:00" time_string = "2000-01-32 02:03:04"
@person = Person.new @person = Person.new
@person.birth_date_and_time = time_string @person.birth_date_and_time = time_string
@person.save @person.save
@ -67,7 +51,7 @@ describe ValidatesTimeliness::AttributeMethods do
unless RAILS_VER < '2.1' unless RAILS_VER < '2.1'
it "should return stored time string as Time with correct timezone" do it "should return stored time string as Time with correct timezone" do
Time.zone = 'Melbourne' Time.zone = 'Melbourne'
time_string = "2000-06-01 01:02:03" time_string = "2000-06-01 02:03:04"
@person.birth_date_and_time = time_string @person.birth_date_and_time = time_string
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000' @person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
end end
@ -83,17 +67,30 @@ describe ValidatesTimeliness::AttributeMethods do
end end
it "should return true for attribute changed?" do it "should return true for attribute changed?" do
time_string = "2000-06-01 01:02:03" time_string = "2000-06-01 02:03:04"
@person.birth_date_and_time = time_string @person.birth_date_and_time = time_string
@person.birth_date_and_time_changed?.should be_true @person.birth_date_and_time_changed?.should be_true
end end
it "should show changes for time attribute as nil to Time object" do it "should show changes for time attribute as nil to Time object" do
time_string = "2000-06-01 01:02:03" time_string = "2000-06-01 02:03:04"
@person.birth_date_and_time = time_string @person.birth_date_and_time = time_string
time = @person.birth_date_and_time time = @person.birth_date_and_time
@person.changes.should == {"birth_date_and_time" => [nil, time]} @person.changes.should == {"birth_date_and_time" => [nil, time]}
end end
else
it "should return time object from database in default timezone" do
ActiveRecord::Base.default_timezone = :utc
time_string = "2000-06-01 09:00:00"
@person = Person.new
@person.birth_date_and_time = time_string
@person.save
@person.reload
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z').should == time_string + ' GMT'
end
end end
it "should return same time object on repeat reads" do it "should return same time object on repeat reads" do

View File

@ -3,13 +3,47 @@ require File.dirname(__FILE__) + '/spec_helper'
describe ValidatesTimeliness::Validations do describe ValidatesTimeliness::Validations do
before :all do before :all do
# freezes time using time_travel plugin # freezes time using time_travel plugin
Time.now = Time.utc(2008, 1, 1, 12, 0, 0) Time.now = Time.utc(2000, 1, 1, 0, 0, 0)
end end
after :all do after :all do
Time.now = nil Time.now = nil
end end
describe "timeliness_date_time_parse" do
it "should return time object for valid time string" do
parse_method("2000-01-01 12:13:14", :datetime).should be_kind_of(Time)
end
it "should return nil for time string with invalid date part" do
parse_method("2000-02-30 12:13:14", :datetime).should be_nil
end
it "should return nil for time string with invalid time part" do
parse_method("2000-02-01 25:13:14", :datetime).should be_nil
end
it "should return Time object when passed a Time object" do
parse_method(Time.now, :datetime).should be_kind_of(Time)
end
if RAILS_VER >= '2.1'
it "should convert time string into current timezone" do
Time.zone = 'Melbourne'
time = parse_method("2000-01-01 12:13:14", :datetime)
Time.zone.utc_offset.should == 10.hours
end
end
it "should return nil for invalid date string" do
parse_method("2000-02-30", :date).should be_nil
end
def parse_method(*args)
ActiveRecord::Base.timeliness_date_time_parse(*args)
end
end
describe "with no restrictions" do describe "with no restrictions" do
before :all do before :all do
class BasicValidation < Person class BasicValidation < Person
@ -24,19 +58,19 @@ describe ValidatesTimeliness::Validations do
end end
it "should have error for invalid date component for datetime column" do it "should have error for invalid date component for datetime column" do
@person.birth_date_and_time = "1980-02-30 01:02:03" @person.birth_date_and_time = "2000-02-30 01:02:03"
@person.should_not be_valid @person.should_not be_valid
@person.errors.on(:birth_date_and_time).should == "is not a valid datetime" @person.errors.on(:birth_date_and_time).should == "is not a valid datetime"
end end
it "should have error for invalid time component for datetime column" do it "should have error for invalid time component for datetime column" do
@person.birth_date_and_time = "1980-02-30 25:02:03" @person.birth_date_and_time = "2000-02-30 25:02:03"
@person.should_not be_valid @person.should_not be_valid
@person.errors.on(:birth_date_and_time).should == "is not a valid datetime" @person.errors.on(:birth_date_and_time).should == "is not a valid datetime"
end end
it "should have error for invalid date value for date column" do it "should have error for invalid date value for date column" do
@person.birth_date = "1980-02-30" @person.birth_date = "2000-02-30"
@person.should_not be_valid @person.should_not be_valid
@person.errors.on(:birth_date).should == "is not a valid date" @person.errors.on(:birth_date).should == "is not a valid date"
end end
@ -48,8 +82,8 @@ describe ValidatesTimeliness::Validations do
end end
it "should be valid with valid values" do it "should be valid with valid values" do
@person.birth_date_and_time = "1980-01-31 12:12:12" @person.birth_date_and_time = "2000-01-31 12:12:12"
@person.birth_date = "1980-01-31" @person.birth_date = "2000-01-31"
@person.should be_valid @person.should be_valid
end end
@ -299,12 +333,13 @@ describe ValidatesTimeliness::Validations do
describe "with mixed value and restriction types" do describe "with mixed value and restriction types" do
before :all do before :all do
class MixedBeforeAndAfter < Person class MixedBeforeAndAfter < Person
validates_timeliness_of :birth_date_and_time, validates_timeliness_of :birth_date_and_time,
:before => Date.new(2008,1,2), :before => Date.new(2008,1,2),
:after => lambda { Time.mktime(2008, 1, 1) } :after => lambda { "2008-01-01" }
validates_timeliness_of :birth_date, :type => :date, validates_timeliness_of :birth_date, :type => :date,
:on_or_before => lambda { Time.mktime(2008, 1, 2) }, :on_or_before => lambda { "2008-01-01" },
:on_or_after => :birth_date_and_time :on_or_after => :birth_date_and_time
end end
end end
@ -341,7 +376,7 @@ describe ValidatesTimeliness::Validations do
end end
describe "ignoring rstriction errors" do describe "ignoring restriction errors" do
before :all do before :all do
class BadRestriction < Person class BadRestriction < Person
validates_date :birth_date, :before => Proc.new { raise } validates_date :birth_date, :before => Proc.new { raise }