mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
removed deprecation warning when localizing aasm state names (look at https://github.com/rubyist/aasm/issues/38 for details)
This commit is contained in:
parent
8190c26bde
commit
d8a4417671
5 changed files with 76 additions and 13 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -6,5 +6,4 @@ en:
|
|||
|
||||
attributes:
|
||||
localizer_test_model:
|
||||
aasm_state:
|
||||
open: "It's opened now!"
|
||||
aasm_state/open: "It's opened now!"
|
||||
|
|
10
spec/en_deprecated_style.yml
Normal file
10
spec/en_deprecated_style.yml
Normal file
|
@ -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!"
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue