mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
added define_write_method with simple parse and cace for dates, times and datetimes when time zone conversion is off and default for Rails 2.0
This commit is contained in:
parent
7500781887
commit
5c03d3d645
@ -31,21 +31,16 @@ module ValidatesTimeliness
|
|||||||
base.class_eval do
|
base.class_eval do
|
||||||
class << self
|
class << self
|
||||||
def create_time_zone_conversion_attribute?(name, column)
|
def create_time_zone_conversion_attribute?(name, column)
|
||||||
column.klass == Time
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Handles timezone shift if Rails 2.1
|
|
||||||
def time_in_time_zone(time)
|
|
||||||
time.respond_to?(:in_time_zone) ? time.in_time_zone : time
|
|
||||||
end
|
|
||||||
|
|
||||||
# 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
|
||||||
# from being type cast using default Rails type casting when writing values
|
# being type cast using default Rails type casting when writing values
|
||||||
# to the database.
|
# 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
|
||||||
@ -53,7 +48,7 @@ module ValidatesTimeliness
|
|||||||
if column = column_for_attribute(attr_name)
|
if column = column_for_attribute(attr_name)
|
||||||
if unserializable_attribute?(attr_name, column)
|
if unserializable_attribute?(attr_name, column)
|
||||||
unserialize_attribute(attr_name)
|
unserialize_attribute(attr_name)
|
||||||
elsif column.klass == Time && @attributes_cache.has_key?(attr_name)
|
elsif [:date, :time, :datetime].include?(column.type) && @attributes_cache.has_key?(attr_name)
|
||||||
@attributes_cache[attr_name]
|
@attributes_cache[attr_name]
|
||||||
else
|
else
|
||||||
column.type_cast(value)
|
column.type_cast(value)
|
||||||
@ -86,8 +81,8 @@ module ValidatesTimeliness
|
|||||||
unless instance_method_already_implemented?("#{name}=")
|
unless instance_method_already_implemented?("#{name}=")
|
||||||
if create_time_zone_conversion_attribute?(name, column)
|
if create_time_zone_conversion_attribute?(name, column)
|
||||||
define_write_method_for_time_zone_conversion(name.to_sym)
|
define_write_method_for_time_zone_conversion(name.to_sym)
|
||||||
elsif column.klass == Date
|
elsif [:date, :time, :datetime].include?(column.type)
|
||||||
define_write_method_for_date(name.to_sym)
|
define_write_method_for_dates_and_times(name.to_sym, column.type)
|
||||||
else
|
else
|
||||||
define_write_method(name.to_sym)
|
define_write_method(name.to_sym)
|
||||||
end
|
end
|
||||||
@ -114,8 +109,8 @@ module ValidatesTimeliness
|
|||||||
unless time.acts_like?(:time)
|
unless time.acts_like?(:time)
|
||||||
time = self.class.parse_date_time(time, :datetime)
|
time = self.class.parse_date_time(time, :datetime)
|
||||||
end
|
end
|
||||||
time = time_in_time_zone(time)
|
time = time.in_time_zone rescue nil
|
||||||
if defined?(ActiveRecord::Dirty) && !changed_attributes.include?('#{attr_name}') && old != time
|
if !changed_attributes.include?('#{attr_name}') && old != time
|
||||||
changed_attributes['#{attr_name}'] = (old.duplicable? ? old.clone : old)
|
changed_attributes['#{attr_name}'] = (old.duplicable? ? old.clone : old)
|
||||||
end
|
end
|
||||||
@attributes_cache['#{attr_name}'] = time
|
@attributes_cache['#{attr_name}'] = time
|
||||||
@ -138,19 +133,21 @@ module ValidatesTimeliness
|
|||||||
time = self.class.parse_date_time(date, :datetime)
|
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 rescue nil
|
||||||
end
|
end
|
||||||
@attributes_cache['#{attr_name}'] = time_in_time_zone(time)
|
@attributes_cache['#{attr_name}'] = time.in_time_zone rescue nil
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
evaluate_attribute_method attr_name, method_body
|
evaluate_attribute_method attr_name, method_body
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_write_method_for_date(attr_name)
|
# Define write for date and time columns or when time zone conversion is
|
||||||
|
# off. This is the default for Rails 2.0
|
||||||
|
def define_write_method_for_dates_and_times(attr_name, type)
|
||||||
method_body = <<-EOV
|
method_body = <<-EOV
|
||||||
def #{attr_name}=(date)
|
def #{attr_name}=(value)
|
||||||
@attributes_cache['#{attr_name}'] ||= self.class.parse_date_time(date, :date)
|
@attributes_cache['#{attr_name}'] ||= self.class.parse_date_time(value, :#{type})
|
||||||
@attributes['#{attr_name}'] = date
|
@attributes['#{attr_name}'] = value
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
evaluate_attribute_method attr_name, method_body
|
evaluate_attribute_method attr_name, method_body
|
||||||
|
|||||||
@ -8,7 +8,7 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
@person = Person.new
|
@person = Person.new
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should call parser on write for time attribute" do
|
it "should call parser on write for datetime attribute" do
|
||||||
@person.class.should_receive(:parse_date_time).once
|
@person.class.should_receive(:parse_date_time).once
|
||||||
@person.birth_date_and_time = "2000-06-01 02:03:04"
|
@person.birth_date_and_time = "2000-06-01 02:03:04"
|
||||||
end
|
end
|
||||||
@ -18,6 +18,11 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
@person.birth_date = "2000-06-01"
|
@person.birth_date = "2000-06-01"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should call parser on write for time attribute" do
|
||||||
|
@person.class.should_receive(:parse_date_time).once
|
||||||
|
@person.birth_time = "12:00"
|
||||||
|
end
|
||||||
|
|
||||||
it "should return raw string value for attribute_before_type_cast when written as string" do
|
it "should return raw string value for attribute_before_type_cast when written as string" do
|
||||||
time_string = "2000-06-01 02:03:04"
|
time_string = "2000-06-01 02:03:04"
|
||||||
@person.birth_date_and_time = time_string
|
@person.birth_date_and_time = time_string
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user