Fix before_type_cast for non-dirty attributes (issue #52).

Only use before_type_cast implementation if earlier than 3.1.0 which has
it's own working version for date/time fields now.
This commit is contained in:
Adam Meehan
2011-09-21 21:28:53 +10:00
parent 34824bbbab
commit 86b7bc4829
4 changed files with 16 additions and 7 deletions

View File

@@ -71,9 +71,10 @@ ActiveRecord::Schema.define(:version => 1) do
end
class Employee < ActiveRecord::Base
define_attribute_methods
attr_accessor :redefined_birth_date_called
validates_date :birth_date, :allow_nil => true
validates_date :birth_time, :allow_nil => true
validates_date :birth_datetime, :allow_nil => true
def birth_date=(value)
self.redefined_birth_date_called = true
@@ -90,8 +91,5 @@ RSpec.configure do |c|
Person.reset_callbacks(:validate)
PersonWithShim.timeliness_validated_attributes = []
Person._validators.clear
Employee.reset_callbacks(:validate)
Employee.timeliness_validated_attributes = []
Employee._validators.clear
end
end

View File

@@ -82,6 +82,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
r = Employee.create!
r.birth_date = '2010-01-01'
r.reload
r._timeliness_raw_value_for(:birth_date).should be_nil
end
end
@@ -94,7 +95,16 @@ describe ValidatesTimeliness, 'ActiveRecord' do
it 'should return original value' do
r = Employee.new
r.birth_datetime = date_string = '2010-01-01'
r.birth_datetime_before_type_cast.should == date_string
end
it 'should return attribute if no attribute assignment has been made' do
datetime = Time.zone.local(2010,01,01)
Employee.create(:birth_datetime => datetime)
r = Employee.last
r.birth_datetime_before_type_cast.should match(/2010-01-01 00:00:00/)
end
end
end