mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
methods for validated attributes for write cache and before type cast
cache raw value in @attributes_cache which is AR convention but should work fine with non-AR ORM before type cast method for reading back cached raw value
This commit is contained in:
@@ -25,12 +25,15 @@ LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/../lib/validates_timel
|
||||
I18n.load_path.unshift(LOCALE_PATH)
|
||||
|
||||
class Person
|
||||
include ActiveModel::AttributeMethods
|
||||
include ActiveModel::Validations
|
||||
extend ActiveModel::Translation
|
||||
|
||||
attr_accessor :birth_date, :birth_time, :birth_datetime
|
||||
attr_accessor :attributes
|
||||
|
||||
def initialize(attributes = {})
|
||||
@attributes = {}
|
||||
attributes.each do |key, value|
|
||||
send "#{key}=", value
|
||||
end
|
||||
|
||||
22
spec/validates_timeliness/attribute_methods_spec.rb
Normal file
22
spec/validates_timeliness/attribute_methods_spec.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe ValidatesTimeliness::AttributeMethods do
|
||||
before do
|
||||
Employee.validates_datetime :birth_datetime
|
||||
Employee.define_attribute_methods
|
||||
end
|
||||
|
||||
it 'should define attribute write method for validated attributes' do
|
||||
Employee.instance_methods(false).should include("birth_datetime=")
|
||||
end
|
||||
|
||||
it 'should define attribute before_type_cast method for validated attributes' do
|
||||
Employee.instance_methods(false).should include("birth_datetime_before_type_cast")
|
||||
end
|
||||
|
||||
it 'should store original raw value on attribute write' do
|
||||
r = Employee.new
|
||||
r.birth_datetime = '2010-01-01'
|
||||
r.birth_datetime_before_type_cast.should == '2010-01-01'
|
||||
end
|
||||
end
|
||||
@@ -12,4 +12,11 @@ describe ValidatesTimeliness::HelperMethods do
|
||||
ActiveRecord::Base.instance_methods.should include('validates_time')
|
||||
ActiveRecord::Base.instance_methods.should include('validates_datetime')
|
||||
end
|
||||
|
||||
describe ".timeliness_validated_attributes" do
|
||||
it 'should return attributes validated with plugin validator' do
|
||||
Person.validates_date :birth_date
|
||||
Person.timeliness_validated_attributes.should == ["birth_date"]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user