renamed error message key

This commit is contained in:
Adam Meehan 2008-07-11 16:37:37 +10:00
parent 89d6d4ac14
commit eafce02a73

View File

@ -8,11 +8,11 @@ module ValidatesTimeliness
base.extend ClassMethods base.extend ClassMethods
error_messages = { error_messages = {
:invalid_date => "is not a valid %s", :invalid_datetime => "is not a valid %s",
:before => "must be before %s", :before => "must be before %s",
:on_or_before => "must be on or before %s", :on_or_before => "must be on or before %s",
:after => "must be after %s", :after => "must be after %s",
:on_or_after => "must be on or after %s" :on_or_after => "must be on or after %s"
} }
ActiveRecord::Errors.default_error_messages.update(error_messages) ActiveRecord::Errors.default_error_messages.update(error_messages)
@ -24,17 +24,15 @@ module ValidatesTimeliness
# Chronic. Just return nil for an invalid value and a Time object for a # Chronic. Just return nil for an invalid value and a Time object for a
# valid parsed value. # valid parsed value.
# #
# Remember, if the date portion is pre the Unix epoch the return object # Remember Rails, since version 2, will automatically handle the fallback
# will need to be a datetime. But luckily Rails, since version 2, will # to a DateTime when you create a time which is out of range.
# automatically handle the fall back to a DateTime when you create a time
# which is out of range.
def timeliness_date_time_parse(raw_value, type) def timeliness_date_time_parse(raw_value, type)
return raw_value.to_time if raw_value.acts_like?(:time) || raw_value.is_a?(Date) 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)
if type == :time if type == :time
# Rails dummy time date part is 2000-01-01 # Rails dummy time date part is defined as 2000-01-01
time_array[0..2] = 2000, 1, 1 time_array[0..2] = 2000, 1, 1
else else
# Date.new enforces days per month, unlike Time # Date.new enforces days per month, unlike Time
@ -65,14 +63,14 @@ module ValidatesTimeliness
begin begin
unless time = timeliness_date_time_parse(raw_value, configuration[:type]) unless time = timeliness_date_time_parse(raw_value, configuration[:type])
record.send("#{attr_name}=", nil) record.send("#{attr_name}=", nil)
record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type]) record.errors.add(attr_name, configuration[:invalid_datetime_message] % configuration[:type])
next next
end end
validate_timeliness_restrictions(record, attr_name, time, configuration) validate_timeliness_restrictions(record, attr_name, time, configuration)
rescue Exception => e rescue Exception => e
record.send("#{attr_name}=", nil) record.send("#{attr_name}=", nil)
record.errors.add(attr_name, configuration[:invalid_date_message] % configuration[:type]) record.errors.add(attr_name, configuration[:invalid_datetime_message] % configuration[:type])
next next
end end
@ -102,6 +100,8 @@ module ValidatesTimeliness
private private
# Validate value against the restrictions. Restriction values maybe of
# mixed type so convert them all to common type as defined by type param.
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 +138,7 @@ module ValidatesTimeliness
end end
def timeliness_default_error_messages def timeliness_default_error_messages
defaults = ActiveRecord::Errors.default_error_messages.slice(:blank, :invalid_date, :before, :on_or_before, :after, :on_or_after) defaults = ActiveRecord::Errors.default_error_messages.slice(:blank, :invalid_datetime, :before, :on_or_before, :after, :on_or_after)
returning({}) do |messages| returning({}) do |messages|
defaults.each {|k, v| messages["#{k}_message".to_sym] = v } defaults.each {|k, v| messages["#{k}_message".to_sym] = v }
end end