Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
# Set the dummy date part for a time type values.
# config.dummy_date_for_time_type = [ 2000, 1, 1 ]
#
# Ignore errors when restriction options are evaluated
# When an exception is raised inside a validation proc, the least surprise
# is to raise an exception, but we have other options below.
config.reraise_validation_errors = true
#
# Ignore errors when restriction options are evaluated. Consider using an :if option to avoid errors and reraising as above
# config.ignore_restriction_errors = false
#
# Re-display invalid values in date/time selects
Expand Down
2 changes: 1 addition & 1 deletion lib/validates_timeliness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module ValidatesTimeliness
class << self
delegate :default_timezone, :default_timezone=, :dummy_date_for_time_type, :dummy_date_for_time_type=, :to => Timeliness

attr_accessor :extend_orms, :ignore_restriction_errors, :restriction_shorthand_symbols, :use_plugin_parser
attr_accessor :extend_orms, :ignore_restriction_errors, :reraise_validation_errors, :restriction_shorthand_symbols, :use_plugin_parser
end

# Extend ORM/ODMs for full support (:active_record).
Expand Down
5 changes: 5 additions & 0 deletions lib/validates_timeliness/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ class Railtie < Rails::Railtie
initializer "validates_timeliness.initialize_restriction_errors" do
ValidatesTimeliness.ignore_restriction_errors = !Rails.env.test?
end

initializer "validates_timeliness.reraise_validation_errors" do
ValidatesTimeliness.reraise_validation_errors = false # for backwards compatibility. initializer file should set to true.
end

end
end
1 change: 1 addition & 0 deletions lib/validates_timeliness/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def validate_restrictions(record, attr_name, value)
add_error(record, attr_name, restriction, restriction_value) and break
end
rescue => e
raise e if ValidatesTimeliness.reraise_validation_errors
unless ValidatesTimeliness.ignore_restriction_errors
message = RESTRICTION_ERROR_MESSAGE % [ attr_name, restriction.inspect, e.message ]
add_error(record, attr_name, message) and break
Expand Down
20 changes: 20 additions & 0 deletions spec/validates_timeliness/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ class PersonWithFormatOption
end
end

describe "reraise validation errors" do
let(:person) { Person.new(:birth_date => Date.today) }

before do
Person.validates_time :birth_date, :is_at => lambda { raise "oops" }, :before => lambda { raise }
end

it "should raise an error when reraise_validation_errors is true" do
with_config(:reraise_validation_errors, true) do
expect {person.valid?}.to raise_exception("oops")
end
end

it "should not raise an error when reraise_validation_errors is false" do
with_config(:reraise_validation_errors, false) do
expect {person.valid?}.not_to raise_exception
end
end
end

describe "restriction value errors" do
let(:person) { Person.new(:birth_date => Date.today) }

Expand Down