Skip to content

Commit

Permalink
all aasm tests without activerecord moved over and passing
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jun 28, 2008
1 parent a9d9ca1 commit c9e366e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 59 deletions.
22 changes: 19 additions & 3 deletions activemodel/lib/active_model/state_machine.rb
Expand Up @@ -37,13 +37,29 @@ def state_machine(name = nil, options = {}, &block)
end end
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) sm = self.class.state_machine(name)
ivar = "@#{sm.name}_current_state" ivar = sm.current_state_variable
if name && new_state 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) instance_variable_set(ivar, new_state)
else 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 end
end end
Expand Down
4 changes: 0 additions & 4 deletions activemodel/lib/active_model/state_machine/event.rb
Expand Up @@ -37,10 +37,6 @@ def transitions_from_state?(state)
@transitions.any? { |t| t.from? state } @transitions.any? { |t| t.from? state }
end end


def success?
!!@success
end

def ==(event) def ==(event)
if event.is_a? Symbol if event.is_a? Symbol
name == event name == event
Expand Down
8 changes: 7 additions & 1 deletion activemodel/lib/active_model/state_machine/machine.rb
Expand Up @@ -28,7 +28,9 @@ def fire_event(event, record, persist, *args)
record.send(event_fired_callback, record.current_state, new_state) record.send(event_fired_callback, record.current_state, new_state)
end 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 else
if record.respond_to?(event_failed_callback) if record.respond_to?(event_failed_callback)
record.send(event_failed_callback, event) record.send(event_failed_callback, event)
Expand All @@ -47,6 +49,10 @@ def events_for(state)
events.map! { |event| event.name } events.map! { |event| event.name }
end end


def current_state_variable
"@#{@name}_current_state"
end

private private
def state(name, options = {}) def state(name, options = {})
@states << (state_index[name] ||= State.new(name, :machine => self)).update(options) @states << (state_index[name] ||= State.new(name, :machine => self)).update(options)
Expand Down
2 changes: 1 addition & 1 deletion activemodel/test/state_machine/event_test.rb
Expand Up @@ -17,7 +17,7 @@ def new_event
end end


test 'should set the success option' do test 'should set the success option' do
assert new_event.success? assert_equal @success, new_event.success
end end


uses_mocha 'StateTransition creation' do uses_mocha 'StateTransition creation' do
Expand Down
99 changes: 49 additions & 50 deletions activemodel/test/state_machine_test.rb
Expand Up @@ -110,41 +110,38 @@ def setup
assert_equal :read, @foo.current_state(:bar) assert_equal :read, @foo.current_state(:bar)
end end
end end
#
#describe AASM, '- event firing with persistence' do class StateMachineEventFiringWithPersistenceTest < ActiveModel::TestCase
# it 'should fire the Event' do def setup
# foo = Foo.new @subj = StateMachineSubject.new
# end
# Foo.aasm_events[:close].should_receive(:fire).with(foo)
# foo.close! test 'updates the current state' do
# end @subj.close!
#
# it 'should update the current state' do assert_equal :closed, @subj.current_state
# foo = Foo.new end
# foo.close!
# uses_mocha "StateMachineEventFiringWithPersistenceTest with callbacks" do
# foo.aasm_current_state.should == :closed test 'fires the Event' do
# end @subj.class.state_machine.events[:close].expects(:fire).with(@subj)
# @subj.close!
# it 'should call the success callback if one was provided' do end
# foo = Foo.new
# test 'calls the success callback if one was provided' do
# foo.should_receive(:success_callback) @subj.expects(:success_callback)
# @subj.close!
# foo.close! end
# end
# test 'attempts to persist if write_state is defined' do
# it 'should attempt to persist if aasm_write_state is defined' do def @subj.write_state
# foo = Foo.new end
#
# def foo.aasm_write_state @subj.expects(:write_state)
# end @subj.close!
# end
# foo.should_receive(:aasm_write_state) end
# end
# foo.close!
# end
#end


class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase
test 'updates the current state' do test 'updates the current state' do
Expand All @@ -162,30 +159,32 @@ class StateMachineEventFiringWithoutPersistence < ActiveModel::TestCase
subj.close subj.close
end 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 subj = StateMachineSubject.new


def subj.aasm_write_state def subj.write_state
end end


subj.expects(:aasm_write_state_without_persistence) subj.expects(:write_state_without_persistence)


subj.close subj.close
end end
end end
end end


#describe AASM, '- persistence' do uses_mocha 'StateMachinePersistenceTest' do
# it 'should read the state if it has not been set and aasm_read_state is defined' do class StateMachinePersistenceTest < ActiveModel::TestCase
# foo = Foo.new test 'reads the state if it has not been set and read_state is defined' do
# def foo.aasm_read_state subj = StateMachineSubject.new
# end def subj.read_state
# end
# foo.should_receive(:aasm_read_state)
# subj.expects(:read_state).with(StateMachineSubject.state_machine)
# foo.aasm_current_state
# end subj.current_state
#end end
end
end


uses_mocha 'StateMachineEventCallbacksTest' do uses_mocha 'StateMachineEventCallbacksTest' do
class StateMachineEventCallbacksTest < ActiveModel::TestCase class StateMachineEventCallbacksTest < ActiveModel::TestCase
Expand Down

0 comments on commit c9e366e

Please sign in to comment.