mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +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
|
||||
|
||||
def timeliness_attribute_timezone_aware?(attr_name)
|
||||
attr_name = attr_name.to_s
|
||||
create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
|
||||
create_time_zone_conversion_attribute?(attr_name, timeliness_column_for_attribute(attr_name))
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def define_attribute_methods
|
||||
|
||||
@ -22,6 +22,10 @@ module ValidatesTimeliness
|
||||
|
||||
RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
|
||||
|
||||
def self.kind
|
||||
:timeliness
|
||||
end
|
||||
|
||||
def initialize(options)
|
||||
@type = options.delete(:type) || :datetime
|
||||
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
|
||||
|
||||
@ -44,9 +44,35 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
Employee.timeliness_attribute_type(:birth_date).should == :date
|
||||
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
|
||||
class EmployeeWithCache < ActiveRecord::Base
|
||||
set_table_name 'employees'
|
||||
self.table_name = 'employees'
|
||||
validates_date :birth_date, :allow_blank => true
|
||||
validates_datetime :birth_datetime, :allow_blank => true
|
||||
end
|
||||
@ -73,7 +99,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
|
||||
with_config(:use_plugin_parser, true)
|
||||
|
||||
class EmployeeWithParser < ActiveRecord::Base
|
||||
set_table_name 'employees'
|
||||
self.table_name = 'employees'
|
||||
validates_date :birth_date, :allow_blank => true
|
||||
validates_datetime :birth_datetime, :allow_blank => true
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user