mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
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:
parent
34824bbbab
commit
86b7bc4829
@ -46,7 +46,7 @@ module ValidatesTimeliness
|
|||||||
def define_timeliness_before_type_cast_method(attr_name)
|
def define_timeliness_before_type_cast_method(attr_name)
|
||||||
method_body, line = <<-EOV, __LINE__ + 1
|
method_body, line = <<-EOV, __LINE__ + 1
|
||||||
def #{attr_name}_before_type_cast
|
def #{attr_name}_before_type_cast
|
||||||
_timeliness_raw_value_for('#{attr_name}')
|
_timeliness_raw_value_for('#{attr_name}') || @attributes['#{attr_name}']
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
generated_timeliness_methods.module_eval(method_body, __FILE__, line)
|
||||||
|
|||||||
@ -7,7 +7,8 @@ module ValidatesTimeliness
|
|||||||
def define_attribute_methods
|
def define_attribute_methods
|
||||||
super
|
super
|
||||||
# Define write method and before_type_cast method
|
# 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
|
end
|
||||||
|
|
||||||
def timeliness_attribute_timezone_aware?(attr_name)
|
def timeliness_attribute_timezone_aware?(attr_name)
|
||||||
|
|||||||
@ -71,9 +71,10 @@ ActiveRecord::Schema.define(:version => 1) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Employee < ActiveRecord::Base
|
class Employee < ActiveRecord::Base
|
||||||
define_attribute_methods
|
|
||||||
|
|
||||||
attr_accessor :redefined_birth_date_called
|
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)
|
def birth_date=(value)
|
||||||
self.redefined_birth_date_called = true
|
self.redefined_birth_date_called = true
|
||||||
@ -90,8 +91,5 @@ RSpec.configure do |c|
|
|||||||
Person.reset_callbacks(:validate)
|
Person.reset_callbacks(:validate)
|
||||||
PersonWithShim.timeliness_validated_attributes = []
|
PersonWithShim.timeliness_validated_attributes = []
|
||||||
Person._validators.clear
|
Person._validators.clear
|
||||||
Employee.reset_callbacks(:validate)
|
|
||||||
Employee.timeliness_validated_attributes = []
|
|
||||||
Employee._validators.clear
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -82,6 +82,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|||||||
r = Employee.create!
|
r = Employee.create!
|
||||||
r.birth_date = '2010-01-01'
|
r.birth_date = '2010-01-01'
|
||||||
r.reload
|
r.reload
|
||||||
|
|
||||||
r._timeliness_raw_value_for(:birth_date).should be_nil
|
r._timeliness_raw_value_for(:birth_date).should be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -94,7 +95,16 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|||||||
it 'should return original value' do
|
it 'should return original value' do
|
||||||
r = Employee.new
|
r = Employee.new
|
||||||
r.birth_datetime = date_string = '2010-01-01'
|
r.birth_datetime = date_string = '2010-01-01'
|
||||||
|
|
||||||
r.birth_datetime_before_type_cast.should == date_string
|
r.birth_datetime_before_type_cast.should == date_string
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user