fixed matcher messages and refactored

This commit is contained in:
Adam Meehan 2008-07-11 17:41:50 +10:00
parent 1a0a35836f
commit 710ed92721

View File

@ -4,9 +4,7 @@ module Spec
class ValidateTimeliness class ValidateTimeliness
def initialize(attribute, options) def initialize(attribute, options)
@expected, @options = attribute, options @expected, @options = attribute, options
messages = ActiveRecord::Base.send(:timeliness_default_error_messages) @options.reverse_merge!(error_messages)
messages
@options.reverse_merge!(messages)
end end
def error_messages def error_messages
@ -21,33 +19,28 @@ module Spec
valid = error_matching('2008-02-30', /#{options[:invalid_date_message]}/) && valid = error_matching('2008-02-30', /#{options[:invalid_date_message]}/) &&
error_matching('2008-01-01 25:00:00', /#{options[:invalid_date_message]}/) && error_matching('2008-01-01 25:00:00', /#{options[:invalid_date_message]}/) &&
no_error_matching('2008-01-01 12:12:12', /#{options[:invalid_date_message]}/) no_error_matching('2008-01-01 12:12:12', /#{options[:invalid_date_message]}/)
return false unless valid
if valid && after = options[:after]
if after = options[:after]
valid = error_matching(after, /#{options[:after_message]}/) && valid = error_matching(after, /#{options[:after_message]}/) &&
no_error_matching(after + 1, /#{options[:after_message]}/) no_error_matching(after + 1, /#{options[:after_message]}/)
end end
return false unless valid
if before = options[:before] if valid && before = options[:before]
valid = error_matching(before, /#{options[:after_message]}/) && valid = error_matching(before, /#{options[:after_message]}/) &&
no_error_matching(before - 1, /#{options[:after_message]}/) no_error_matching(before - 1, /#{options[:after_message]}/)
end end
return false unless valid
if on_or_after = options[:on_or_after] if valid && on_or_after = options[:on_or_after]
valid = error_matching(on_or_after -1, /#{options[:on_or_after_message]}/) && valid = error_matching(on_or_after -1, /#{options[:on_or_after_message]}/) &&
no_error_matching(on_or_after, /#{options[:on_or_after_message]}/) no_error_matching(on_or_after, /#{options[:on_or_after_message]}/)
end end
return false unless valid
if on_or_before = options[:on_or_before] if valid && on_or_before = options[:on_or_before]
valid = error_matching(on_or_before + 1, /#{options[:on_or_before_message]}/) && valid = error_matching(on_or_before + 1, /#{options[:on_or_before_message]}/) &&
no_error_matching(on_or_before, /#{options[:on_or_before_message]}/) no_error_matching(on_or_before, /#{options[:on_or_before_message]}/)
end end
return false unless valid
return true return valid
end end
def failure_message def failure_message