class ActiveModelTest::MachineWithInternationalizationTest

Public Instance Methods

setup() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 985
def setup
  I18n.backend = I18n::Backend::Simple.new
  
  # Initialize the backend
  I18n.backend.translate(:en, 'activemodel.errors.messages.invalid_transition', :event => 'ignite', :value => 'idling')
  
  @model = new_model { include ActiveModel::Validations }
end
test_should_add_locale_to_beginning_of_load_path() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1141
def test_should_add_locale_to_beginning_of_load_path
  @original_load_path = I18n.load_path
  I18n.backend = I18n::Backend::Simple.new
  
  app_locale = File.dirname(__FILE__) + '/../../files/en.yml'
  default_locale = File.dirname(__FILE__) + '/../../../lib/state_machine/integrations/active_model/locale.rb'
  I18n.load_path = [app_locale]
  
  StateMachine::Machine.new(@model)
  
  assert_equal [default_locale, app_locale].map {|path| File.expand_path(path)}, I18n.load_path.map {|path| File.expand_path(path)}
ensure
  I18n.load_path = @original_load_path
end
test_should_allow_customized_error_key() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1009
def test_should_allow_customized_error_key
  I18n.backend.store_translations(:en, {
    :activemodel => {:errors => {:messages => {:bad_transition => 'cannot %{event}'}}}
  })
  
  machine = StateMachine::Machine.new(@model, :action => :save, :messages => {:invalid_transition => :bad_transition})
  machine.state :parked, :idling
  
  record = @model.new
  record.state = 'idling'
  
  machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
  assert_equal ['State cannot ignite'], record.errors.full_messages
end
test_should_allow_customized_error_string() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1024
def test_should_allow_customized_error_string
  machine = StateMachine::Machine.new(@model, :action => :save, :messages => {:invalid_transition => 'cannot %{event}'})
  machine.state :parked, :idling
  
  record = @model.new(:state => 'idling')
  
  machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
  assert_equal ['State cannot ignite'], record.errors.full_messages
end
test_should_allow_customized_event_key_scoped_to_class() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1099
def test_should_allow_customized_event_key_scoped_to_class
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:'active_model_test/foo' => {:events => {:park => 'stop'}}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  machine.event :park
  
  assert_equal 'stop', machine.event(:park).human_name
end
test_should_allow_customized_event_key_scoped_to_class_and_machine() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1088
def test_should_allow_customized_event_key_scoped_to_class_and_machine
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:'active_model_test/foo' => {:state => {:events => {:park => 'stop'}}}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  machine.event :park
  
  assert_equal 'stop', machine.event(:park).human_name
end
test_should_allow_customized_event_key_scoped_to_machine() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1110
def test_should_allow_customized_event_key_scoped_to_machine
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:state => {:events => {:park => 'stop'}}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  machine.event :park
  
  assert_equal 'stop', machine.event(:park).human_name
end
test_should_allow_customized_event_key_unscoped() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1121
def test_should_allow_customized_event_key_unscoped
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:events => {:park => 'stop'}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  machine.event :park
  
  assert_equal 'stop', machine.event(:park).human_name
end
test_should_allow_customized_state_key_scoped_to_class() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1045
def test_should_allow_customized_state_key_scoped_to_class
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:'active_model_test/foo' => {:states => {:parked => 'shutdown'}}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  machine.state :parked
  
  assert_equal 'shutdown', machine.state(:parked).human_name
end
test_should_allow_customized_state_key_scoped_to_class_and_machine() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1034
def test_should_allow_customized_state_key_scoped_to_class_and_machine
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:'active_model_test/foo' => {:state => {:states => {:parked => 'shutdown'}}}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  machine.state :parked
  
  assert_equal 'shutdown', machine.state(:parked).human_name
end
test_should_allow_customized_state_key_scoped_to_machine() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1056
def test_should_allow_customized_state_key_scoped_to_machine
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:state => {:states => {:parked => 'shutdown'}}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  machine.state :parked
  
  assert_equal 'shutdown', machine.state(:parked).human_name
end
test_should_allow_customized_state_key_unscoped() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1067
def test_should_allow_customized_state_key_unscoped
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:states => {:parked => 'shutdown'}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  machine.state :parked
  
  assert_equal 'shutdown', machine.state(:parked).human_name
end
test_should_only_add_locale_once_in_load_path() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1132
def test_should_only_add_locale_once_in_load_path
  assert_equal 1, I18n.load_path.select {|path| path =~ %r{active_model/locale\.rb$}}.length
  
  # Create another ActiveModel model that will triger the i18n feature
  new_model
  
  assert_equal 1, I18n.load_path.select {|path| path =~ %r{active_model/locale\.rb$}}.length
end
test_should_prefer_other_locales_first() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1156
def test_should_prefer_other_locales_first
  @original_load_path = I18n.load_path
  I18n.backend = I18n::Backend::Simple.new
  I18n.load_path = [File.dirname(__FILE__) + '/../../files/en.yml']
  
  machine = StateMachine::Machine.new(@model)
  machine.state :parked, :idling
  machine.event :ignite
  
  record = @model.new(:state => 'idling')
  
  machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
  assert_equal ['State cannot ignite'], record.errors.full_messages
ensure
  I18n.load_path = @original_load_path
end
test_should_support_nil_state_key() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 1078
def test_should_support_nil_state_key
  I18n.backend.store_translations(:en, {
    :activemodel => {:state_machines => {:states => {:nil => 'empty'}}}
  })
  
  machine = StateMachine::Machine.new(@model)
  
  assert_equal 'empty', machine.state(nil).human_name
end
test_should_use_defaults() click to toggle source
# File test/unit/integrations/active_model_test.rb, line 994
def test_should_use_defaults
  I18n.backend.store_translations(:en, {
    :activemodel => {:errors => {:messages => {:invalid_transition => 'cannot %{event}'}}}
  })
  
  machine = StateMachine::Machine.new(@model, :action => :save)
  machine.state :parked, :idling
  machine.event :ignite
  
  record = @model.new(:state => 'idling')
  
  machine.invalidate(record, :state, :invalid_transition, [[:event, 'ignite']])
  assert_equal ['State cannot ignite'], record.errors.full_messages
end