mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
refactored error value formats to use locale file for I18n. Rail 2.0/2.1 to use default_error_value_formats now.
moved default_error_messages_method into validator
This commit is contained in:
parent
a836ed8434
commit
7967b5a212
12
README.rdoc
12
README.rdoc
@ -302,12 +302,22 @@ will be inserted.
|
||||
And for something a little more specific you can override the format of the interpolation
|
||||
values inserted in the error messages for temporal restrictions like so
|
||||
|
||||
ValidatesTimeliness::Validator.error_value_formats.update(
|
||||
For Rails 2.0/2.1:
|
||||
|
||||
ValidatesTimeliness::Validator.default_error_value_formats.update(
|
||||
:time => '%H:%M:%S',
|
||||
:date => '%Y-%m-%d',
|
||||
:datetime => '%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
Rails 2.2+ using the I18n system to define new defaults:
|
||||
|
||||
validates_timeliness:
|
||||
error_value_formats:
|
||||
date: '%Y-%m-%d'
|
||||
time: '%H:%M:%S'
|
||||
datetime: '%Y-%m-%d %H:%M:%S'
|
||||
|
||||
Those are Ruby strftime formats not the plugin formats.
|
||||
|
||||
|
||||
|
||||
@ -34,20 +34,14 @@ module ValidatesTimeliness
|
||||
I18n.load_path += [ LOCALE_PATH ]
|
||||
I18n.reload!
|
||||
else
|
||||
messages = YAML::load(IO.read(LOCALE_PATH))
|
||||
errors = messages['en']['activerecord']['errors']['messages'].inject({}) {|h,(k,v)| h[k.to_sym] = v.gsub(/\{\{\w*\}\}/, '%s');h }
|
||||
defaults = YAML::load(IO.read(LOCALE_PATH))['en']
|
||||
errors = defaults['activerecord']['errors']['messages'].inject({}) {|h,(k,v)| h[k.to_sym] = v.gsub(/\{\{\w*\}\}/, '%s');h }
|
||||
::ActiveRecord::Errors.default_error_messages.update(errors)
|
||||
|
||||
ValidatesTimeliness::Validator.default_error_value_formats = defaults['validates_timeliness']['error_value_formats'].symbolize_keys
|
||||
end
|
||||
end
|
||||
|
||||
def default_error_messages
|
||||
if Rails::VERSION::STRING < '2.2'
|
||||
::ActiveRecord::Errors.default_error_messages
|
||||
else
|
||||
I18n.translate('activerecord.errors.messages')
|
||||
end
|
||||
end
|
||||
|
||||
def setup_for_rails
|
||||
self.default_timezone = ::ActiveRecord::Base.default_timezone
|
||||
self.use_time_zones = ::ActiveRecord::Base.time_zone_aware_attributes rescue false
|
||||
|
||||
@ -11,3 +11,8 @@ en:
|
||||
after: "must be after {{restriction}}"
|
||||
on_or_after: "must be on or after {{restriction}}"
|
||||
between: "must be between {{earliest}} and {{latest}}"
|
||||
validates_timeliness:
|
||||
error_value_formats:
|
||||
date: '%Y-%m-%d'
|
||||
time: '%H:%M:%S'
|
||||
datetime: '%Y-%m-%d %H:%M:%S'
|
||||
|
||||
@ -116,7 +116,7 @@ module Spec
|
||||
end
|
||||
|
||||
def error_message_for(option)
|
||||
msg = @validator.send(:error_messages)[option]
|
||||
msg = @validator.error_messages[option]
|
||||
restriction = @validator.class.send(:evaluate_option_value, @validator.configuration[option], @type, @record)
|
||||
|
||||
if restriction
|
||||
@ -135,7 +135,7 @@ module Spec
|
||||
|
||||
def format_value(value)
|
||||
return value if value.is_a?(String)
|
||||
value.strftime(ValidatesTimeliness::Validator.error_value_formats[@type])
|
||||
value.strftime(@validator.class.error_value_formats[@type])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -2,14 +2,9 @@ module ValidatesTimeliness
|
||||
|
||||
class Validator
|
||||
cattr_accessor :ignore_restriction_errors
|
||||
cattr_accessor :error_value_formats
|
||||
cattr_accessor :default_error_value_formats
|
||||
|
||||
self.ignore_restriction_errors = false
|
||||
self.error_value_formats = {
|
||||
:time => '%H:%M:%S',
|
||||
:date => '%Y-%m-%d',
|
||||
:datetime => '%Y-%m-%d %H:%M:%S'
|
||||
}
|
||||
|
||||
RESTRICTION_METHODS = {
|
||||
:equal_to => :==,
|
||||
@ -47,7 +42,11 @@ module ValidatesTimeliness
|
||||
|
||||
validate_restrictions(record, attr_name, value)
|
||||
end
|
||||
|
||||
|
||||
def error_messages
|
||||
@error_messages ||= self.class.default_error_messages.merge(custom_error_messages)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def raw_value(record, attr_name)
|
||||
@ -120,10 +119,6 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
def error_messages
|
||||
@error_messages ||= ValidatesTimeliness.default_error_messages.merge(custom_error_messages)
|
||||
end
|
||||
|
||||
def custom_error_messages
|
||||
@custom_error_messages ||= configuration.inject({}) {|msgs, (k, v)|
|
||||
if md = /(.*)_message$/.match(k.to_s)
|
||||
@ -132,7 +127,7 @@ module ValidatesTimeliness
|
||||
msgs
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
def combine_date_and_time(value, record)
|
||||
if type == :date
|
||||
date = value
|
||||
@ -156,6 +151,22 @@ module ValidatesTimeliness
|
||||
# class methods
|
||||
class << self
|
||||
|
||||
def default_error_messages
|
||||
if Rails::VERSION::STRING < '2.2'
|
||||
::ActiveRecord::Errors.default_error_messages
|
||||
else
|
||||
I18n.translate('activerecord.errors.messages')
|
||||
end
|
||||
end
|
||||
|
||||
def error_value_formats
|
||||
if defined?(I18n)
|
||||
I18n.translate('validates_timeliness.error_value_formats')
|
||||
else
|
||||
default_error_value_formats
|
||||
end
|
||||
end
|
||||
|
||||
def evaluate_option_value(value, type, record)
|
||||
case value
|
||||
when Time, Date
|
||||
|
||||
@ -554,12 +554,18 @@ describe ValidatesTimeliness::Validator do
|
||||
describe "custom formats" do
|
||||
|
||||
before :all do
|
||||
@@formats = ValidatesTimeliness::Validator.error_value_formats
|
||||
ValidatesTimeliness::Validator.error_value_formats = {
|
||||
custom = {
|
||||
:time => '%H:%M %p',
|
||||
:date => '%d-%m-%Y',
|
||||
:datetime => '%d-%m-%Y %H:%M %p'
|
||||
}
|
||||
|
||||
if defined?(I18n)
|
||||
I18n.backend.store_translations 'en', :validates_timeliness => { :error_value_formats => custom }
|
||||
else
|
||||
@@formats = ValidatesTimeliness::Validator.default_error_value_formats
|
||||
ValidatesTimeliness::Validator.default_error_value_formats = custom
|
||||
end
|
||||
end
|
||||
|
||||
it "should format datetime value of restriction" do
|
||||
@ -581,7 +587,11 @@ describe ValidatesTimeliness::Validator do
|
||||
end
|
||||
|
||||
after :all do
|
||||
ValidatesTimeliness::Validator.error_value_formats = @@formats
|
||||
if defined?(I18n)
|
||||
I18n.reload!
|
||||
else
|
||||
ValidatesTimeliness::Validator.default_error_value_formats = @@formats
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user