mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
fixed restriction value conversion and added nil check
This commit is contained in:
parent
cae3d8cb84
commit
9a8a82c699
@ -8,48 +8,56 @@ module ValidatesTimeliness
|
||||
module ClassMethods
|
||||
|
||||
def validates_timeliness_of(*attr_names)
|
||||
# possible options only_date only_time only_epoch
|
||||
configuration = { :on => :save, :allow_nil => false }
|
||||
configuration = { :on => :save, :allow_nil => false, :allow_blank => false }
|
||||
configuration.update(attr_names.extract_options!)
|
||||
|
||||
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
||||
|
||||
validates_each(attr_names, configuration) do |record, attr_name, value|
|
||||
raw_value = record.send("#{attr_name}_before_type_cast") || value
|
||||
# we need to check raw value for blank or nil to catch when invalid value returns nil
|
||||
allow_nil = configuration.delete(:allow_nil)
|
||||
allow_blank = configuration.delete(:allow_blank)
|
||||
|
||||
if raw_value.nil?
|
||||
record.errors.add(attr_name, "cant' be blank") unless configuration[:allow_nil]
|
||||
next
|
||||
end
|
||||
validates_each(attr_names, configuration) do |record, attr_name, value|
|
||||
raw_value = record.send("#{attr_name}_before_type_cast")
|
||||
|
||||
next if (raw_value.nil? && allow_nil) || (raw_value.blank? && allow_blank)
|
||||
|
||||
record.errors.add(attr_name, "can't be blank") and next if raw_value.blank?
|
||||
|
||||
column = record.column_for_attribute(attr_name)
|
||||
begin
|
||||
time = if raw_value.acts_like?(:time)
|
||||
time = if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
|
||||
raw_value
|
||||
elsif raw_value.is_a?(Date)
|
||||
raw_value.to_time
|
||||
else
|
||||
time_array = ParseDate.parsedate(raw_value)
|
||||
|
||||
# checks if date is valid and enforces number of days in a month unlike Time
|
||||
date = Date.new(*time_array[0..2])
|
||||
# checks if date is valid which enforces number of days in a month unlike Time
|
||||
Date.new(*time_array[0..2])
|
||||
|
||||
# checks if time is valid as it will accept bad date values
|
||||
# checks if time is valid and return object
|
||||
Time.mktime(*time_array)
|
||||
end
|
||||
|
||||
conversion_method = column.type == :date ? :to_date : :to_time
|
||||
time = time.send(conversion_method)
|
||||
|
||||
restriction_methods.each do |option, method|
|
||||
if restriction = configuration[option]
|
||||
begin
|
||||
compare = case restriction
|
||||
when Time, Date, DateTime
|
||||
restriction.to_time
|
||||
when Date
|
||||
restriction
|
||||
when Time, DateTime
|
||||
restriction.respond_to?(:in_time_zone) ? restriction.in_time_zone : restriction
|
||||
when Symbol
|
||||
record.send(restriction).to_time
|
||||
record.send(restriction)
|
||||
when Proc
|
||||
restriction.call(record)
|
||||
end
|
||||
|
||||
begin
|
||||
next if compare.nil?
|
||||
compare = compare.send(conversion_method) if compare
|
||||
|
||||
record.errors.add(attr_name, "must be #{option.to_s.humanize.downcase} #{compare}") unless time.send(method, compare)
|
||||
rescue
|
||||
record.errors.add(attr_name, "restriction '#{option}' value was invalid")
|
||||
@ -65,6 +73,9 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
alias validates_times validates_timeliness_of
|
||||
alias validates_dates validates_timeliness_of
|
||||
alias validates_datetimes validates_timeliness_of
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user