Skip to content

Commit

Permalink
removed deprecation warning when localizing aasm state names (look at h…
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Jun 6, 2012
1 parent 8190c26 commit d8a4417
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
35 changes: 25 additions & 10 deletions lib/aasm/supporting_classes/localizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions spec/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ en:

attributes:
localizer_test_model:
aasm_state:
open: "It's opened now!"
aasm_state/open: "It's opened now!"
10 changes: 10 additions & 0 deletions spec/en_deprecated_style.yml
Original file line number Diff line number Diff line change
@@ -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!"
37 changes: 36 additions & 1 deletion spec/unit/localizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,3 +50,38 @@ class LocalizerTestModel < ActiveRecord::Base
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

0 comments on commit d8a4417

Please sign in to comment.