2008-04-29 01:27:56 -04:00
|
|
|
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'aasm')
|
|
|
|
|
|
|
|
begin
|
2008-05-29 16:23:53 -04:00
|
|
|
require 'rubygems'
|
2008-04-29 01:27:56 -04:00
|
|
|
require 'active_record'
|
|
|
|
|
|
|
|
# A dummy class for mocking the activerecord connection class
|
|
|
|
class Connection
|
|
|
|
end
|
|
|
|
|
2008-05-29 16:23:53 -04:00
|
|
|
class FooBar < ActiveRecord::Base
|
2008-04-29 01:27:56 -04:00
|
|
|
include AASM
|
|
|
|
|
|
|
|
# Fake this column for testing purposes
|
|
|
|
attr_accessor :aasm_state
|
|
|
|
|
|
|
|
aasm_state :open
|
|
|
|
aasm_state :closed
|
|
|
|
|
|
|
|
aasm_event :view do
|
|
|
|
transitions :to => :read, :from => [:needs_attention]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Fi < ActiveRecord::Base
|
|
|
|
def aasm_read_state
|
|
|
|
"fi"
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
include AASM
|
|
|
|
end
|
|
|
|
|
|
|
|
class Fo < ActiveRecord::Base
|
|
|
|
def aasm_write_state(state)
|
|
|
|
"fo"
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
include AASM
|
|
|
|
end
|
|
|
|
|
|
|
|
class Fum < ActiveRecord::Base
|
|
|
|
def aasm_write_state_without_persistence(state)
|
|
|
|
"fum"
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
include AASM
|
|
|
|
end
|
|
|
|
|
2008-06-22 11:17:12 -04:00
|
|
|
class June < ActiveRecord::Base
|
|
|
|
include AASM
|
|
|
|
aasm_column :status
|
|
|
|
end
|
2008-11-15 01:41:46 -05:00
|
|
|
|
2008-06-22 11:17:12 -04:00
|
|
|
class Beaver < June
|
|
|
|
end
|
2009-05-27 20:32:02 -04:00
|
|
|
|
2009-02-26 12:06:43 -05:00
|
|
|
class Thief < ActiveRecord::Base
|
|
|
|
include AASM
|
|
|
|
aasm_initial_state Proc.new { |thief| thief.skilled ? :rich : :jailed }
|
|
|
|
aasm_state :rich
|
|
|
|
aasm_state :jailed
|
|
|
|
attr_accessor :skilled, :aasm_state
|
|
|
|
end
|
2008-06-22 11:17:12 -04:00
|
|
|
|
2008-04-29 01:27:56 -04:00
|
|
|
describe "aasm model", :shared => true do
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::InstanceMethods" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
2008-05-29 16:23:53 -04:00
|
|
|
describe FooBar, "class methods" do
|
2008-04-29 01:27:56 -04:00
|
|
|
before(:each) do
|
2008-05-29 16:23:53 -04:00
|
|
|
@klass = FooBar
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
it_should_behave_like "aasm model"
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe Fi, "class methods" do
|
|
|
|
before(:each) do
|
|
|
|
@klass = Fi
|
|
|
|
end
|
|
|
|
it_should_behave_like "aasm model"
|
|
|
|
it "should not include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
|
|
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe Fo, "class methods" do
|
|
|
|
before(:each) do
|
|
|
|
@klass = Fo
|
|
|
|
end
|
|
|
|
it_should_behave_like "aasm model"
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
|
|
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe Fum, "class methods" do
|
|
|
|
before(:each) do
|
|
|
|
@klass = Fum
|
|
|
|
end
|
|
|
|
it_should_behave_like "aasm model"
|
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::ReadState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should include AASM::Persistence::ActiveRecordPersistence::WriteState" do
|
|
|
|
@klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
it "should not include AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence" do
|
|
|
|
@klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
|
2008-11-15 01:41:46 -05:00
|
|
|
end
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
2008-05-29 16:23:53 -04:00
|
|
|
describe FooBar, "instance methods" do
|
2008-04-29 01:27:56 -04:00
|
|
|
before(:each) do
|
|
|
|
connection = mock(Connection, :columns => [])
|
2008-05-29 16:23:53 -04:00
|
|
|
FooBar.stub!(:connection).and_return(connection)
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should respond to aasm read state when not previously defined" do
|
2008-05-29 16:23:53 -04:00
|
|
|
FooBar.new.should respond_to(:aasm_read_state)
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should respond to aasm write state when not previously defined" do
|
2008-05-29 16:23:53 -04:00
|
|
|
FooBar.new.should respond_to(:aasm_write_state)
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should respond to aasm write state without persistence when not previously defined" do
|
2008-05-29 16:23:53 -04:00
|
|
|
FooBar.new.should respond_to(:aasm_write_state_without_persistence)
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the initial state when new and the aasm field is nil" do
|
2008-05-29 16:23:53 -04:00
|
|
|
FooBar.new.aasm_current_state.should == :open
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the aasm column when new and the aasm field is not nil" do
|
2008-05-29 16:23:53 -04:00
|
|
|
foo = FooBar.new
|
2008-04-29 01:27:56 -04:00
|
|
|
foo.aasm_state = "closed"
|
|
|
|
foo.aasm_current_state.should == :closed
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the aasm column when not new and the aasm_column is not nil" do
|
2008-05-29 16:23:53 -04:00
|
|
|
foo = FooBar.new
|
2008-04-29 01:27:56 -04:00
|
|
|
foo.stub!(:new_record?).and_return(false)
|
|
|
|
foo.aasm_state = "state"
|
|
|
|
foo.aasm_current_state.should == :state
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow a nil state" do
|
2008-05-29 16:23:53 -04:00
|
|
|
foo = FooBar.new
|
2008-04-29 01:27:56 -04:00
|
|
|
foo.stub!(:new_record?).and_return(false)
|
|
|
|
foo.aasm_state = nil
|
|
|
|
foo.aasm_current_state.should be_nil
|
|
|
|
end
|
|
|
|
|
2008-04-29 01:58:05 -04:00
|
|
|
it "should have aasm_ensure_initial_state" do
|
2008-05-29 16:23:53 -04:00
|
|
|
foo = FooBar.new
|
2008-04-29 01:58:05 -04:00
|
|
|
foo.send :aasm_ensure_initial_state
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call aasm_ensure_initial_state on validation before create" do
|
2008-05-29 16:23:53 -04:00
|
|
|
foo = FooBar.new
|
2008-04-29 01:58:05 -04:00
|
|
|
foo.should_receive(:aasm_ensure_initial_state).and_return(true)
|
|
|
|
foo.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call aasm_ensure_initial_state on validation before create" do
|
2008-05-29 16:23:53 -04:00
|
|
|
foo = FooBar.new
|
2008-04-29 01:58:05 -04:00
|
|
|
foo.stub!(:new_record?).and_return(false)
|
|
|
|
foo.should_not_receive(:aasm_ensure_initial_state)
|
|
|
|
foo.valid?
|
|
|
|
end
|
2008-11-15 01:41:46 -05:00
|
|
|
|
2008-04-29 01:27:56 -04:00
|
|
|
end
|
|
|
|
|
2008-06-22 11:17:12 -04:00
|
|
|
describe 'Beavers' do
|
|
|
|
it "should have the same states as it's parent" do
|
|
|
|
Beaver.aasm_states.should == June.aasm_states
|
|
|
|
end
|
2008-11-15 01:41:46 -05:00
|
|
|
|
2008-06-22 11:17:12 -04:00
|
|
|
it "should have the same events as it's parent" do
|
|
|
|
Beaver.aasm_events.should == June.aasm_events
|
|
|
|
end
|
2008-11-15 01:41:46 -05:00
|
|
|
|
2008-06-22 11:17:12 -04:00
|
|
|
it "should have the same column as it's parent" do
|
|
|
|
Beaver.aasm_column.should == :status
|
|
|
|
end
|
|
|
|
end
|
2008-11-15 01:41:46 -05:00
|
|
|
|
|
|
|
describe AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods do
|
|
|
|
class NamedScopeExample < ActiveRecord::Base
|
|
|
|
include AASM
|
|
|
|
end
|
|
|
|
|
|
|
|
context "Does not already respond_to? the scope name" do
|
|
|
|
it "should add a named_scope" do
|
|
|
|
NamedScopeExample.should_receive(:named_scope)
|
|
|
|
NamedScopeExample.aasm_state :unknown_scope
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "Already respond_to? the scope name" do
|
|
|
|
it "should not add a named_scope" do
|
|
|
|
NamedScopeExample.should_not_receive(:named_scope)
|
|
|
|
NamedScopeExample.aasm_state :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-05-27 20:32:02 -04:00
|
|
|
|
2009-02-26 12:06:43 -05:00
|
|
|
describe 'Thieves' do
|
|
|
|
before(:each) do
|
|
|
|
connection = mock(Connection, :columns => [])
|
|
|
|
Thief.stub!(:connection).and_return(connection)
|
|
|
|
end
|
2009-05-27 20:32:02 -04:00
|
|
|
|
2009-02-26 12:06:43 -05:00
|
|
|
it 'should be rich if they\'re skilled' do
|
|
|
|
Thief.new(:skilled => true).aasm_current_state.should == :rich
|
|
|
|
end
|
2009-05-27 20:32:02 -04:00
|
|
|
|
2009-02-26 12:06:43 -05:00
|
|
|
it 'should be jailed if they\'re unskilled' do
|
|
|
|
Thief.new(:skilled => false).aasm_current_state.should == :jailed
|
|
|
|
end
|
|
|
|
end
|
2008-06-22 11:17:12 -04:00
|
|
|
|
2008-04-29 01:27:56 -04:00
|
|
|
# TODO: figure out how to test ActiveRecord reload! without a database
|
|
|
|
|
|
|
|
rescue LoadError => e
|
|
|
|
if e.message == "no such file to load -- active_record"
|
|
|
|
puts "You must install active record to run this spec. Install with sudo gem install activerecord"
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|