mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
fixe for attribute write and raw value methods
add new generalised method to get raw value without dependence on before_type_cast which may not be supported in ORMs call super to define full ORM write method default
This commit is contained in:
parent
30fb0e5192
commit
e3928e78eb
@ -22,9 +22,16 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
class_eval(method_body, __FILE__, line)
|
class_eval(method_body, __FILE__, line)
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
end
|
||||||
|
super rescue(NoMethodError)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
module InstanceMethods
|
||||||
|
|
||||||
|
def _timeliness_raw_value_for(attr_name)
|
||||||
|
@attributes_cache && @attributes_cache["_#{attr_name}_before_type_cast"]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@ -35,7 +42,7 @@ module ValidatesTimeliness
|
|||||||
if timeliness_validated_attributes.include?(attr_name)
|
if timeliness_validated_attributes.include?(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
|
||||||
@attributes_cache && @attributes_cache["_#{attr_name}_before_type_cast"]
|
_timeliness_raw_value_for('#{attr_name}')
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
class_eval(method_body, __FILE__, line)
|
class_eval(method_body, __FILE__, line)
|
||||||
|
|||||||
@ -56,8 +56,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def attribute_raw_value(record, attr_name)
|
def attribute_raw_value(record, attr_name)
|
||||||
before_type_cast = "#{attr_name}_before_type_cast"
|
record._timeliness_raw_value_for(attr_name)
|
||||||
record.send(before_type_cast) if record.respond_to?(before_type_cast)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast(value)
|
def type_cast(value)
|
||||||
|
|||||||
@ -4,19 +4,35 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
before do
|
before do
|
||||||
Employee.validates_datetime :birth_datetime
|
Employee.validates_datetime :birth_datetime
|
||||||
Employee.define_attribute_methods
|
Employee.define_attribute_methods
|
||||||
|
Person.validates_datetime :birth_datetime
|
||||||
|
Person.define_attribute_methods [:birth_datetime]
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should define attribute write method for validated attributes' do
|
it 'should define _timeliness_raw_value_for instance method' do
|
||||||
Employee.instance_methods(false).should include("birth_datetime=")
|
Person.instance_methods.should include('_timeliness_raw_value_for')
|
||||||
|
end
|
||||||
|
|
||||||
|
context "attribute write method" do
|
||||||
|
it 'should cache attribute raw value' do
|
||||||
|
r = Employee.new
|
||||||
|
r.birth_datetime = date_string = '2010-01-01'
|
||||||
|
r._timeliness_raw_value_for(:birth_datetime).should == date_string
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should define attribute before_type_cast method for validated attributes' do
|
context "before_type_cast method" do
|
||||||
Employee.instance_methods(false).should include("birth_datetime_before_type_cast")
|
it 'should be defined on class if ORM supports it' do
|
||||||
end
|
Employee.instance_methods(false).should include("birth_datetime_before_type_cast")
|
||||||
|
end
|
||||||
|
|
||||||
it 'should store original raw value on attribute write' do
|
it 'should not be defined if ORM does not support it' do
|
||||||
r = Employee.new
|
Person.instance_methods(false).should_not include("birth_datetime_before_type_cast")
|
||||||
r.birth_datetime = '2010-01-01'
|
end
|
||||||
r.birth_datetime_before_type_cast.should == '2010-01-01'
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user