From 5bed56e180bd8ab5855e8ef2436bb6f7a16eeea5 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Sun, 7 Nov 2010 10:59:16 +1100 Subject: [PATCH] fixes for latest I18n interpolation token detection --- lib/validates_timeliness.rb | 14 ++++++++++++-- lib/validates_timeliness/locale/en.new.yml | 18 ++++++++++++++++++ .../locale/{en.yml => en.old.yml} | 0 spec/ginger_scenarios.rb | 2 +- spec/validator_spec.rb | 16 +++++++++++++--- 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 lib/validates_timeliness/locale/en.new.yml rename lib/validates_timeliness/locale/{en.yml => en.old.yml} (100%) diff --git a/lib/validates_timeliness.rb b/lib/validates_timeliness.rb index 4a09111..8207ddf 100644 --- a/lib/validates_timeliness.rb +++ b/lib/validates_timeliness.rb @@ -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 diff --git a/lib/validates_timeliness/locale/en.new.yml b/lib/validates_timeliness/locale/en.new.yml new file mode 100644 index 0000000..a5229f2 --- /dev/null +++ b/lib/validates_timeliness/locale/en.new.yml @@ -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' diff --git a/lib/validates_timeliness/locale/en.yml b/lib/validates_timeliness/locale/en.old.yml similarity index 100% rename from lib/validates_timeliness/locale/en.yml rename to lib/validates_timeliness/locale/en.old.yml diff --git a/spec/ginger_scenarios.rb b/spec/ginger_scenarios.rb index 86f4ced..13bfb3f 100644 --- a/spec/ginger_scenarios.rb +++ b/spec/ginger_scenarios.rb @@ -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}") diff --git a/spec/validator_spec.rb b/spec/validator_spec.rb index 428e8a3..657228b 100644 --- a/spec/validator_spec.rb +++ b/spec/validator_spec.rb @@ -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