fix for validation nil value with :format option plugin parser disabled (issue 34)

This commit is contained in:
Adam Meehan 2011-01-29 16:04:35 +11:00
parent 983e80f239
commit 3793ef2ed4
4 changed files with 51 additions and 2 deletions

View File

@ -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

View File

@ -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 = []

View File

@ -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

View File

@ -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