Skip to content

Commit

Permalink
Support enter and exit actions on states
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Barron committed May 31, 2008
1 parent 2bb30ee commit 3ccbeac
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
* Support enter and exit actions on states

* Use named_scope in AR persistence layer, if available [Jan De Poorter]

* Incremented version number
Expand Down
1 change: 0 additions & 1 deletion TODO
@@ -1,6 +1,5 @@
Before Next Release:

* Add state actions (enter, exit, after)
* Add #aasm_next_state_for_event
* Add #aasm_next_states_for_event

Expand Down
9 changes: 9 additions & 0 deletions lib/aasm.rb
Expand Up @@ -96,9 +96,18 @@ def aasm_current_state=(state)
@aasm_current_state = state
end

def aasm_state_object_for_state(name)
self.class.aasm_states.find {|s| s == name}
end

def aasm_fire_event(name, persist)
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)

new_state = self.class.aasm_events[name].fire(self)

unless new_state.nil?
aasm_state_object_for_state(new_state).call_action(:enter, self)

if self.respond_to?(:aasm_event_fired)
self.aasm_event_fired(self.aasm_current_state, new_state)
end
Expand Down
23 changes: 21 additions & 2 deletions spec/unit/aasm_spec.rb
Expand Up @@ -3,8 +3,8 @@
class Foo
include AASM
aasm_initial_state :open
aasm_state :open
aasm_state :closed
aasm_state :open, :exit => :exit
aasm_state :closed, :enter => :enter

aasm_event :close, :success => :success_callback do
transitions :to => :closed, :from => [:open]
Expand All @@ -20,6 +20,11 @@ def always_false

def success_callback
end

def enter
end
def exit
end
end

class Bar
Expand Down Expand Up @@ -231,4 +236,18 @@ def foo.aasm_event_failed(event)
end
end

describe AASM, '- state actions' do
it "should call enter when entering state" do
foo = Foo.new
foo.should_receive(:enter)

foo.close
end

it "should call exit when exiting state" do
foo = Foo.new
foo.should_receive(:exit)

foo.close
end
end

0 comments on commit 3ccbeac

Please sign in to comment.