From 62557e7e043c40f5b9623a73f52b5524a73bd6b4 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Thu, 9 Aug 2012 12:53:29 +1000 Subject: [PATCH] Allow any validated attribute to pass timezone aware check in AR --- lib/validates_timeliness/orm/active_record.rb | 12 ++++++-- lib/validates_timeliness/validator.rb | 4 +++ .../orm/active_record_spec.rb | 30 +++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/validates_timeliness/orm/active_record.rb b/lib/validates_timeliness/orm/active_record.rb index eb235dc..f1b0b35 100644 --- a/lib/validates_timeliness/orm/active_record.rb +++ b/lib/validates_timeliness/orm/active_record.rb @@ -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 diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 9999b73..cc0baef 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -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) diff --git a/spec/validates_timeliness/orm/active_record_spec.rb b/spec/validates_timeliness/orm/active_record_spec.rb index 492b8ec..5670eba 100644 --- a/spec/validates_timeliness/orm/active_record_spec.rb +++ b/spec/validates_timeliness/orm/active_record_spec.rb @@ -43,10 +43,36 @@ describe ValidatesTimeliness, 'ActiveRecord' do it 'should determine type for attribute' 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