mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
refactored conversion to remove Time class check which is done in type
cast method
This commit is contained in:
parent
caf92fba13
commit
228ab29b5f
@ -25,10 +25,12 @@ module ValidatesTimeliness
|
|||||||
# valid parsed value.
|
# valid parsed value.
|
||||||
#
|
#
|
||||||
# Remember, if the date portion is pre the Unix epoch the return object
|
# Remember, if the date portion is pre the Unix epoch the return object
|
||||||
# will need to be a datatime. But luckily Rails, since version 2, will
|
# will need to be a datetime. But luckily Rails, since version 2, will
|
||||||
# automatically handle the fall back to a DateTime when you create a time
|
# automatically handle the fall back to a DateTime when you create a time
|
||||||
# with Time.new or #to_time.
|
# which is out of range.
|
||||||
def timeliness_date_time_parse(raw_value)
|
def timeliness_date_time_parse(raw_value)
|
||||||
|
return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
||||||
|
|
||||||
time_array = ParseDate.parsedate(raw_value, true)
|
time_array = ParseDate.parsedate(raw_value, true)
|
||||||
|
|
||||||
# checks if date part is valid, enforcing days in a month unlike Time
|
# checks if date part is valid, enforcing days in a month unlike Time
|
||||||
@ -41,7 +43,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def validates_timeliness_of(*attr_names)
|
def validates_timeliness_of(*attr_names)
|
||||||
configuration = { :on => :save, :type => :time, :allow_nil => false, :allow_blank => false }
|
configuration = { :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false }
|
||||||
configuration.update(timeliness_default_error_messages)
|
configuration.update(timeliness_default_error_messages)
|
||||||
configuration.update(attr_names.extract_options!)
|
configuration.update(attr_names.extract_options!)
|
||||||
|
|
||||||
@ -58,14 +60,10 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
column = record.column_for_attribute(attr_name)
|
column = record.column_for_attribute(attr_name)
|
||||||
begin
|
begin
|
||||||
if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
unless time = timeliness_date_time_parse(raw_value)
|
||||||
time = raw_value
|
record.send("#{attr_name}=", nil)
|
||||||
else
|
record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type])
|
||||||
unless time = timeliness_date_time_parse(raw_value)
|
next
|
||||||
record.send("#{attr_name}=", nil)
|
|
||||||
record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type])
|
|
||||||
next
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
validate_timeliness_restrictions(record, attr_name, time, configuration)
|
validate_timeliness_restrictions(record, attr_name, time, configuration)
|
||||||
@ -78,7 +76,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Use this validation to force validation of values as Time
|
# Use this validation to force validation of values as dummy time
|
||||||
def validates_time(*attr_names)
|
def validates_time(*attr_names)
|
||||||
configuration = attr_names.extract_options!
|
configuration = attr_names.extract_options!
|
||||||
configuration[:type] = :time
|
configuration[:type] = :time
|
||||||
@ -92,19 +90,24 @@ module ValidatesTimeliness
|
|||||||
validates_timeliness_of(attr_names, configuration)
|
validates_timeliness_of(attr_names, configuration)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Use this validation to force validation of values as DateTime
|
# Use this validation to force validation of values as Time/DateTime
|
||||||
def validates_datetime(*attr_names)
|
def validates_datetime(*attr_names)
|
||||||
configuration = attr_names.extract_options!
|
configuration = attr_names.extract_options!
|
||||||
configuration[:type] = :datetime
|
configuration[:type] = :datetime
|
||||||
validates_timeliness_of(attr_names, configuration)
|
validates_timeliness_of(attr_names, configuration)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
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 => '>='}
|
||||||
|
|
||||||
conversion_method = "to_#{configuration[:type]}".to_sym
|
conversion_method = case configuration[:type]
|
||||||
|
when :time then :to_dummy_time
|
||||||
|
when :date then :to_date
|
||||||
|
when :datetime then :to_time
|
||||||
|
end
|
||||||
|
|
||||||
time = value.send(conversion_method)
|
time = value.send(conversion_method)
|
||||||
|
|
||||||
restriction_methods.each do |option, method|
|
restriction_methods.each do |option, method|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user