moved ignore_restriction_errors and error_value_formats into Validator class

This commit is contained in:
Adam Meehan 2008-12-05 20:24:06 +11:00
parent 6cd6cd9dc0
commit 87b0beef5a
5 changed files with 23 additions and 28 deletions

6
README
View File

@ -187,7 +187,7 @@ of d/my/yy. By default the plugin uses the US formats as this is the Ruby defaul
when it does date interpretation, and is in keeping PoLS (principle of least when it does date interpretation, and is in keeping PoLS (principle of least
surprise). surprise).
To switch to using the European (or Rest of The World) formats put this in an To switch to using the :after => 1.day.from_nowEuropean (or Rest of The World) formats put this in an
initializer or environment.rb initializer or environment.rb
ValidatesTimeliness::Formats.remove_us_formats ValidatesTimeliness::Formats.remove_us_formats
@ -237,7 +237,7 @@ and you want the validation to complete. In these situations you turn them off.
To turn them off: To turn them off:
ValidatesTimeliness.ignore_restriction_errors = true ValidatesTimeliness::Validator.ignore_restriction_errors = true
A word of warning though, as this may hide issues with the model and make those A word of warning though, as this may hide issues with the model and make those
corner cases a little harder to test. In general if you are using procs or corner cases a little harder to test. In general if you are using procs or
@ -278,7 +278,7 @@ will be inserted.
And for something a little more specific you can override the format of the interpolation And for something a little more specific you can override the format of the interpolation
values inserted in the error messages for temporal restrictions like so values inserted in the error messages for temporal restrictions like so
ValidatesTimeliness.error_value_formats.update( ValidatesTimeliness::Validator.error_value_formats.update(
:time => '%H:%M:%S', :time => '%H:%M:%S',
:date => '%Y-%m-%d', :date => '%Y-%m-%d',
:datetime => '%Y-%m-%d %H:%M:%S' :datetime => '%Y-%m-%d %H:%M:%S'

View File

@ -13,17 +13,9 @@ require 'validates_timeliness/core_ext/date_time'
module ValidatesTimeliness module ValidatesTimeliness
mattr_accessor :ignore_restriction_errors
mattr_accessor :default_timezone mattr_accessor :default_timezone
mattr_accessor :error_value_formats
self.ignore_restriction_errors = false
self.default_timezone = :utc self.default_timezone = :utc
self.error_value_formats = {
:time => '%H:%M:%S',
:date => '%Y-%m-%d',
:datetime => '%Y-%m-%d %H:%M:%S'
}
LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/validates_timeliness/locale/en.yml') LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/validates_timeliness/locale/en.yml')

View File

@ -91,7 +91,7 @@ module Spec
def format_value(value) def format_value(value)
return value if value.is_a?(String) return value if value.is_a?(String)
value.strftime(ValidatesTimeliness.error_value_formats[options[:type]]) value.strftime(ValidatesTimeliness::Validator.error_value_formats[options[:type]])
end end
end end

View File

@ -1,9 +1,16 @@
module ValidatesTimeliness module ValidatesTimeliness
# Adds ActiveRecord validation methods for date, time and datetime validation.
# The validity of values can be restricted to be before or after certain dates
# or times.
class Validator class Validator
cattr_accessor :ignore_restriction_errors
cattr_accessor :error_value_formats
self.ignore_restriction_errors = false
self.error_value_formats = {
:time => '%H:%M:%S',
:date => '%Y-%m-%d',
:datetime => '%Y-%m-%d %H:%M:%S'
}
attr_reader :configuration, :type attr_reader :configuration, :type
def initialize(configuration) def initialize(configuration)
@ -33,13 +40,10 @@ module ValidatesTimeliness
record.send("#{attr_name}_before_type_cast") record.send("#{attr_name}_before_type_cast")
end end
# Validate value against the temporal restrictions. Restriction values
# maybe of mixed type, so they are evaluated as a common type, which may
# require conversion. The type used is defined by validation type.
def validate_restrictions(record, attr_name, value) def validate_restrictions(record, attr_name, value)
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='} restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
display = ValidatesTimeliness.error_value_formats[type] display = self.class.error_value_formats[type]
value = type_cast_value(value) value = type_cast_value(value)
@ -54,7 +58,7 @@ module ValidatesTimeliness
add_error(record, attr_name, option, :restriction => compare.strftime(display)) add_error(record, attr_name, option, :restriction => compare.strftime(display))
end end
rescue rescue
unless ValidatesTimeliness.ignore_restriction_errors unless self.class.ignore_restriction_errors
add_error(record, attr_name, "restriction '#{option}' value was invalid") add_error(record, attr_name, "restriction '#{option}' value was invalid")
end end
end end

View File

@ -121,7 +121,6 @@ describe ValidatesTimeliness::Validator do
end end
describe "instance with before and after restrictions" do describe "instance with before and after restrictions" do
describe "for datetime type" do describe "for datetime type" do
@ -333,18 +332,18 @@ describe ValidatesTimeliness::Validator do
end end
it "should be added by default for invalid restriction" do it "should be added by default for invalid restriction" do
ValidatesTimeliness.ignore_restriction_errors = false ValidatesTimeliness::Validator.ignore_restriction_errors = false
validate_with(:birth_date, Date.today) validate_with(:birth_date, Date.today)
person.errors.on(:birth_date).should match(/restriction 'before' value was invalid/) person.errors.on(:birth_date).should match(/restriction 'before' value was invalid/)
end end
it "should not be added when ignore switch is true and restriction is invalid" do it "should not be added when ignore switch is true and restriction is invalid" do
ValidatesTimeliness.ignore_restriction_errors = true ValidatesTimeliness::Validator.ignore_restriction_errors = true
person.should be_valid person.should be_valid
end end
after :all do after :all do
ValidatesTimeliness.ignore_restriction_errors = false ValidatesTimeliness::Validator.ignore_restriction_errors = false
end end
end end
@ -374,8 +373,8 @@ describe ValidatesTimeliness::Validator do
describe "custom formats" do describe "custom formats" do
before :all do before :all do
@@formats = ValidatesTimeliness.error_value_formats @@formats = ValidatesTimeliness::Validator.error_value_formats
ValidatesTimeliness.error_value_formats = { ValidatesTimeliness::Validator.error_value_formats = {
:time => '%H:%M %p', :time => '%H:%M %p',
:date => '%d-%m-%Y', :date => '%d-%m-%Y',
:datetime => '%d-%m-%Y %H:%M %p' :datetime => '%d-%m-%Y %H:%M %p'
@ -401,7 +400,7 @@ describe ValidatesTimeliness::Validator do
end end
after :all do after :all do
ValidatesTimeliness.error_value_formats = @@formats ValidatesTimeliness::Validator.error_value_formats = @@formats
end end
end end