From 27e01486e941676fd99d4d03c91991fe2fd124e0 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 21 Sep 2010 08:36:11 +1000 Subject: [PATCH] actually use the plugin parser in the write method (yikes) --- lib/validates_timeliness.rb | 2 +- lib/validates_timeliness/attribute_methods.rb | 1 + spec/spec_helper.rb | 1 + .../attribute_methods_spec.rb | 31 +++++++++++++++++-- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/validates_timeliness.rb b/lib/validates_timeliness.rb index 506e036..5a48279 100644 --- a/lib/validates_timeliness.rb +++ b/lib/validates_timeliness.rb @@ -9,6 +9,7 @@ require 'active_support/core_ext/date_time/acts_like' require 'active_support/core_ext/date_time/conversions' module ValidatesTimeliness + autoload :Parser, 'validates_timeliness/parser' autoload :VERSION, 'validates_timeliness/version' # Add plugin to supported ORMs (only :active_record for now) @@ -45,7 +46,6 @@ module ValidatesTimeliness end end -require 'validates_timeliness/parser' require 'validates_timeliness/conversion' require 'validates_timeliness/validator' require 'validates_timeliness/helper_methods' diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index 53a6c0a..1e67b03 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -18,6 +18,7 @@ module ValidatesTimeliness def #{attr_name}=(value) @attributes_cache ||= {} @attributes_cache["_#{attr_name}_before_type_cast"] = value + #{ "value = ValidatesTimeliness::Parser.parse(value, :#{type}) if value.is_a?(String)" if ValidatesTimeliness.use_plugin_parser } super end EOV diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dd3824c..af4b52e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -56,6 +56,7 @@ class Person define_attribute_methods model_attributes end +ActiveRecord::Base.time_zone_aware_attributes = true ActiveRecord::Base.establish_connection({:adapter => 'sqlite3', :database => ':memory:'}) ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define(:version => 1) do diff --git a/spec/validates_timeliness/attribute_methods_spec.rb b/spec/validates_timeliness/attribute_methods_spec.rb index 6dc3632..0359064 100644 --- a/spec/validates_timeliness/attribute_methods_spec.rb +++ b/spec/validates_timeliness/attribute_methods_spec.rb @@ -6,16 +6,43 @@ describe ValidatesTimeliness::AttributeMethods do end context "attribute write method" do - class EmployeeCopy < ActiveRecord::Base + class EmployeeWithCache < ActiveRecord::Base set_table_name 'employees' validates_datetime :birth_datetime end it 'should cache attribute raw value' do - r = EmployeeCopy.new + r = EmployeeWithCache.new r.birth_datetime = date_string = '2010-01-01' r._timeliness_raw_value_for(:birth_datetime).should == date_string end + + context "with plugin parser" do + class EmployeeWithParser < ActiveRecord::Base + set_table_name 'employees' + validates_datetime :birth_date + end + + before :all do + ValidatesTimeliness.use_plugin_parser = true + end + + it 'should parse a string value' do + ValidatesTimeliness::Parser.should_receive(:parse) + r = EmployeeWithParser.new + r.birth_date = '2010-01-01' + end + + it 'should be strict on day values' do + r = EmployeeWithParser.new + r.birth_date = '2010-02-31' + r.birth_date.should be_nil + end + + after :all do + ValidatesTimeliness.use_plugin_parser = false + end + end end context "before_type_cast method" do