mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
fix for validation nil value with :format option plugin parser disabled (issue 34)
This commit is contained in:
parent
983e80f239
commit
3793ef2ed4
@ -56,6 +56,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def parse(value)
|
def parse(value)
|
||||||
|
return nil if value.nil?
|
||||||
if ValidatesTimeliness.use_plugin_parser
|
if ValidatesTimeliness.use_plugin_parser
|
||||||
Timeliness::Parser.parse(value, @type, :zone => (:current if @timezone_aware), :format => options[:format], :strict => false)
|
Timeliness::Parser.parse(value, @type, :zone => (:current if @timezone_aware), :format => options[:format], :strict => false)
|
||||||
else
|
else
|
||||||
|
|||||||
@ -11,6 +11,7 @@ require 'validates_timeliness'
|
|||||||
|
|
||||||
require 'support/test_model'
|
require 'support/test_model'
|
||||||
require 'support/model_helpers'
|
require 'support/model_helpers'
|
||||||
|
require 'support/config_helper'
|
||||||
|
|
||||||
ValidatesTimeliness.setup do |c|
|
ValidatesTimeliness.setup do |c|
|
||||||
c.extend_orms = [ :active_record ]
|
c.extend_orms = [ :active_record ]
|
||||||
@ -89,6 +90,7 @@ Rspec.configure do |c|
|
|||||||
c.mock_with :rspec
|
c.mock_with :rspec
|
||||||
c.include(RspecTagMatchers)
|
c.include(RspecTagMatchers)
|
||||||
c.include(ModelHelpers)
|
c.include(ModelHelpers)
|
||||||
|
c.include(ConfigHelper)
|
||||||
c.before do
|
c.before do
|
||||||
Person.reset_callbacks(:validate)
|
Person.reset_callbacks(:validate)
|
||||||
PersonWithShim.timeliness_validated_attributes = []
|
PersonWithShim.timeliness_validated_attributes = []
|
||||||
|
|||||||
10
spec/support/config_helper.rb
Normal file
10
spec/support/config_helper.rb
Normal 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
|
||||||
@ -3,13 +3,13 @@ require 'spec_helper'
|
|||||||
describe ValidatesTimeliness::Conversion do
|
describe ValidatesTimeliness::Conversion do
|
||||||
include ValidatesTimeliness::Conversion
|
include ValidatesTimeliness::Conversion
|
||||||
|
|
||||||
|
let(:options) { Hash.new }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
Timecop.freeze(Time.mktime(2010, 1, 1, 0, 0, 0))
|
Timecop.freeze(Time.mktime(2010, 1, 1, 0, 0, 0))
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#type_cast_value" do
|
describe "#type_cast_value" do
|
||||||
let(:options) { Hash.new }
|
|
||||||
|
|
||||||
describe "for date type" do
|
describe "for date type" do
|
||||||
it "should return same value for date value" 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)
|
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
|
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
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user