mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
refactored to get attribute type from model types not validation type
reverts to behaviour of old version which allows you to define validations of any type for sake of the values it is validating against
This commit is contained in:
@@ -48,11 +48,13 @@ end
|
||||
|
||||
class Person
|
||||
include TestModel
|
||||
self.model_attributes = :birth_date, :birth_time, :birth_datetime
|
||||
attribute :birth_date, :date
|
||||
attribute :birth_time, :time
|
||||
attribute :birth_datetime, :datetime
|
||||
validates_date :birth_date
|
||||
validates_time :birth_time
|
||||
validates_datetime :birth_datetime
|
||||
define_attribute_methods model_attributes
|
||||
define_attribute_methods model_attributes.keys
|
||||
end
|
||||
|
||||
ActiveRecord::Base.time_zone_aware_attributes = true
|
||||
@@ -62,8 +64,8 @@ ActiveRecord::Schema.define(:version => 1) do
|
||||
create_table :employees, :force => true do |t|
|
||||
t.string :first_name
|
||||
t.string :last_name
|
||||
t.datetime :birth_date
|
||||
t.datetime :birth_time
|
||||
t.date :birth_date
|
||||
t.time :birth_time
|
||||
t.datetime :birth_datetime
|
||||
end
|
||||
end
|
||||
@@ -80,10 +82,10 @@ Rspec.configure do |c|
|
||||
c.include(RspecTagMatchers)
|
||||
c.before do
|
||||
Person.reset_callbacks(:validate)
|
||||
Person.timeliness_validated_attributes = {}
|
||||
Person.timeliness_validated_attributes = []
|
||||
Person._validators.clear
|
||||
Employee.reset_callbacks(:validate)
|
||||
Employee.timeliness_validated_attributes = {}
|
||||
Employee.timeliness_validated_attributes = []
|
||||
Employee._validators.clear
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,24 +13,29 @@ module TestModel
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def attribute(name, type)
|
||||
self.model_attributes ||= {}
|
||||
self.model_attributes[name] = type
|
||||
end
|
||||
|
||||
def define_method_attribute=(attr_name)
|
||||
generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=self.class.type_cast(new_value); end", __FILE__, __LINE__)
|
||||
generated_attribute_methods.module_eval("def #{attr_name}=(new_value); @attributes['#{attr_name}']=self.class.type_cast('#{attr_name}', new_value); end", __FILE__, __LINE__)
|
||||
end
|
||||
|
||||
def define_method_attribute(attr_name)
|
||||
generated_attribute_methods.module_eval("def #{attr_name}; @attributes['#{attr_name}']; end", __FILE__, __LINE__)
|
||||
end
|
||||
|
||||
def type_cast(value)
|
||||
def type_cast(attr_name, value)
|
||||
return value unless value.is_a?(String)
|
||||
value.to_time rescue nil
|
||||
value.send("to_#{model_attributes[attr_name.to_sym]}") rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
module DynamicMethods
|
||||
def method_missing(method_id, *args, &block)
|
||||
if !self.class.attribute_methods_generated?
|
||||
self.class.define_attribute_methods self.class.model_attributes.map(&:to_s)
|
||||
self.class.define_attribute_methods self.class.model_attributes.keys.map(&:to_s)
|
||||
method_name = method_id.to_s
|
||||
send(method_id, *args, &block)
|
||||
else
|
||||
|
||||
@@ -8,7 +8,9 @@ describe ValidatesTimeliness::AttributeMethods do
|
||||
context "attribute write method" do
|
||||
class PersonWithCache
|
||||
include TestModel
|
||||
self.model_attributes = :birth_date, :birth_time, :birth_datetime
|
||||
attribute :birth_date, :date
|
||||
attribute :birth_time, :time
|
||||
attribute :birth_datetime, :datetime
|
||||
validates_date :birth_date
|
||||
validates_time :birth_time
|
||||
validates_datetime :birth_datetime
|
||||
@@ -23,7 +25,9 @@ describe ValidatesTimeliness::AttributeMethods do
|
||||
context "with plugin parser" do
|
||||
class PersonWithParser
|
||||
include TestModel
|
||||
self.model_attributes = :birth_date, :birth_time, :birth_datetime
|
||||
attribute :birth_date, :date
|
||||
attribute :birth_time, :time
|
||||
attribute :birth_datetime, :datetime
|
||||
validates_date :birth_date
|
||||
validates_time :birth_time
|
||||
validates_datetime :birth_datetime
|
||||
|
||||
@@ -21,16 +21,12 @@ describe ValidatesTimeliness::HelperMethods do
|
||||
|
||||
describe ".timeliness_validated_attributes" do
|
||||
it 'should return attributes validated with plugin validator' do
|
||||
Person.timeliness_validated_attributes = {}
|
||||
Person.timeliness_validated_attributes = []
|
||||
Person.validates_date :birth_date
|
||||
Person.validates_time :birth_time
|
||||
Person.validates_datetime :birth_datetime
|
||||
|
||||
Person.timeliness_validated_attributes.should == {
|
||||
"birth_date" => :date,
|
||||
"birth_time" => :time,
|
||||
"birth_datetime" => :datetime
|
||||
}
|
||||
Person.timeliness_validated_attributes.should == [ :birth_date, :birth_time, :birth_datetime ]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
end
|
||||
end
|
||||
|
||||
it 'should define _timeliness_raw_value_for instance method' do
|
||||
Employee.instance_methods.should include('_timeliness_raw_value_for')
|
||||
it 'should determine type for attribute' do
|
||||
Employee.timeliness_attribute_type(:birth_date).should == :date
|
||||
end
|
||||
|
||||
context "attribute write method" do
|
||||
|
||||
@@ -38,8 +38,8 @@ describe ValidatesTimeliness, 'Mongoid' do
|
||||
end
|
||||
end
|
||||
|
||||
it 'should define _timeliness_raw_value_for instance method' do
|
||||
Article.instance_methods.should include('_timeliness_raw_value_for')
|
||||
it 'should determine type for attribute' do
|
||||
Article.timeliness_attribute_type(:publish_date).should == :date
|
||||
end
|
||||
|
||||
context "attribute write method" do
|
||||
|
||||
@@ -101,7 +101,9 @@ describe ValidatesTimeliness::Validator do
|
||||
describe ":format option" do
|
||||
class PersonWithFormatOption
|
||||
include TestModel
|
||||
self.model_attributes = :birth_date, :birth_time, :birth_datetime
|
||||
attribute :birth_date, :date
|
||||
attribute :birth_time, :time
|
||||
attribute :birth_datetime, :datetime
|
||||
validates_date :birth_date, :format => 'dd-mm-yyyy'
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user