mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
moved attribute methods modules into Old and New for rails versions
This commit is contained in:
parent
9a8a82c699
commit
05a2d53b9a
@ -16,90 +16,92 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
if ActiveRecord::VERSION::STRING < '2.1'
|
if ActiveRecord::VERSION::STRING < '2.1'
|
||||||
base.extend ClassMethodsOld
|
base.extend ClassMethods::Old
|
||||||
else
|
else
|
||||||
base.extend ClassMethodsNew
|
base.extend ClassMethods::New
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# ActiveRecord >= 2.1
|
module ClassMethods
|
||||||
module ClassMethodsNew
|
# ActiveRecord >= 2.1
|
||||||
def define_read_method_for_time_zone_conversion(attr_name)
|
module New
|
||||||
method_body = <<-EOV
|
def define_read_method_for_time_zone_conversion(attr_name)
|
||||||
|
method_body = <<-EOV
|
||||||
|
def #{attr_name}(reload = false)
|
||||||
|
cached = @attributes_cache['#{attr_name}']
|
||||||
|
return cached if cached && !reload
|
||||||
|
time = read_attribute_before_type_cast('#{attr_name}')
|
||||||
|
if time.acts_like?(:time)
|
||||||
|
time = time.in_time_zone
|
||||||
|
elsif time
|
||||||
|
# check invalid date
|
||||||
|
time.to_date rescue time = nil
|
||||||
|
time = time.in_time_zone if time
|
||||||
|
end
|
||||||
|
@attributes_cache['#{attr_name}'] = time
|
||||||
|
end
|
||||||
|
EOV
|
||||||
|
evaluate_attribute_method attr_name, method_body
|
||||||
|
end
|
||||||
|
|
||||||
|
def define_write_method_for_time_zone_conversion(attr_name)
|
||||||
|
method_body = <<-EOV
|
||||||
|
def #{attr_name}=(time)
|
||||||
|
if time.acts_like?(:time)
|
||||||
|
time = time.in_time_zone rescue time
|
||||||
|
end
|
||||||
|
write_attribute(:#{attr_name}, time)
|
||||||
|
end
|
||||||
|
EOV
|
||||||
|
evaluate_attribute_method attr_name, method_body, "#{attr_name}="
|
||||||
|
end
|
||||||
|
end # New
|
||||||
|
|
||||||
|
# ActiveRecord < 2.1
|
||||||
|
module Old
|
||||||
|
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)
|
||||||
|
else
|
||||||
|
if column.klass == Time
|
||||||
|
define_read_method_for_time_attribute(name.to_sym)
|
||||||
|
else
|
||||||
|
define_read_method(name.to_sym, name, column)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
unless instance_method_already_implemented?("#{name}=")
|
||||||
|
define_write_method(name.to_sym)
|
||||||
|
end
|
||||||
|
|
||||||
|
unless instance_method_already_implemented?("#{name}?")
|
||||||
|
define_question_method(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def define_read_method_for_time_attribute(attr_name)
|
||||||
|
method_body = <<-EOV
|
||||||
def #{attr_name}(reload = false)
|
def #{attr_name}(reload = false)
|
||||||
cached = @attributes_cache['#{attr_name}']
|
cached = @attributes_cache['#{attr_name}']
|
||||||
return cached if cached && !reload
|
return cached if cached && !reload
|
||||||
time = read_attribute_before_type_cast('#{attr_name}')
|
time = read_attribute_before_type_cast('#{attr_name}')
|
||||||
if time.acts_like?(:time)
|
unless time.acts_like?(:time)
|
||||||
time = time.in_time_zone
|
|
||||||
elsif time
|
|
||||||
# check invalid date
|
# check invalid date
|
||||||
time.to_date rescue time = nil
|
time.to_date rescue time = nil
|
||||||
time = time.in_time_zone if time
|
time = time.to_time if time
|
||||||
end
|
end
|
||||||
@attributes_cache['#{attr_name}'] = time
|
@attributes_cache['#{attr_name}'] = time
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
evaluate_attribute_method attr_name, method_body
|
evaluate_attribute_method attr_name, method_body
|
||||||
end
|
|
||||||
|
|
||||||
def define_write_method_for_time_zone_conversion(attr_name)
|
|
||||||
method_body = <<-EOV
|
|
||||||
def #{attr_name}=(time)
|
|
||||||
if time.acts_like?(:time)
|
|
||||||
time = time.in_time_zone rescue time
|
|
||||||
end
|
|
||||||
write_attribute(:#{attr_name}, time)
|
|
||||||
end
|
|
||||||
EOV
|
|
||||||
evaluate_attribute_method attr_name, method_body, "#{attr_name}="
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# ActiveRecord < 2.1
|
|
||||||
module ClassMethodsOld
|
|
||||||
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)
|
|
||||||
else
|
|
||||||
if column.klass == Time
|
|
||||||
define_read_method_for_time_attribute(name.to_sym)
|
|
||||||
else
|
|
||||||
define_read_method(name.to_sym, name, column)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
unless instance_method_already_implemented?("#{name}=")
|
|
||||||
define_write_method(name.to_sym)
|
|
||||||
end
|
|
||||||
|
|
||||||
unless instance_method_already_implemented?("#{name}?")
|
|
||||||
define_question_method(name)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end # Old
|
||||||
|
|
||||||
def define_read_method_for_time_attribute(attr_name)
|
|
||||||
method_body = <<-EOV
|
|
||||||
def #{attr_name}(reload = false)
|
|
||||||
cached = @attributes_cache['#{attr_name}']
|
|
||||||
return cached if cached && !reload
|
|
||||||
time = read_attribute_before_type_cast('#{attr_name}')
|
|
||||||
unless time.acts_like?(:time)
|
|
||||||
# check invalid date
|
|
||||||
time.to_date rescue time = nil
|
|
||||||
time = time.to_time if time
|
|
||||||
end
|
|
||||||
@attributes_cache['#{attr_name}'] = time
|
|
||||||
end
|
|
||||||
EOV
|
|
||||||
evaluate_attribute_method attr_name, method_body
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
end # ClassMethods
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user