mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 23:06:42 +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
|
class ActiveRecord::Base
|
||||||
|
|
||||||
def self.parse_date_time_with_chronic(raw_value, type, strict=true)
|
def self.parse_date_time_with_chronic(raw_value, type, strict=true)
|
||||||
# You can ignore type and strict with Chronic
|
# you can ignore strict with Chronic
|
||||||
Chronic.parse(raw_value)
|
|
||||||
|
|
||||||
# You could also fallback to normal plugin with
|
# Times without meridian are assumed to be in 24 hour format
|
||||||
#rescue
|
options = { :ambiguous_time_range => :none }
|
||||||
# parse_date_time_without_chronic
|
|
||||||
|
# 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
|
end
|
||||||
alias_method_chain :parse_date_time, :chronic
|
|
||||||
|
|
||||||
end
|
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:
|
== CREDITS:
|
||||||
|
|
||||||
* Adam Meehan (adam.meehan@gmail.com, http://duckpunching.com/)
|
* 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.
|
# Error messages added to AR defaults to allow global override.
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
base.extend ClassMethods
|
base.extend ClassMethods
|
||||||
|
base.class_inheritable_accessor :ignore_datetime_restriction_errors
|
||||||
|
base.ignore_datetime_restriction_errors = false
|
||||||
|
|
||||||
error_messages = {
|
error_messages = {
|
||||||
:invalid_datetime => "is not a valid %s",
|
:invalid_datetime => "is not a valid %s",
|
||||||
@ -105,9 +107,9 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Validate value against the restrictions. Restriction values maybe of
|
# Validate value against the temoSpral restrictions. Restriction values
|
||||||
# mixed type so evaluate them and convert them all to common type as
|
# maybe of mixed type, so the are evaluated as a common type, which may
|
||||||
# defined by type param.
|
# require conversion. The type used is defined by validation type.
|
||||||
def validate_timeliness_restrictions(record, attr_name, value, configuration)
|
def validate_timeliness_restrictions(record, attr_name, value, configuration)
|
||||||
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
||||||
|
|
||||||
@ -138,7 +140,7 @@ module ValidatesTimeliness
|
|||||||
compare = compare.send(conversion_method)
|
compare = compare.send(conversion_method)
|
||||||
record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare) unless value.send(method, compare)
|
record.errors.add(attr_name, configuration["#{option}_message".to_sym] % compare) unless value.send(method, compare)
|
||||||
rescue
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -297,7 +297,6 @@ describe ValidatesTimeliness::Validations do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
describe "with mixed value and restriction types" do
|
describe "with mixed value and restriction types" do
|
||||||
before :all do
|
before :all do
|
||||||
class MixedBeforeAndAfter < Person
|
class MixedBeforeAndAfter < Person
|
||||||
@ -340,5 +339,23 @@ describe ValidatesTimeliness::Validations do
|
|||||||
@person.errors.on(:birth_date).should match(/must be on or after/)
|
@person.errors.on(:birth_date).should match(/must be on or after/)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user