Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Exit restriction validation on first restriction error

  • Loading branch information...
commit 2822e368e3c3c766896e408e6c6a660ab4cdc828 1 parent 2244d5e
@adzap adzap authored
View
13 lib/validates_timeliness/validator.rb
@@ -20,6 +20,8 @@ class Validator < ActiveModel::EachValidator
:datetime => '%Y-%m-%d %H:%M:%S'
}.freeze
+ RESTRICTION_ERROR_MESSAGE = "Error occurred validating %s for %s restriction:\n%s"
+
def self.kind
:timeliness
end
@@ -45,17 +47,22 @@ def validate_each(record, attr_name, value)
value = parse(raw_value) if value.is_a?(String) || options[:format]
value = type_cast_value(value, @type)
- return add_error(record, attr_name, :"invalid_#{@type}") if value.blank?
+ add_error(record, attr_name, :"invalid_#{@type}") and return if value.blank?
+
+ validate_restrictions(record, attr_name, value)
+ end
+ def validate_restrictions(record, attr_name, value)
@restrictions_to_check.each do |restriction|
begin
restriction_value = type_cast_value(evaluate_option_value(options[restriction], record), @type)
unless value.send(RESTRICTIONS[restriction], restriction_value)
- return add_error(record, attr_name, restriction, restriction_value)
+ add_error(record, attr_name, restriction, restriction_value) and break
end
rescue => e
unless ValidatesTimeliness.ignore_restriction_errors
- add_error(record, attr_name, "Error occurred validating #{attr_name} for #{restriction.inspect} restriction:\n#{e.message}")
+ message = RESTRICTION_ERROR_MESSAGE % [ attr_name, restriction.inspect, e.message ]
+ add_error(record, attr_name, message) and break
end
end
end
View
9 spec/validates_timeliness/validator_spec.rb
@@ -136,7 +136,7 @@ class PersonWithFormatOption
let(:person) { Person.new(:birth_date => Date.today) }
before do
- Person.validates_time :birth_date, :is_at => lambda { raise }
+ Person.validates_time :birth_date, :is_at => lambda { raise }, :before => lambda { raise }
end
it "should be added when ignore_restriction_errors is false" do
@@ -152,6 +152,13 @@ class PersonWithFormatOption
person.errors[:birth_date].should be_empty
end
end
+
+ it 'should exit on first error' do
+ with_config(:ignore_restriction_errors, false) do
+ person.valid?
+ person.errors[:birth_date].should have(1).items
+ end
+ end
end
describe "#format_error_value" do
Please sign in to comment.
Something went wrong with that request. Please try again.