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:
parent
7dc7bcbb8c
commit
5619789c3d
10 changed files with 57 additions and 19 deletions
|
@ -1,5 +1,9 @@
|
|||
# CHANGELOG
|
||||
|
||||
## 3.0.15
|
||||
|
||||
* added support for localized state names (on a class level, like Record.aasm.states.map(&:localized_name))
|
||||
|
||||
## 3.0.14
|
||||
|
||||
* supporting event inspection for to-states transitions (`Event#transitions_to_state?`)
|
||||
|
|
|
@ -111,7 +111,7 @@ module AASM
|
|||
end
|
||||
|
||||
def aasm_human_state
|
||||
AASM::SupportingClasses::Localizer.new.human_state(self)
|
||||
AASM::SupportingClasses::Localizer.new.human_state_name(self.class, aasm_current_state)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -26,7 +26,7 @@ module AASM
|
|||
def state(name, options={})
|
||||
# @clazz.aasm_state(name, options)
|
||||
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
|
||||
|
||||
@clazz.send(:define_method, "#{name.to_s}?") do
|
||||
|
|
|
@ -29,8 +29,8 @@ module AASM
|
|||
@events = @events.dup
|
||||
end
|
||||
|
||||
def create_state(name, options)
|
||||
@states << AASM::SupportingClasses::State.new(name, options) unless @states.include?(name)
|
||||
def create_state(name, clazz, options)
|
||||
@states << AASM::SupportingClasses::State.new(name, clazz, options) unless @states.include?(name)
|
||||
end
|
||||
|
||||
end # StateMachine
|
||||
|
|
|
@ -9,21 +9,20 @@ module AASM
|
|||
translate_queue(checklist) || I18n.translate(checklist.shift, :default => event.to_s.humanize)
|
||||
end
|
||||
|
||||
def human_state(obj)
|
||||
klass = obj.class
|
||||
def human_state_name(klass, state)
|
||||
checklist = ancestors_list(klass).inject([]) do |list, ancestor|
|
||||
list << item_for(obj, klass, ancestor)
|
||||
list << item_for(obj, klass, ancestor, :old_style => true)
|
||||
list << item_for(klass, state, ancestor)
|
||||
list << item_for(klass, state, ancestor, :old_style => true)
|
||||
list
|
||||
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
|
||||
|
||||
private
|
||||
|
||||
def item_for(obj, klass, ancestor, options={})
|
||||
def item_for(klass, state, ancestor, options={})
|
||||
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
|
||||
|
||||
def translate_queue(checklist)
|
||||
|
|
|
@ -3,8 +3,9 @@ module AASM
|
|||
class State
|
||||
attr_reader :name, :options
|
||||
|
||||
def initialize(name, options={})
|
||||
def initialize(name, clazz, options={})
|
||||
@name = name
|
||||
@clazz = clazz
|
||||
update(options)
|
||||
end
|
||||
|
||||
|
@ -24,6 +25,10 @@ module AASM
|
|||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
name.to_s
|
||||
end
|
||||
|
||||
def fire_callbacks(action, record)
|
||||
action = @options[action]
|
||||
catch :halt_aasm_chain do
|
||||
|
@ -34,7 +39,17 @@ module AASM
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
def for_select
|
||||
|
|
|
@ -6,4 +6,4 @@ en:
|
|||
|
||||
attributes:
|
||||
localizer_test_model:
|
||||
aasm_state/open: "It's opened now!"
|
||||
aasm_state/opened: "It's open now!"
|
||||
|
|
|
@ -7,4 +7,4 @@ en:
|
|||
attributes:
|
||||
localizer_test_model:
|
||||
aasm_state:
|
||||
open: "It's opened now!"
|
||||
opened: "It's open now!"
|
||||
|
|
|
@ -10,7 +10,7 @@ class LocalizerTestModel < ActiveRecord::Base
|
|||
|
||||
attr_accessor :aasm_state
|
||||
|
||||
aasm_initial_state :open
|
||||
aasm_initial_state :opened
|
||||
aasm_state :opened
|
||||
aasm_state :closed
|
||||
|
||||
|
@ -18,6 +18,26 @@ class LocalizerTestModel < ActiveRecord::Base
|
|||
aasm_event :open
|
||||
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
|
||||
before(:all) do
|
||||
I18n.load_path << 'spec/en.yml'
|
||||
|
@ -34,7 +54,7 @@ describe AASM::SupportingClasses::Localizer, "new style" do
|
|||
|
||||
context 'aasm_human_state' 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
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
it 'should return humanized value if not localized' do
|
||||
|
|
|
@ -7,7 +7,7 @@ describe AASM::SupportingClasses::State do
|
|||
end
|
||||
|
||||
def new_state(options={})
|
||||
AASM::SupportingClasses::State.new(@name, @options.merge(options))
|
||||
AASM::SupportingClasses::State.new(@name, Conversation, @options.merge(options))
|
||||
end
|
||||
|
||||
it 'should set the name' do
|
||||
|
|
Loading…
Reference in a new issue