mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
refactored restrictions validation into own method
added validates_*type methods for forcing the value type used when doing restriction validation
This commit is contained in:
parent
a27e4172a7
commit
d035de1d7c
@ -27,7 +27,7 @@ module ValidatesTimeliness
|
||||
begin
|
||||
time_array = ParseDate.parsedate(raw_value)
|
||||
|
||||
# checks if date is valid which enforces number of days in a month unlike Time
|
||||
# checks if date part is valid, enforcing days in a month unlike Time
|
||||
Date.new(*time_array[0..2])
|
||||
|
||||
# checks if time part is valid and returns object
|
||||
@ -37,21 +37,12 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
def timeliness_default_error_messages
|
||||
defaults = ActiveRecord::Errors.default_error_messages.slice(:blank, :invalid_date, :before, :on_or_before, :after, :on_or_after)
|
||||
returning({}) do |messages|
|
||||
defaults.each {|k, v| messages["#{k}_message".to_sym] = v }
|
||||
end
|
||||
end
|
||||
|
||||
def validates_timeliness_of(*attr_names)
|
||||
configuration = { :on => :save, :allow_nil => false, :allow_blank => false }
|
||||
configuration = { :on => :save, :type => :time, :allow_nil => false, :allow_blank => false }
|
||||
configuration.update(timeliness_default_error_messages)
|
||||
configuration.update(attr_names.extract_options!)
|
||||
|
||||
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
||||
|
||||
# we need to check raw value for blank or nil to catch when invalid value returns nil
|
||||
# we need to check raw value for blank or nil in cases when an invalid value returns nil
|
||||
allow_nil = configuration.delete(:allow_nil)
|
||||
allow_blank = configuration.delete(:allow_blank)
|
||||
|
||||
@ -69,13 +60,49 @@ module ValidatesTimeliness
|
||||
else
|
||||
unless time = timeliness_date_time_parse(raw_value)
|
||||
record.send("#{attr_name}=", nil)
|
||||
record.errors.add(attr_name, configuration[:invalid_date_message] % column.type)
|
||||
record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type])
|
||||
next
|
||||
end
|
||||
end
|
||||
|
||||
conversion_method = column.type == :date ? :to_date : :to_time
|
||||
time = time.send(conversion_method)
|
||||
validate_timeliness_restrictions(record, attr_name, time, configuration)
|
||||
rescue
|
||||
record.send("#{attr_name}=", nil)
|
||||
record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type])
|
||||
next
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
# Use this validation to force validation of values as Time
|
||||
def validates_time(*attr_names)
|
||||
configuration = attr_names.extract_options!
|
||||
configuration[:type] = :time
|
||||
validates_timeliness_of(attr_names, configuration)
|
||||
end
|
||||
|
||||
# Use this validation to force validation of values as Date
|
||||
def validates_date(*attr_names)
|
||||
configuration = attr_names.extract_options!
|
||||
configuration[:type] = :date
|
||||
validates_timeliness_of(attr_names, configuration)
|
||||
end
|
||||
|
||||
# Use this validation to force validation of values as DateTime
|
||||
def validates_datetime(*attr_names)
|
||||
configuration = attr_names.extract_options!
|
||||
configuration[:type] = :datetime
|
||||
validates_timeliness_of(attr_names, configuration)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_timeliness_restrictions(record, attr_name, value, configuration)
|
||||
restriction_methods = {:before => '<', :after => '>', :on_or_before => '<=', :on_or_after => '>='}
|
||||
|
||||
conversion_method = "to_#{configuration[:type]}".to_sym
|
||||
time = value.send(conversion_method)
|
||||
|
||||
restriction_methods.each do |option, method|
|
||||
if restriction = configuration[option]
|
||||
@ -102,19 +129,15 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
end
|
||||
rescue
|
||||
record.send("#{attr_name}=", nil)
|
||||
record.errors.add(attr_name, configuration[:invalid_date_message] % column.type)
|
||||
next
|
||||
end
|
||||
|
||||
def timeliness_default_error_messages
|
||||
defaults = ActiveRecord::Errors.default_error_messages.slice(:blank, :invalid_date, :before, :on_or_before, :after, :on_or_after)
|
||||
returning({}) do |messages|
|
||||
defaults.each {|k, v| messages["#{k}_message".to_sym] = v }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
alias validates_times validates_timeliness_of
|
||||
alias validates_dates validates_timeliness_of
|
||||
alias validates_datetimes validates_timeliness_of
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@ -4,8 +4,8 @@ describe ValidatesTimeliness::Validations do
|
||||
describe "with no restrictions" do
|
||||
before :all do
|
||||
class BasicValidation < Person
|
||||
validates_timeliness_of :birth_date_and_time, :allow_blank => true
|
||||
validates_timeliness_of :birth_date, :allow_blank => true
|
||||
validates_timeliness_of :birth_date_and_time, :allow_blank => true, :type => :datetime
|
||||
validates_timeliness_of :birth_date, :allow_blank => true, :type => :date
|
||||
end
|
||||
end
|
||||
|
||||
@ -140,7 +140,7 @@ describe ValidatesTimeliness::Validations do
|
||||
describe "with on_or_before and on_or_after restrictions" do
|
||||
before :all do
|
||||
class DateOnOrBeforeAndAfter < Person
|
||||
validates_timeliness_of :birth_date, :on_or_before => 1.day.from_now.to_date, :on_or_after => 1.day.ago.to_date
|
||||
validates_timeliness_of :birth_date, :on_or_before => 1.day.from_now.to_date, :on_or_after => 1.day.ago.to_date, :type => :date
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user