mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
moved ignore_restriction_errors and error_value_formats into Validator class
This commit is contained in:
parent
6cd6cd9dc0
commit
87b0beef5a
6
README
6
README
@ -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
|
||||
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
|
||||
|
||||
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:
|
||||
|
||||
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
|
||||
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
|
||||
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',
|
||||
:date => '%Y-%m-%d',
|
||||
:datetime => '%Y-%m-%d %H:%M:%S'
|
||||
|
||||
@ -13,17 +13,9 @@ require 'validates_timeliness/core_ext/date_time'
|
||||
|
||||
module ValidatesTimeliness
|
||||
|
||||
mattr_accessor :ignore_restriction_errors
|
||||
mattr_accessor :default_timezone
|
||||
mattr_accessor :error_value_formats
|
||||
|
||||
self.ignore_restriction_errors = false
|
||||
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')
|
||||
|
||||
|
||||
@ -91,7 +91,7 @@ module Spec
|
||||
|
||||
def format_value(value)
|
||||
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
|
||||
|
||||
|
||||
@ -1,9 +1,16 @@
|
||||
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
|
||||
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
|
||||
|
||||
def initialize(configuration)
|
||||
@ -33,13 +40,10 @@ module ValidatesTimeliness
|
||||
record.send("#{attr_name}_before_type_cast")
|
||||
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)
|
||||
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)
|
||||
|
||||
@ -54,7 +58,7 @@ module ValidatesTimeliness
|
||||
add_error(record, attr_name, option, :restriction => compare.strftime(display))
|
||||
end
|
||||
rescue
|
||||
unless ValidatesTimeliness.ignore_restriction_errors
|
||||
unless self.class.ignore_restriction_errors
|
||||
add_error(record, attr_name, "restriction '#{option}' value was invalid")
|
||||
end
|
||||
end
|
||||
|
||||
@ -121,7 +121,6 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
end
|
||||
|
||||
|
||||
describe "instance with before and after restrictions" do
|
||||
|
||||
describe "for datetime type" do
|
||||
@ -333,18 +332,18 @@ describe ValidatesTimeliness::Validator do
|
||||
end
|
||||
|
||||
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)
|
||||
person.errors.on(:birth_date).should match(/restriction 'before' value was invalid/)
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
after :all do
|
||||
ValidatesTimeliness.ignore_restriction_errors = false
|
||||
ValidatesTimeliness::Validator.ignore_restriction_errors = false
|
||||
end
|
||||
end
|
||||
|
||||
@ -374,8 +373,8 @@ describe ValidatesTimeliness::Validator do
|
||||
describe "custom formats" do
|
||||
|
||||
before :all do
|
||||
@@formats = ValidatesTimeliness.error_value_formats
|
||||
ValidatesTimeliness.error_value_formats = {
|
||||
@@formats = ValidatesTimeliness::Validator.error_value_formats
|
||||
ValidatesTimeliness::Validator.error_value_formats = {
|
||||
:time => '%H:%M %p',
|
||||
:date => '%d-%m-%Y',
|
||||
:datetime => '%d-%m-%Y %H:%M %p'
|
||||
@ -401,7 +400,7 @@ describe ValidatesTimeliness::Validator do
|
||||
end
|
||||
|
||||
after :all do
|
||||
ValidatesTimeliness.error_value_formats = @@formats
|
||||
ValidatesTimeliness::Validator.error_value_formats = @@formats
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user