diff --git a/lib/validates_timeliness/conversion.rb b/lib/validates_timeliness/conversion.rb index 81d8468..7bd26d0 100644 --- a/lib/validates_timeliness/conversion.rb +++ b/lib/validates_timeliness/conversion.rb @@ -56,6 +56,7 @@ module ValidatesTimeliness end def parse(value) + return nil if value.nil? if ValidatesTimeliness.use_plugin_parser Timeliness::Parser.parse(value, @type, :zone => (:current if @timezone_aware), :format => options[:format], :strict => false) else diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9efd455..9417565 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,6 +11,7 @@ require 'validates_timeliness' require 'support/test_model' require 'support/model_helpers' +require 'support/config_helper' ValidatesTimeliness.setup do |c| c.extend_orms = [ :active_record ] @@ -89,6 +90,7 @@ Rspec.configure do |c| c.mock_with :rspec c.include(RspecTagMatchers) c.include(ModelHelpers) + c.include(ConfigHelper) c.before do Person.reset_callbacks(:validate) PersonWithShim.timeliness_validated_attributes = [] diff --git a/spec/support/config_helper.rb b/spec/support/config_helper.rb new file mode 100644 index 0000000..329a1f1 --- /dev/null +++ b/spec/support/config_helper.rb @@ -0,0 +1,10 @@ +module ConfigHelper + # Justin French tip + def with_config(preference_name, temporary_value) + old_value = ValidatesTimeliness.send(preference_name) + ValidatesTimeliness.send(:"#{preference_name}=", temporary_value) + yield + ensure + ValidatesTimeliness.send(:"#{preference_name}=", old_value) + end +end diff --git a/spec/validates_timeliness/conversion_spec.rb b/spec/validates_timeliness/conversion_spec.rb index 656ff57..a113e87 100644 --- a/spec/validates_timeliness/conversion_spec.rb +++ b/spec/validates_timeliness/conversion_spec.rb @@ -3,13 +3,13 @@ require 'spec_helper' describe ValidatesTimeliness::Conversion do include ValidatesTimeliness::Conversion + let(:options) { Hash.new } + before do Timecop.freeze(Time.mktime(2010, 1, 1, 0, 0, 0)) end describe "#type_cast_value" do - let(:options) { Hash.new } - describe "for date type" do it "should return same value for date value" do type_cast_value(Date.new(2010, 1, 1), :date).should == Date.new(2010, 1, 1) @@ -206,4 +206,40 @@ describe ValidatesTimeliness::Conversion do end end end + + describe "#parse" do + context "use_plugin_parser setting is true" do + around do |example| + with_config(:use_plugin_parser, true, &example) + end + + it 'should use timeliness' do + Timeliness::Parser.should_receive(:parse) + parse('2000-01-01') + end + end + + context "use_plugin_parser setting is false" do + around do |example| + with_config(:use_plugin_parser, false, &example) + end + + it 'should use Time.zone.parse attribute is timezone aware' do + @timezone_aware = true + Time.zone.should_receive(:parse) + parse('2000-01-01') + end + + it 'should use value#to_time if use_plugin_parser setting is false and attribute is not timezone aware' do + @timezone_aware = false + value = '2000-01-01' + value.should_receive(:to_time) + parse(value) + end + end + + it 'should return nil if value is nil' do + parse(nil).should be_nil + end + end end