mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
Allow any validated attribute to pass timezone aware check in AR
This commit is contained in:
parent
fd73c4eccd
commit
62557e7e04
@ -20,12 +20,18 @@ module ValidatesTimeliness
|
|||||||
public
|
public
|
||||||
|
|
||||||
def timeliness_attribute_timezone_aware?(attr_name)
|
def timeliness_attribute_timezone_aware?(attr_name)
|
||||||
attr_name = attr_name.to_s
|
create_time_zone_conversion_attribute?(attr_name, timeliness_column_for_attribute(attr_name))
|
||||||
create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def timeliness_attribute_type(attr_name)
|
def timeliness_attribute_type(attr_name)
|
||||||
columns_hash[attr_name.to_s].type
|
timeliness_column_for_attribute(attr_name).type
|
||||||
|
end
|
||||||
|
|
||||||
|
def timeliness_column_for_attribute(attr_name)
|
||||||
|
columns_hash.fetch(attr_name.to_s) do |attr_name|
|
||||||
|
validation_type = _validators[attr_name.to_sym].find {|v| v.kind == :timeliness }.type
|
||||||
|
::ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, validation_type.to_s)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_attribute_methods
|
def define_attribute_methods
|
||||||
|
|||||||
@ -22,6 +22,10 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
|
RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
|
||||||
|
|
||||||
|
def self.kind
|
||||||
|
:timeliness
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
@type = options.delete(:type) || :datetime
|
@type = options.delete(:type) || :datetime
|
||||||
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
|
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
|
||||||
|
|||||||
@ -43,10 +43,36 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|||||||
it 'should determine type for attribute' do
|
it 'should determine type for attribute' do
|
||||||
Employee.timeliness_attribute_type(:birth_date).should == :date
|
Employee.timeliness_attribute_type(:birth_date).should == :date
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'attribute timezone awareness' do
|
||||||
|
let(:klass) {
|
||||||
|
Class.new(ActiveRecord::Base) do
|
||||||
|
self.table_name = 'employees'
|
||||||
|
attr_accessor :some_date
|
||||||
|
attr_accessor :some_datetime
|
||||||
|
validates_date :some_date
|
||||||
|
validates_datetime :some_datetime
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
context 'for column attribute' do
|
||||||
|
it 'should be detected from column type' do
|
||||||
|
klass.timeliness_attribute_timezone_aware?(:birth_date).should be_false
|
||||||
|
klass.timeliness_attribute_timezone_aware?(:birth_datetime).should be_true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'for non-column attribute' do
|
||||||
|
it 'should be detected from the validation type' do
|
||||||
|
klass.timeliness_attribute_timezone_aware?(:some_date).should be_false
|
||||||
|
klass.timeliness_attribute_timezone_aware?(:some_datetime).should be_true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "attribute write method" do
|
context "attribute write method" do
|
||||||
class EmployeeWithCache < ActiveRecord::Base
|
class EmployeeWithCache < ActiveRecord::Base
|
||||||
set_table_name 'employees'
|
self.table_name = 'employees'
|
||||||
validates_date :birth_date, :allow_blank => true
|
validates_date :birth_date, :allow_blank => true
|
||||||
validates_datetime :birth_datetime, :allow_blank => true
|
validates_datetime :birth_datetime, :allow_blank => true
|
||||||
end
|
end
|
||||||
@ -73,7 +99,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
|||||||
with_config(:use_plugin_parser, true)
|
with_config(:use_plugin_parser, true)
|
||||||
|
|
||||||
class EmployeeWithParser < ActiveRecord::Base
|
class EmployeeWithParser < ActiveRecord::Base
|
||||||
set_table_name 'employees'
|
self.table_name = 'employees'
|
||||||
validates_date :birth_date, :allow_blank => true
|
validates_date :birth_date, :allow_blank => true
|
||||||
validates_datetime :birth_datetime, :allow_blank => true
|
validates_datetime :birth_datetime, :allow_blank => true
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user