mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-24 23:06:42 +00:00
use global error value formats if missing in locale
always loads globals value formats into class accessor replace old format accessor methods class accessor
This commit is contained in:
parent
c580c3e682
commit
a6712de5ff
@ -25,15 +25,15 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def load_error_messages
|
def load_error_messages
|
||||||
|
defaults = YAML::load(IO.read(LOCALE_PATH))['en']
|
||||||
|
ValidatesTimeliness::Validator.error_value_formats = defaults['validates_timeliness']['error_value_formats'].symbolize_keys
|
||||||
|
|
||||||
if defined?(I18n)
|
if defined?(I18n)
|
||||||
I18n.load_path.unshift(LOCALE_PATH)
|
I18n.load_path.unshift(LOCALE_PATH)
|
||||||
I18n.reload!
|
I18n.reload!
|
||||||
else
|
else
|
||||||
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 }
|
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)
|
::ActiveRecord::Errors.default_error_messages.update(errors)
|
||||||
|
|
||||||
ValidatesTimeliness::Validator.error_value_formats = defaults['validates_timeliness']['error_value_formats'].symbolize_keys
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
module ValidatesTimeliness
|
module ValidatesTimeliness
|
||||||
|
|
||||||
class Validator
|
class Validator
|
||||||
|
cattr_accessor :error_value_formats
|
||||||
cattr_accessor :ignore_restriction_errors
|
cattr_accessor :ignore_restriction_errors
|
||||||
self.ignore_restriction_errors = false
|
self.ignore_restriction_errors = false
|
||||||
|
|
||||||
@ -81,7 +82,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def interpolation_values(option, restriction)
|
def interpolation_values(option, restriction)
|
||||||
format = self.class.error_value_formats[type]
|
format = self.class.error_value_format_for(type)
|
||||||
restriction = [restriction] unless restriction.is_a?(Array)
|
restriction = [restriction] unless restriction.is_a?(Array)
|
||||||
|
|
||||||
if defined?(I18n)
|
if defined?(I18n)
|
||||||
@ -161,18 +162,14 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def error_value_formats
|
def error_value_format_for(type)
|
||||||
if defined?(I18n)
|
if defined?(I18n)
|
||||||
I18n.t('validates_timeliness.error_value_formats')
|
I18n.t(type, :default => error_value_formats[type], :scope => 'validates_timeliness.error_value_formats')
|
||||||
else
|
else
|
||||||
@@error_value_formats
|
error_value_formats[type]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def error_value_formats=(formats)
|
|
||||||
@@error_value_formats = formats
|
|
||||||
end
|
|
||||||
|
|
||||||
def evaluate_option_value(value, type, record)
|
def evaluate_option_value(value, type, record)
|
||||||
case value
|
case value
|
||||||
when Time, Date
|
when Time, Date
|
||||||
|
|||||||
@ -566,6 +566,24 @@ describe ValidatesTimeliness::Validator do
|
|||||||
validate_with(:birth_time, '11:59')
|
validate_with(:birth_time, '11:59')
|
||||||
person.errors.on(:birth_time).should match(/after \d{2}:\d{2}:\d{2}\Z/)
|
person.errors.on(:birth_time).should match(/after \d{2}:\d{2}:\d{2}\Z/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if defined?(I18n)
|
||||||
|
|
||||||
|
describe "I18n" do
|
||||||
|
it "should use global default if locale format missing" do
|
||||||
|
I18n.backend.store_translations 'zz', :activerecord => {:errors => {:messages => { :after => 'after {{restriction}}' }}}
|
||||||
|
I18n.locale = :zz
|
||||||
|
configure_validator(:type => :datetime, :after => 1.day.from_now)
|
||||||
|
validate_with(:birth_date_and_time, Time.now)
|
||||||
|
person.errors.on(:birth_date_and_time).should match(/after \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\Z/)
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
I18n.locale = :en
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "custom formats" do
|
describe "custom formats" do
|
||||||
@ -644,7 +662,7 @@ describe ValidatesTimeliness::Validator do
|
|||||||
|
|
||||||
def error_messages
|
def error_messages
|
||||||
return @error_messages if defined?(@error_messages)
|
return @error_messages if defined?(@error_messages)
|
||||||
messages = validator.send(:error_messages)
|
messages = defined?(I18n) ? I18n.t('activerecord.errors.messages') : validator.send(:error_messages)
|
||||||
@error_messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub(/ (\%s|\{\{\w*\}\}).*/, ''); h }
|
@error_messages = messages.inject({}) {|h, (k, v)| h[k] = v.sub(/ (\%s|\{\{\w*\}\}).*/, ''); h }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user