From 59d9957ab6bb4e53b1d27a57a1f94f98de055096 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 29 Dec 2009 11:13:15 +1100 Subject: [PATCH] fix generated_methods bug where read instead of write method stored existing spec failed unless run alone as attribute methods already primed. fixed spec to undef method and empty generated_methods set --- lib/validates_timeliness/active_record/attribute_methods.rb | 5 +++-- spec/active_record/attribute_methods_spec.rb | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/validates_timeliness/active_record/attribute_methods.rb b/lib/validates_timeliness/active_record/attribute_methods.rb index 7c1747f..6488519 100644 --- a/lib/validates_timeliness/active_record/attribute_methods.rb +++ b/lib/validates_timeliness/active_record/attribute_methods.rb @@ -51,10 +51,11 @@ module ValidatesTimeliness if [:date, :time, :datetime].include?(column.type) time_zone_aware = create_time_zone_conversion_attribute?(name, column) rescue false - define_method("#{name}=") do |value| + method_name = "#{name}=" + define_method(method_name) do |value| write_date_time_attribute(name, value, column.type, time_zone_aware) end - timeliness_methods << name + timeliness_methods << method_name end end diff --git a/spec/active_record/attribute_methods_spec.rb b/spec/active_record/attribute_methods_spec.rb index 0d9bfc0..94df280 100644 --- a/spec/active_record/attribute_methods_spec.rb +++ b/spec/active_record/attribute_methods_spec.rb @@ -5,7 +5,9 @@ describe ValidatesTimeliness::ActiveRecord::AttributeMethods do @person = Person.new end - it "should call write_date_time_attribute when date attribute assigned value" do + it "should define and call write method on first assign" do + Person.class_eval { @generated_methods = Set.new } + Person.send(:undef_method, :birth_date=) @person.should_receive(:write_date_time_attribute) @person.birth_date = "2000-01-01" end