diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index 40b594a..5264fa6 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -42,7 +42,7 @@ module ValidatesTimeliness super end EOV - class_eval(method_body, __FILE__, line) + generated_timeliness_methods.module_eval(method_body, __FILE__, line) end def define_timeliness_before_type_cast_method(attr_name) @@ -51,7 +51,11 @@ module ValidatesTimeliness _timeliness_raw_value_for('#{attr_name}') end EOV - class_eval(method_body, __FILE__, line) + generated_timeliness_methods.module_eval(method_body, __FILE__, line) + end + + def generated_timeliness_methods + @generated_timeliness_methods ||= Module.new.tap { |m| include(m) } end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 56aac25..1092871 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -75,6 +75,13 @@ class Employee < ActiveRecord::Base validates_time :birth_time validates_datetime :birth_datetime define_attribute_methods + + attr_accessor :redefined_birth_date_called + + def birth_date=(value) + self.redefined_birth_date_called = true + super + end end Rspec.configure do |c| diff --git a/spec/validates_timeliness/attribute_methods_spec.rb b/spec/validates_timeliness/attribute_methods_spec.rb index bf246aa..c4e6c6e 100644 --- a/spec/validates_timeliness/attribute_methods_spec.rb +++ b/spec/validates_timeliness/attribute_methods_spec.rb @@ -34,6 +34,12 @@ describe ValidatesTimeliness::AttributeMethods do r._timeliness_raw_value_for(:birth_datetime).should == date_string end + it 'should not overwrite user defined methods' do + e = Employee.new + e.birth_date = '2010-01-01' + e.redefined_birth_date_called.should be_true + end + context "with plugin parser" do class PersonWithParser include TestModel