mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
ignore restriction value errors
This commit is contained in:
parent
0720abeb0e
commit
6acf61aa88
@ -18,6 +18,9 @@ module ValidatesTimeliness
|
||||
mattr_accessor :dummy_date_for_time_type
|
||||
@@dummy_date_for_time_type = [ 2000, 1, 1 ]
|
||||
|
||||
# Ignore errors when restriction options are evaluated
|
||||
mattr_accessor :ignore_restriction_errors
|
||||
@@ignore_restriction_errors = false
|
||||
end
|
||||
|
||||
require 'validates_timeliness/conversion'
|
||||
|
||||
@ -39,9 +39,15 @@ module ValidatesTimeliness
|
||||
value = type_cast(value)
|
||||
|
||||
(RESTRICTIONS.keys & options.keys).each do |restriction|
|
||||
restriction_value = type_cast(evaluate_option_value(options[restriction], record))
|
||||
unless value.send(RESTRICTIONS[restriction], restriction_value)
|
||||
return record.errors.add(attr_name, restriction, :restriction => restriction_value)
|
||||
begin
|
||||
restriction_value = type_cast(evaluate_option_value(options[restriction], record))
|
||||
unless value.send(RESTRICTIONS[restriction], restriction_value)
|
||||
return record.errors.add(attr_name, restriction, :restriction => restriction_value)
|
||||
end
|
||||
rescue => e
|
||||
unless ValidatesTimeliness.ignore_restriction_errors
|
||||
record.errors[attr_name] = "Error occurred validating #{attr_name} for #{restriction.inspect} restriction:\n#{e.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -66,4 +66,28 @@ describe ValidatesTimeliness::Validator do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "restriction value errors" do
|
||||
let(:person) { Person.new(:birth_date => Date.today) }
|
||||
|
||||
before do
|
||||
Person.validates_time :birth_date, :is_at => lambda { raise }
|
||||
end
|
||||
|
||||
it "should be added when ignore_restriction_errors is false" do
|
||||
ValidatesTimeliness.ignore_restriction_errors = false
|
||||
person.valid?
|
||||
person.errors[:birth_date].first.should match("Error occurred validating birth_date for :is_at restriction")
|
||||
end
|
||||
|
||||
it "should not be added when ignore_restriction_errors is true" do
|
||||
ValidatesTimeliness.ignore_restriction_errors = true
|
||||
person.valid?
|
||||
person.errors[:birth_date].should be_empty
|
||||
end
|
||||
|
||||
after :all do
|
||||
ValidatesTimeliness.ignore_restriction_errors = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user