mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
added ignore_datetime_restriction_errors class option and docs
bit more for chronic parser
This commit is contained in:
parent
1836e4f6ca
commit
4e8007bc65
36
README
36
README
@ -240,17 +240,39 @@ an example of using Chronis instead. Put this into a file in the lib directory.
|
||||
class ActiveRecord::Base
|
||||
|
||||
def self.parse_date_time_with_chronic(raw_value, type, strict=true)
|
||||
# You can ignore type and strict with Chronic
|
||||
Chronic.parse(raw_value)
|
||||
# you can ignore strict with Chronic
|
||||
|
||||
# You could also fallback to normal plugin with
|
||||
#rescue
|
||||
# parse_date_time_without_chronic
|
||||
# Times without meridian are assumed to be in 24 hour format
|
||||
options = { :ambiguous_time_range => :none }
|
||||
|
||||
# Attribute of type :time will have a dummy date of 2000-01-01 which is
|
||||
# per Rails convention
|
||||
options[:now] = Time.local(2000, 1, 1) if type == :time
|
||||
Chronic.parse(raw_value)
|
||||
end
|
||||
alias_method_chain :parse_date_time, :chronic
|
||||
|
||||
end
|
||||
|
||||
|
||||
=== TEMPORAL RESTRICTION ERRORS
|
||||
|
||||
When using the validation temporal options there are times when the restriction
|
||||
value itself may be invalid. Normally this will add an error to the model such as
|
||||
'restriction :before value was invalid'. These can be annoying if you are using
|
||||
other attributes in the model as restrictions, since hopefully those attributes
|
||||
used are also validated and will have errors themselves which will stop the
|
||||
record from saving. In these situations you turn them off.
|
||||
|
||||
To turn them off globally:
|
||||
|
||||
ActiveRecord::Base.ignore_datetime_restriction_errors = true
|
||||
|
||||
To switch them on or off per model:
|
||||
|
||||
class Person < ActiveRecord::Base
|
||||
self.ignore_datetime_restriction_errors = false
|
||||
end
|
||||
|
||||
|
||||
== CREDITS:
|
||||
|
||||
* Adam Meehan (adam.meehan@gmail.com, http://duckpunching.com/)
|
||||
|
||||
@ -7,6 +7,8 @@ module ValidatesTimeliness
|
||||
# Error messages added to AR defaults to allow global override.
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.class_inheritable_accessor :ignore_datetime_restriction_errors
|
||||
base.ignore_datetime_restriction_errors = false
|
||||
|
||||
error_messages = {
|
||||
:invalid_datetime => "is not a valid %s",
|
||||
@ -105,9 +107,9 @@ module ValidatesTimeliness
|
||||
|
||||
private
|
||||
|
||||
# Validate value against the restrictions. Restriction values maybe of
|
||||
# mixed type so evaluate them and convert them all to common type as
|
||||
# defined by type param.
|
||||
# Validate value against the temoSpral restrictions. Restriction values
|
||||
# maybe of mixed type, so the are evaluated as a common type, which may
|
||||
# require conversion. The type used is defined by validation type.
|
||||
def validate_timeliness_restrictions(record, attr_name, value, configuration)
|
||||
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
||||
|
||||
@ -138,7 +140,7 @@ module ValidatesTimeliness
|
||||
compare = compare.send(conversion_method)
|
||||
record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare) unless value.send(method, compare)
|
||||
rescue
|
||||
record.errors.add(attr_name, "restriction '#{option}' value was invalid")
|
||||
record.errors.add(attr_name, "restriction '#{option}' value was invalid") unless self.ignore_datetime_restriction_errors
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -297,7 +297,6 @@ describe ValidatesTimeliness::Validations do
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe "with mixed value and restriction types" do
|
||||
before :all do
|
||||
class MixedBeforeAndAfter < Person
|
||||
@ -340,5 +339,23 @@ describe ValidatesTimeliness::Validations do
|
||||
@person.errors.on(:birth_date).should match(/must be on or after/)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe "ignoring rstriction errors" do
|
||||
before :all do
|
||||
class BadRestriction < Person
|
||||
validates_date :birth_date, :before => Proc.new { raise }
|
||||
self.ignore_datetime_restriction_errors = true
|
||||
end
|
||||
end
|
||||
|
||||
before :each do
|
||||
@person = BadRestriction.new
|
||||
end
|
||||
|
||||
it "should have no errors when restriction is invalid" do
|
||||
@person.birth_date = '1950-01-01'
|
||||
@person.should be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user