behaves as better AR citizen by alias_method_chaining define_attribute_methods and read_attribute instead of overriding

This commit is contained in:
Adam Meehan 2009-03-03 12:38:54 +11:00
parent 19457a6c1d
commit 7ca84f3116

View File

@ -18,25 +18,26 @@ module ValidatesTimeliness
def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
base.class_eval do
alias_method_chain :read_attribute, :timeliness
class << self
alias_method_chain :define_attribute_methods, :timeliness
end
end
end end
# Adds check for cached date/time attributes which have been type cast already # Adds check for cached date/time attributes which have been type cast already
# and value can be used from cache. This prevents the raw date/time value from # and value can be used from cache. This prevents the raw date/time value 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_with_timeliness(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?
if column = column_for_attribute(attr_name) column = column_for_attribute(attr_name)
if unserializable_attribute?(attr_name, column) if column && [:date, :time, :datetime].include?(column.type) && @attributes_cache.has_key?(attr_name)
unserialize_attribute(attr_name) @attributes_cache[attr_name]
elsif [:date, :time, :datetime].include?(column.type) && @attributes_cache.has_key?(attr_name)
@attributes_cache[attr_name]
else
column.type_cast(value)
end
else else
value read_attribute_without_timeliness(attr_name)
end end
else else
nil nil
@ -84,19 +85,15 @@ module ValidatesTimeliness
module ClassMethods module ClassMethods
# Override AR method to define attribute reader and writer method for # Define attribute reader and writer method for date, time and
# date, time and datetime attributes to use plugin parser. # datetime attributes to use plugin parser.
def define_attribute_methods def define_attribute_methods_with_timeliness
return if generated_methods? return if generated_methods?
columns_hash.each do |name, column| columns_hash.each do |name, column|
unless instance_method_already_implemented?(name) unless instance_method_already_implemented?(name)
if self.serialized_attributes[name] if [:date, :time, :datetime].include?(column.type)
define_read_method_for_serialized_attribute(name)
elsif [:date, :time, :datetime].include?(column.type)
time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false
define_read_method_for_dates_and_times(name, column.type, time_zone_aware) define_read_method_for_dates_and_times(name, column.type, time_zone_aware)
else
define_read_method(name.to_sym, name, column)
end end
end end
@ -104,15 +101,10 @@ module ValidatesTimeliness
if [:date, :time, :datetime].include?(column.type) if [:date, :time, :datetime].include?(column.type)
time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false
define_write_method_for_dates_and_times(name, column.type, time_zone_aware) define_write_method_for_dates_and_times(name, column.type, time_zone_aware)
else
define_write_method(name.to_sym)
end end
end end
unless instance_method_already_implemented?("#{name}?")
define_question_method(name)
end
end end
define_attribute_methods_without_timeliness
end end
# Define write method for date, time and datetime columns # Define write method for date, time and datetime columns