mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
fixes for latest I18n interpolation token detection
This commit is contained in:
parent
c3a591a5d8
commit
5bed56e180
@ -2,10 +2,18 @@ require 'validates_timeliness/formats'
|
||||
require 'validates_timeliness/parser'
|
||||
require 'validates_timeliness/validator'
|
||||
require 'validates_timeliness/validation_methods'
|
||||
|
||||
require 'validates_timeliness/active_record/attribute_methods'
|
||||
require 'validates_timeliness/active_record/multiparameter_attributes'
|
||||
require 'validates_timeliness/action_view/instance_tag'
|
||||
begin
|
||||
i18n_path = $:.grep(/active_support\/vendor\/i18n-/)
|
||||
if i18n_path.empty?
|
||||
require 'i18n/version'
|
||||
else
|
||||
require i18n_path[0] + '/version'
|
||||
end
|
||||
rescue LoadError
|
||||
end if defined?(I18n)
|
||||
|
||||
module ValidatesTimeliness
|
||||
|
||||
@ -15,7 +23,9 @@ module ValidatesTimeliness
|
||||
mattr_accessor :use_time_zones
|
||||
self.use_time_zones = false
|
||||
|
||||
LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/validates_timeliness/locale/en.yml')
|
||||
I18N_LATEST = defined?(I18n::VERSION) && I18n::VERSION >= '0.4.0'
|
||||
locale_file = I18N_LATEST ? 'en.new.yml' : 'en.old.yml'
|
||||
LOCALE_PATH = File.expand_path(File.join(File.dirname(__FILE__),'validates_timeliness','locale',locale_file))
|
||||
|
||||
class << self
|
||||
|
||||
|
||||
18
lib/validates_timeliness/locale/en.new.yml
Normal file
18
lib/validates_timeliness/locale/en.new.yml
Normal file
@ -0,0 +1,18 @@
|
||||
en:
|
||||
activerecord:
|
||||
errors:
|
||||
messages:
|
||||
invalid_date: "is not a valid date"
|
||||
invalid_time: "is not a valid time"
|
||||
invalid_datetime: "is not a valid datetime"
|
||||
is_at: "must be at %{restriction}"
|
||||
before: "must be before %{restriction}"
|
||||
on_or_before: "must be on or before %{restriction}"
|
||||
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'
|
||||
@ -9,7 +9,7 @@
|
||||
# ginger spec
|
||||
#
|
||||
Ginger.configure do |config|
|
||||
rails_versions = ['2.0.2', '2.1.2', '2.2.2', '2.3.3', '2.3.4', '2.3.5']
|
||||
rails_versions = ['2.0.2', '2.1.2', '2.2.2', '2.3.3', '2.3.4', '2.3.5', '2.3.6', '2.3.9']
|
||||
|
||||
rails_versions.each do |v|
|
||||
g = Ginger::Scenario.new("Rails #{v}")
|
||||
|
||||
@ -3,6 +3,14 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
||||
describe ValidatesTimeliness::Validator do
|
||||
attr_accessor :person, :validator
|
||||
|
||||
if ValidatesTimeliness::I18N_LATEST
|
||||
I18N_REGEXP = /\%\{\w*\}/
|
||||
I18N_INTERPOLATION = '%{%s}'
|
||||
else
|
||||
I18N_REGEXP = /\{\{\w*\}\}/
|
||||
I18N_INTERPOLATION = '{{%s}}'
|
||||
end
|
||||
|
||||
before :all do
|
||||
# freezes time using time_travel plugin
|
||||
Time.now = Time.utc(2000, 1, 1, 0, 0, 0)
|
||||
@ -513,8 +521,9 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
describe "localized error messages" do
|
||||
before(:all) do
|
||||
message = "retfa #{I18N_INTERPOLATION}" % 'restriction'
|
||||
translations = {
|
||||
:activerecord => {:errors => {:messages => { :after => 'retfa {{restriction}}' }}},
|
||||
:activerecord => {:errors => {:messages => { :after => message }}},
|
||||
:validates_timeliness => {:error_value_formats => {}}
|
||||
}
|
||||
I18n.backend.store_translations 'zz', translations
|
||||
@ -616,7 +625,8 @@ describe ValidatesTimeliness::Validator do
|
||||
|
||||
describe "I18n" do
|
||||
it "should use global default if locale format missing" do
|
||||
I18n.backend.store_translations 'zz', :activerecord => {:errors => {:messages => { :after => 'after {{restriction}}' }}}
|
||||
message = "after #{I18N_INTERPOLATION}" % 'restriction'
|
||||
I18n.backend.store_translations 'zz', :activerecord => {:errors => {:messages => { :after => message }}}
|
||||
I18n.locale = :zz
|
||||
configure_validator(:type => :datetime, :after => 1.day.from_now)
|
||||
validate_with(:birth_date_and_time, Time.now)
|
||||
@ -708,6 +718,6 @@ describe ValidatesTimeliness::Validator do
|
||||
def error_messages
|
||||
return @error_messages if defined?(@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|#{I18N_REGEXP}).*/, ''); h }
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user