From d8a4417671b01f835ece97bfd7bc061e00cecadd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20B=C3=B6ttger?= Date: Wed, 6 Jun 2012 22:32:52 +1200 Subject: [PATCH] removed deprecation warning when localizing aasm state names (look at https://github.com/rubyist/aasm/issues/38 for details) --- CHANGELOG.md | 4 +++ lib/aasm/supporting_classes/localizer.rb | 35 +++++++++++++++------- spec/en.yml | 3 +- spec/en_deprecated_style.yml | 10 +++++++ spec/unit/localizer_spec.rb | 37 +++++++++++++++++++++++- 5 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 spec/en_deprecated_style.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e8a71a..934424c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 3.0.7 (not yet released) + + * removed deprecation warning when localizing aasm state names (look at https://github.com/rubyist/aasm/issues/38 for details) + ## 3.0.6 * bugfix: if configured to skip validation the code does not validate anymore diff --git a/lib/aasm/supporting_classes/localizer.rb b/lib/aasm/supporting_classes/localizer.rb index d211bf7..64a2aad 100644 --- a/lib/aasm/supporting_classes/localizer.rb +++ b/lib/aasm/supporting_classes/localizer.rb @@ -2,20 +2,35 @@ module AASM module SupportingClasses class Localizer def human_event_name(klass, event) - defaults = ancestors_list(klass).map do |ancestor| - :"#{i18n_scope(klass)}.events.#{i18n_klass(ancestor)}.#{event}" - end << event.to_s.humanize - - I18n.translate(defaults.shift, :default => defaults, :raise => true) + checklist = ancestors_list(klass).inject([]) do |list, ancestor| + list << :"#{i18n_scope(klass)}.events.#{i18n_klass(ancestor)}.#{event}" + list + end + (0...(checklist.size-1)).each do |i| + begin + return I18n.translate(checklist.shift, :raise => true) + rescue I18n::MissingTranslationData + # that's okay + end + end + I18n.translate(checklist.shift, :default => event.to_s.humanize) end def human_state(obj) klass = obj.class - defaults = ancestors_list(klass).map do |ancestor| - :"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}.#{obj.aasm_current_state}" - end << obj.aasm_current_state.to_s.humanize - - I18n.translate(defaults.shift, :default => defaults, :raise => true) + checklist = ancestors_list(klass).inject([]) do |list, ancestor| + list << :"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}/#{obj.aasm_current_state}" + list << :"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}.#{obj.aasm_current_state}" + list + end + (0...(checklist.size-1)).each do |i| + begin + return I18n.translate(checklist.shift, :raise => true) + rescue I18n::MissingTranslationData + # that's okay + end + end + I18n.translate(checklist.shift, :default => obj.aasm_current_state.to_s.humanize) end private diff --git a/spec/en.yml b/spec/en.yml index 38ab0bb..31d10b6 100644 --- a/spec/en.yml +++ b/spec/en.yml @@ -6,5 +6,4 @@ en: attributes: localizer_test_model: - aasm_state: - open: "It's opened now!" + aasm_state/open: "It's opened now!" diff --git a/spec/en_deprecated_style.yml b/spec/en_deprecated_style.yml new file mode 100644 index 0000000..38ab0bb --- /dev/null +++ b/spec/en_deprecated_style.yml @@ -0,0 +1,10 @@ +en: + activerecord: + events: + localizer_test_model: + close: "Let's close it!" + + attributes: + localizer_test_model: + aasm_state: + open: "It's opened now!" diff --git a/spec/unit/localizer_spec.rb b/spec/unit/localizer_spec.rb index 307b198..a59cc20 100644 --- a/spec/unit/localizer_spec.rb +++ b/spec/unit/localizer_spec.rb @@ -16,7 +16,7 @@ class LocalizerTestModel < ActiveRecord::Base aasm_event :open end -describe AASM::SupportingClasses::Localizer do +describe AASM::SupportingClasses::Localizer, "new style" do before(:all) do I18n.load_path << 'spec/en.yml' I18n.default_locale = :en @@ -50,3 +50,38 @@ describe AASM::SupportingClasses::Localizer do end end end + +describe AASM::SupportingClasses::Localizer, "deprecated style" do + before(:all) do + I18n.load_path << 'spec/en_deprecated_style.yml' + I18n.default_locale = :en + I18n.reload! + end + + after(:all) do + I18n.load_path.clear + end + + let (:foo_opened) { LocalizerTestModel.new } + let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } } + + context 'aasm_human_state' do + it 'should return translated state value' do + foo_opened.aasm_human_state.should == "It's opened now!" + end + + it 'should return humanized value if not localized' do + foo_closed.aasm_human_state.should == "Closed" + end + end + + context 'aasm_human_event_name' do + it 'should return translated event name' do + LocalizerTestModel.aasm_human_event_name(:close).should == "Let's close it!" + end + + it 'should return humanized event name' do + LocalizerTestModel.aasm_human_event_name(:open).should == "Open" + end + end +end