Move timeliness defined methods to a module so they can be overwritten.

This commit is contained in:
José Valim 2010-11-01 14:56:03 -02:00
parent 0e3f56e26d
commit 889b5a9b07
3 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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|

View File

@ -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