diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index cba1fb0..c8f75db 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -46,7 +46,7 @@ module ValidatesTimeliness def define_timeliness_before_type_cast_method(attr_name) method_body, line = <<-EOV, __LINE__ + 1 def #{attr_name}_before_type_cast - _timeliness_raw_value_for('#{attr_name}') + _timeliness_raw_value_for('#{attr_name}') || @attributes['#{attr_name}'] end EOV generated_timeliness_methods.module_eval(method_body, __FILE__, line) diff --git a/lib/validates_timeliness/orm/active_record.rb b/lib/validates_timeliness/orm/active_record.rb index 8309fb5..5205824 100644 --- a/lib/validates_timeliness/orm/active_record.rb +++ b/lib/validates_timeliness/orm/active_record.rb @@ -7,7 +7,8 @@ module ValidatesTimeliness def define_attribute_methods super # Define write method and before_type_cast method - define_timeliness_methods(true) + use_before_type_cast = ::ActiveRecord::VERSION::STRING < '3.1.0' + define_timeliness_methods(use_before_type_cast) end def timeliness_attribute_timezone_aware?(attr_name) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8cc22f3..73835a3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/validates_timeliness/orm/active_record_spec.rb b/spec/validates_timeliness/orm/active_record_spec.rb index 19ff24b..af022d4 100644 --- a/spec/validates_timeliness/orm/active_record_spec.rb +++ b/spec/validates_timeliness/orm/active_record_spec.rb @@ -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