mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
fix bug preventing custom class methods on date/times
This commit is contained in:
parent
e9eb812c9e
commit
b356363791
@ -24,7 +24,6 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
def write_date_time_attribute(attr_name, value, type, time_zone_aware)
|
def write_date_time_attribute(attr_name, value, type, time_zone_aware)
|
||||||
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
@attributes_cache["_#{attr_name}_before_type_cast"] = value
|
||||||
|
|
||||||
value = ValidatesTimeliness::Parser.parse(value, type)
|
value = ValidatesTimeliness::Parser.parse(value, type)
|
||||||
|
|
||||||
if value && type != :date
|
if value && type != :date
|
||||||
@ -49,16 +48,22 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
columns_hash.each do |name, column|
|
columns_hash.each do |name, column|
|
||||||
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
|
|
||||||
|
|
||||||
method_name = "#{name}="
|
method_name = "#{name}="
|
||||||
|
next if instance_method_already_implemented?(method_name)
|
||||||
|
|
||||||
|
time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false
|
||||||
define_method(method_name) do |value|
|
define_method(method_name) do |value|
|
||||||
write_date_time_attribute(name, value, column.type, time_zone_aware)
|
write_date_time_attribute(name, value, column.type, time_zone_aware)
|
||||||
end
|
end
|
||||||
timeliness_methods << method_name
|
timeliness_methods << method_name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Hack to get around instance_method_already_implemented? caching the
|
||||||
|
# methods in the ivar. It then appears to subsequent calls that the
|
||||||
|
# methods defined here, have not been and get defined again.
|
||||||
|
@_defined_class_methods = nil
|
||||||
|
|
||||||
define_attribute_methods_without_timeliness
|
define_attribute_methods_without_timeliness
|
||||||
# add generated methods which is a Set object hence no += method
|
# add generated methods which is a Set object hence no += method
|
||||||
timeliness_methods.each {|attr| generated_methods << attr }
|
timeliness_methods.each {|attr| generated_methods << attr }
|
||||||
|
|||||||
@ -137,4 +137,21 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do
|
|||||||
person.birth_date.should == tomorrow
|
person.birth_date.should == tomorrow
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "attribute writer" do
|
||||||
|
|
||||||
|
it "should be able to be overridden in class" do
|
||||||
|
Person.class_eval { attr_accessor :birth_date }
|
||||||
|
person = Person.new
|
||||||
|
person.birth_date = 'overriden'
|
||||||
|
person.birth_date.should == 'overriden'
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
Person.class_eval do
|
||||||
|
undef_method(:birth_date)
|
||||||
|
undef_method(:birth_date=)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user