aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorrick <technoweenie@gmail.com>2008-06-28 11:33:50 -0700
committerrick <technoweenie@gmail.com>2008-06-28 11:33:50 -0700
commitc9e366e997c6f3a383cfaa6351fa847e92de7fe4 (patch)
tree1fce341c1b769bd78494bfb6a99a314b57d6a3ed /activemodel
parenta9d9ca16c739ec39a192d29c62f760e51040fc6e (diff)
downloadrails-c9e366e997c6f3a383cfaa6351fa847e92de7fe4.tar.gz
rails-c9e366e997c6f3a383cfaa6351fa847e92de7fe4.tar.bz2
rails-c9e366e997c6f3a383cfaa6351fa847e92de7fe4.zip
all aasm tests without activerecord moved over and passing
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/state_machine.rb22
-rw-r--r--activemodel/lib/active_model/state_machine/event.rb4
-rw-r--r--activemodel/lib/active_model/state_machine/machine.rb8
-rw-r--r--activemodel/test/state_machine/event_test.rb2
-rw-r--r--activemodel/test/state_machine_test.rb99
5 files changed, 76 insertions, 59 deletions
diff --git a/activemodel/lib/active_model/state_machine.rb b/activemodel/lib/active_model/state_machine.rb
index 2a5ac95a3e..96df6539ae 100644
--- a/activemodel/lib/active_model/state_machine.rb
+++ b/activemodel/lib/active_model/state_machine.rb
@@ -37,13 +37,29 @@ module ActiveModel
end
end
- def current_state(name = nil, new_state = nil)
+ def current_state(name = nil, new_state = nil, persist = false)
sm = self.class.state_machine(name)
- ivar = "@#{sm.name}_current_state"
+ ivar = sm.current_state_variable
if name && new_state
+ if persist && respond_to?(:write_state)
+ write_state(sm, new_state)
+ end
+
+ if respond_to?(:write_state_without_persistence)
+ write_state_without_persistence(sm, new_state)
+ end
+
instance_variable_set(ivar, new_state)
else
- instance_variable_get(ivar) || instance_variable_set(ivar, sm.initial_state)
+ instance_variable_set(ivar, nil) unless instance_variable_defined?(ivar)
+ value = instance_variable_get(ivar)
+ return value if value
+
+ if respond_to?(:read_state)
+ value = instance_variable_set(ivar, read_state(sm))
+ end
+
+ value || sm.initial_state
end
end
end
diff --git a/activemodel/lib/active_model/state_machine/event.rb b/activemodel/lib/active_model/state_machine/event.rb
index ea4df343de..e8bc8ebdb7 100644
--- a/activemodel/lib/active_model/state_machine/event.rb
+++ b/activemodel/lib/active_model/state_machine/event.rb
@@ -37,10 +37,6 @@ module ActiveModel
@transitions.any? { |t| t.from? state }
end
- def success?
- !!@success
- end
-
def ==(event)
if event.is_a? Symbol
name == event
diff --git a/activemodel/lib/active_model/state_machine/machine.rb b/activemodel/lib/active_model/state_machine/machine.rb
index 53ce71794f..170505c0b2 100644
--- a/activemodel/lib/active_model/state_machine/machine.rb
+++ b/activemodel/lib/active_model/state_machine/machine.rb
@@ -28,7 +28,9 @@ module ActiveModel
record.send(event_fired_callback, record.current_state, new_state)
end
- record.current_state(@name, new_state)
+ record.current_state(@name, new_state, persist)
+ record.send(@events[event].success) if @events[event].success
+ true
else
if record.respond_to?(event_failed_callback)
record.send(event_failed_callback, event)
@@ -47,6 +49,10 @@ module ActiveModel
events.map! { |event| event.name }
end
+ def current_state_variable
+ "@#{@name}_current_state"
+ end
+
private
def state(name, options = {})
@states << (state_index[name] ||= State.new(name, :machine => self)).update(options)
diff --git a/activemodel/test/state_machine/event_test.rb b/activemodel/test/state_machine/event_test.rb
index 7db4f8d887..40b630da7c 100644
--- a/activemodel/test/state_machine/event_test.rb
+++ b/activemodel/test/state_machine/event_test.rb
@@ -17,7 +17,7 @@ class EventTest < ActiveModel::TestCase
end
test 'should set the success option' do
- assert new_event.success?
+ assert_equal @success, new_event.success
end
uses_mocha 'StateTransition creation' do
diff --git a/activemodel/test/state_machine_test.rb b/activemodel/test/state_machine_test.rb
index 2f08b522d9..b2f0fc4ec0 100644
--- a/activemodel/test/state_machine_test.rb
+++ b/activemodel/test/state_machine_test.rb
@@ -110,41 +110,38 @@ class StateMachineInitialStatesTest < ActiveModel::TestCase
assert_equal :read, @foo.current_state(:bar)
end
end
-#
-#describe AASM, '- event firing with persistence' do
-# it 'should fire the Event' do
-# foo = Foo.new
-#
-# Foo.aasm_events[:close].should_receive(:fire).with(foo)
-# foo.close!
-# end
-#
-# it 'should update the current state' do
-# foo = Foo.new
-# foo.close!
-#
-# foo.aasm_current_state.should == :closed
-# end
-#
-# it 'should call the success callback if one was provided' do
-# foo = Foo.new
-#
-# foo.should_receive(:success_callback)
-#
-# foo.close!
-# end
-#
-# it 'should attempt to persist if aasm_write_state is defined' do
-# foo = Foo.new
-#
-# def foo.aasm_write_state
-# end
-#
-# foo.should_receive(:aasm_write_state)
-#
-# foo.close!
-# end
-#end
+
+class StateMachineEventFiringWithPersistenceTest < ActiveModel::TestCase
+ def setup
+ @subj = StateMachineSubject.new
+ end
+
+ test 'updates the current state' do
+ @subj.close!
+
+ assert_equal :closed, @subj.current_state
+ end
+
+ uses_mocha "StateMachineEventFiringWithPersistenceTest with callbacks" do
+ test 'fires the Event' do
+ @subj.class.state_machine.events[:close].expects(:fire).with(@subj)
+ @subj.close!
+ end
+
+ test 'calls the success callback if one was provided' do
+ @subj.expects(:success_callback)
+ @subj.close!
+ end
+
+ test 'attempts to persist if write_state is defined' do
+ def @subj.write_state
+ end
+
+ @subj.expects(:write_state)
+ @subj.close!
+ end
+ end
+end
class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase
test 'updates the current state' do
@@ -162,30 +159,32 @@ class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase
subj.close
end
- test 'should attempt to persist if aasm_write_state is defined' do
+ test 'attempts to persist if write_state is defined' do
subj = StateMachineSubject.new
- def subj.aasm_write_state
+ def subj.write_state
end
- subj.expects(:aasm_write_state_without_persistence)
+ subj.expects(:write_state_without_persistence)
subj.close
end
end
end
-
-#describe AASM, '- persistence' do
-# it 'should read the state if it has not been set and aasm_read_state is defined' do
-# foo = Foo.new
-# def foo.aasm_read_state
-# end
-#
-# foo.should_receive(:aasm_read_state)
-#
-# foo.aasm_current_state
-# end
-#end
+
+uses_mocha 'StateMachinePersistenceTest' do
+ class StateMachinePersistenceTest < ActiveModel::TestCase
+ test 'reads the state if it has not been set and read_state is defined' do
+ subj = StateMachineSubject.new
+ def subj.read_state
+ end
+
+ subj.expects(:read_state).with(StateMachineSubject.state_machine)
+
+ subj.current_state
+ end
+ end
+end
uses_mocha 'StateMachineEventCallbacksTest' do
class StateMachineEventCallbacksTest < ActiveModel::TestCase