1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

added support for localized state names (on a class level, like Record.aasm.states.map(&:localized_name))

This commit is contained in:
Thorsten Böttger 2012-12-27 10:23:41 +13:00
parent 7dc7bcbb8c
commit 5619789c3d
10 changed files with 57 additions and 19 deletions

View file

@ -1,5 +1,9 @@
# CHANGELOG # CHANGELOG
## 3.0.15
* added support for localized state names (on a class level, like Record.aasm.states.map(&:localized_name))
## 3.0.14 ## 3.0.14
* supporting event inspection for to-states transitions (`Event#transitions_to_state?`) * supporting event inspection for to-states transitions (`Event#transitions_to_state?`)

View file

@ -111,7 +111,7 @@ module AASM
end end
def aasm_human_state def aasm_human_state
AASM::SupportingClasses::Localizer.new.human_state(self) AASM::SupportingClasses::Localizer.new.human_state_name(self.class, aasm_current_state)
end end
private private

View file

@ -26,7 +26,7 @@ module AASM
def state(name, options={}) def state(name, options={})
# @clazz.aasm_state(name, options) # @clazz.aasm_state(name, options)
sm = AASM::StateMachine[@clazz] sm = AASM::StateMachine[@clazz]
sm.create_state(name, options) sm.create_state(name, @clazz, options)
sm.initial_state = name if options[:initial] || !sm.initial_state sm.initial_state = name if options[:initial] || !sm.initial_state
@clazz.send(:define_method, "#{name.to_s}?") do @clazz.send(:define_method, "#{name.to_s}?") do

View file

@ -29,8 +29,8 @@ module AASM
@events = @events.dup @events = @events.dup
end end
def create_state(name, options) def create_state(name, clazz, options)
@states << AASM::SupportingClasses::State.new(name, options) unless @states.include?(name) @states << AASM::SupportingClasses::State.new(name, clazz, options) unless @states.include?(name)
end end
end # StateMachine end # StateMachine

View file

@ -9,21 +9,20 @@ module AASM
translate_queue(checklist) || I18n.translate(checklist.shift, :default => event.to_s.humanize) translate_queue(checklist) || I18n.translate(checklist.shift, :default => event.to_s.humanize)
end end
def human_state(obj) def human_state_name(klass, state)
klass = obj.class
checklist = ancestors_list(klass).inject([]) do |list, ancestor| checklist = ancestors_list(klass).inject([]) do |list, ancestor|
list << item_for(obj, klass, ancestor) list << item_for(klass, state, ancestor)
list << item_for(obj, klass, ancestor, :old_style => true) list << item_for(klass, state, ancestor, :old_style => true)
list list
end end
translate_queue(checklist) || I18n.translate(checklist.shift, :default => obj.aasm_current_state.to_s.humanize) translate_queue(checklist) || I18n.translate(checklist.shift, :default => state.to_s.humanize)
end end
private private
def item_for(obj, klass, ancestor, options={}) def item_for(klass, state, ancestor, options={})
separator = options[:old_style] ? '.' : '/' separator = options[:old_style] ? '.' : '/'
:"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}#{separator}#{obj.aasm_current_state}" :"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}#{separator}#{state}"
end end
def translate_queue(checklist) def translate_queue(checklist)

View file

@ -3,8 +3,9 @@ module AASM
class State class State
attr_reader :name, :options attr_reader :name, :options
def initialize(name, options={}) def initialize(name, clazz, options={})
@name = name @name = name
@clazz = clazz
update(options) update(options)
end end
@ -24,6 +25,10 @@ module AASM
end end
end end
def to_s
name.to_s
end
def fire_callbacks(action, record) def fire_callbacks(action, record)
action = @options[action] action = @options[action]
catch :halt_aasm_chain do catch :halt_aasm_chain do
@ -34,7 +39,17 @@ module AASM
end end
def display_name def display_name
@display_name ||= name.to_s.gsub(/_/, ' ').capitalize @display_name ||= begin
if Module.const_defined?(:I18n)
localized_name
else
name.to_s.gsub(/_/, ' ').capitalize
end
end
end
def localized_name
AASM::SupportingClasses::Localizer.new.human_state_name(@clazz, self)
end end
def for_select def for_select

View file

@ -6,4 +6,4 @@ en:
attributes: attributes:
localizer_test_model: localizer_test_model:
aasm_state/open: "It's opened now!" aasm_state/opened: "It's open now!"

View file

@ -7,4 +7,4 @@ en:
attributes: attributes:
localizer_test_model: localizer_test_model:
aasm_state: aasm_state:
open: "It's opened now!" opened: "It's open now!"

View file

@ -10,7 +10,7 @@ class LocalizerTestModel < ActiveRecord::Base
attr_accessor :aasm_state attr_accessor :aasm_state
aasm_initial_state :open aasm_initial_state :opened
aasm_state :opened aasm_state :opened
aasm_state :closed aasm_state :closed
@ -18,6 +18,26 @@ class LocalizerTestModel < ActiveRecord::Base
aasm_event :open aasm_event :open
end end
describe 'localized state names' do
before(:all) do
I18n.load_path << 'spec/en.yml'
I18n.default_locale = :en
I18n.reload!
end
after(:all) do
I18n.load_path.clear
end
it 'should localize' do
LocalizerTestModel.aasm.states.detect {|s| s == :opened}.localized_name.should == "It's open now!"
end
it 'should use fallback' do
LocalizerTestModel.aasm.states.detect {|s| s == :closed}.localized_name.should == 'Closed'
end
end
describe AASM::SupportingClasses::Localizer, "new style" do describe AASM::SupportingClasses::Localizer, "new style" do
before(:all) do before(:all) do
I18n.load_path << 'spec/en.yml' I18n.load_path << 'spec/en.yml'
@ -34,7 +54,7 @@ describe AASM::SupportingClasses::Localizer, "new style" do
context 'aasm_human_state' do context 'aasm_human_state' do
it 'should return translated state value' do it 'should return translated state value' do
foo_opened.aasm_human_state.should == "It's opened now!" foo_opened.aasm_human_state.should == "It's open now!"
end end
it 'should return humanized value if not localized' do it 'should return humanized value if not localized' do
@ -69,7 +89,7 @@ describe AASM::SupportingClasses::Localizer, "deprecated style" do
context 'aasm_human_state' do context 'aasm_human_state' do
it 'should return translated state value' do it 'should return translated state value' do
foo_opened.aasm_human_state.should == "It's opened now!" foo_opened.aasm_human_state.should == "It's open now!"
end end
it 'should return humanized value if not localized' do it 'should return humanized value if not localized' do

View file

@ -7,7 +7,7 @@ describe AASM::SupportingClasses::State do
end end
def new_state(options={}) def new_state(options={})
AASM::SupportingClasses::State.new(@name, @options.merge(options)) AASM::SupportingClasses::State.new(@name, Conversation, @options.merge(options))
end end
it 'should set the name' do it 'should set the name' do