2011-02-12 23:10:39 +07:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'active_record'
|
|
|
|
require 'logger'
|
|
|
|
require 'i18n'
|
|
|
|
|
2012-12-27 09:51:01 +13:00
|
|
|
load_schema
|
|
|
|
|
2011-09-10 18:41:48 +02:00
|
|
|
class LocalizerTestModel < ActiveRecord::Base
|
2011-02-12 23:10:39 +07:00
|
|
|
include AASM
|
2011-07-04 02:09:17 +07:00
|
|
|
|
2011-02-12 23:10:39 +07:00
|
|
|
attr_accessor :aasm_state
|
|
|
|
|
2013-11-30 21:30:07 +01:00
|
|
|
aasm do
|
2013-12-01 00:23:34 +01:00
|
|
|
state :opened, :initial => true
|
2013-11-30 21:30:07 +01:00
|
|
|
state :closed
|
|
|
|
event :close
|
|
|
|
event :open
|
|
|
|
end
|
2011-02-12 23:10:39 +07:00
|
|
|
end
|
|
|
|
|
2012-12-27 10:23:41 +13:00
|
|
|
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
|
2014-01-11 12:43:10 +01:00
|
|
|
state = LocalizerTestModel.aasm.states.detect {|s| s == :opened}
|
|
|
|
state.localized_name.should == "It's open now!"
|
|
|
|
state.human_name.should == "It's open now!"
|
2012-12-27 10:23:41 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use fallback' do
|
2014-01-11 12:43:10 +01:00
|
|
|
state = LocalizerTestModel.aasm.states.detect {|s| s == :closed}
|
|
|
|
state.localized_name.should == 'Closed'
|
|
|
|
state.human_name.should == 'Closed'
|
2012-12-27 10:23:41 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-22 16:02:40 +13:00
|
|
|
describe AASM::Localizer, "new style" do
|
2011-02-12 23:10:39 +07:00
|
|
|
before(:all) do
|
|
|
|
I18n.load_path << 'spec/en.yml'
|
|
|
|
I18n.default_locale = :en
|
2012-01-16 17:04:03 +01:00
|
|
|
I18n.reload!
|
2011-02-12 23:10:39 +07:00
|
|
|
end
|
2011-07-04 02:09:17 +07:00
|
|
|
|
2012-01-16 17:04:03 +01:00
|
|
|
after(:all) do
|
|
|
|
I18n.load_path.clear
|
|
|
|
end
|
2011-02-12 23:10:39 +07:00
|
|
|
|
2011-09-10 18:41:48 +02:00
|
|
|
let (:foo_opened) { LocalizerTestModel.new }
|
|
|
|
let (:foo_closed) { LocalizerTestModel.new.tap { |x| x.aasm_state = :closed } }
|
2011-02-12 23:10:39 +07:00
|
|
|
|
2013-11-30 20:56:50 +01:00
|
|
|
context 'aasm.human_state' do
|
2011-02-12 23:10:39 +07:00
|
|
|
it 'should return translated state value' do
|
2013-11-30 20:56:50 +01:00
|
|
|
foo_opened.aasm.human_state.should == "It's open now!"
|
2011-02-12 23:10:39 +07:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return humanized value if not localized' do
|
2013-11-30 20:56:50 +01:00
|
|
|
foo_closed.aasm.human_state.should == "Closed"
|
2011-02-12 23:10:39 +07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-26 18:56:37 +01:00
|
|
|
context 'aasm_human_event_name' do
|
2011-02-12 23:10:39 +07:00
|
|
|
it 'should return translated event name' do
|
2011-11-26 18:56:37 +01:00
|
|
|
LocalizerTestModel.aasm_human_event_name(:close).should == "Let's close it!"
|
2011-02-12 23:10:39 +07:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return humanized event name' do
|
2011-11-26 18:56:37 +01:00
|
|
|
LocalizerTestModel.aasm_human_event_name(:open).should == "Open"
|
2011-02-12 23:10:39 +07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-06 22:32:52 +12:00
|
|
|
|
2013-02-22 16:02:40 +13:00
|
|
|
describe AASM::Localizer, "deprecated style" do
|
2012-06-06 22:32:52 +12:00
|
|
|
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 } }
|
|
|
|
|
2013-11-30 20:56:50 +01:00
|
|
|
context 'aasm.human_state' do
|
2012-06-06 22:32:52 +12:00
|
|
|
it 'should return translated state value' do
|
2013-11-30 20:56:50 +01:00
|
|
|
foo_opened.aasm.human_state.should == "It's open now!"
|
2012-06-06 22:32:52 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return humanized value if not localized' do
|
2013-11-30 20:56:50 +01:00
|
|
|
foo_closed.aasm.human_state.should == "Closed"
|
2012-06-06 22:32:52 +12:00
|
|
|
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
|