Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Aug 18, 2014
1 parent 2f5f9cb commit 43a63a7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 29 deletions.
6 changes: 3 additions & 3 deletions AASM4.md
@@ -1,8 +1,8 @@
# Planned changes for AASM 4

* **done**: firing an event does not require `to_state` parameter anymore (closing issues #11, #58, #80)
* don't allow direct assignment of state attribute (see #53)
* remove old callbacks (see #96)
* **done**: firing an event does not require `to_state` parameter anymore (closing [issue #11](https://github.com/aasm/aasm/issues/11), [issue #58](https://github.com/aasm/aasm/issues/58) and [issue #80](https://github.com/aasm/aasm/issues/80))
* **done**: don't allow direct assignment of state attribute (see [issue #53](https://github.com/aasm/aasm/issues/53))
* remove old callbacks (see [issue #96](https://github.com/aasm/aasm/issues/96))
* remove old aasm DSL (like `aasm_state`)


Expand Down
21 changes: 21 additions & 0 deletions callbacks.txt
@@ -1,5 +1,7 @@
callbacks

AASM 3

begin
old_state exit # old? should be deprecated -> use old_state.before_exit instead
event before
Expand All @@ -15,3 +17,22 @@ begin
event after
rescue
event error


AASM 4

begin
event before
old_state before_exit
old_state exit # old? should be deprecated -> use old_state.before_exit instead
new_state before_enter
new_state enter # old? should be deprecated -> use new_state.before_enter instead
...update state...
transition guard
transition on_transition
event success # if persist successful
old_state after_exit
new_state after_enter
event after
rescue
event error
8 changes: 6 additions & 2 deletions lib/aasm/aasm.rb
Expand Up @@ -163,7 +163,7 @@ def aasm_fire_event(event_name, options, *args, &block)
event = self.class.aasm.events[event_name]
begin
old_state = aasm.state_object_for_name(aasm.current_state)
old_state.fire_callbacks(:exit, self)
# old_state.fire_callbacks(:exit, self)

# new event before callback
event.fire_callbacks(
Expand All @@ -172,6 +172,9 @@ def aasm_fire_event(event_name, options, *args, &block)
*process_args(event, aasm.current_state, *args)
)

old_state.fire_callbacks(:before_exit, self)
old_state.fire_callbacks(:exit, self)

if new_state_name = event.fire(self, *args)
aasm_fired(event, old_state, new_state_name, options, *args, &block)
else
Expand All @@ -188,7 +191,8 @@ def aasm_fired(event, old_state, new_state_name, options, *args)
new_state = aasm.state_object_for_name(new_state_name)

# new before_ callbacks
old_state.fire_callbacks(:before_exit, self)
# old_state.fire_callbacks(:before_exit, self)
# old_state.fire_callbacks(:exit, self)
new_state.fire_callbacks(:before_enter, self)

new_state.fire_callbacks(:enter, self)
Expand Down
2 changes: 1 addition & 1 deletion spec/models/auth_machine.rb
Expand Up @@ -8,7 +8,7 @@ class AuthMachine
state :pending, :initial => true, :enter => :make_activation_code
state :active, :enter => :do_activate
state :suspended
state :deleted, :enter => :do_delete, :exit => :do_undelete
state :deleted, :enter => :do_delete#, :exit => :do_undelete
state :waiting

event :register do
Expand Down
32 changes: 19 additions & 13 deletions spec/models/callback_new_dsl.rb
Expand Up @@ -17,29 +17,35 @@ class CallbackNewDsl
:after_exit => :after_exit_closed

event :close, :before => :before, :after => :after do
transitions :to => :closed, :from => [:open]
transitions :to => :closed, :from => [:open], :on_transition => :transitioning
end

event :open, :before => :before, :after => :after do
transitions :to => :open, :from => :closed
end
end

def before_enter_open; end
def before_exit_open; end
def after_enter_open; end
def after_exit_open; end
def log(text)
puts text
end

def before_enter_closed; end
def before_exit_closed; end
def after_enter_closed; end
def after_exit_closed; end
def before_enter_open; log('before_enter_open'); end
def before_exit_open; log('before_exit_open'); end
def after_enter_open; log('after_enter_open'); end
def after_exit_open; log('after_exit_open'); end

def before; end
def after; end
def before_enter_closed; log('before_enter_closed'); end
def before_exit_closed; log('before_exit_closed'); end
def after_enter_closed; log('after_enter_closed'); end
def after_exit_closed; log('after_exit_closed'); end

def enter_closed; end
def exit_open; end
def transitioning; log('transitioning'); end

def before; log('before'); end
def after; log('after'); end

def enter_closed; log('enter_closed'); end
def exit_open; log('exit_open'); end
end

class CallbackNewDslArgs
Expand Down
23 changes: 13 additions & 10 deletions spec/unit/callbacks_spec.rb
Expand Up @@ -4,25 +4,28 @@
let(:callback) {CallbackNewDsl.new}

it "be called in order" do
expect(callback).to receive(:exit_open).once.ordered
expect(callback).to receive(:before).once.ordered
expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes
expect(callback).to receive(:before_enter_closed).once.ordered
expect(callback).to receive(:enter_closed).once.ordered
expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes
expect(callback).to receive(:after_enter_closed).once.ordered
expect(callback).to receive(:after).once.ordered
# expect(callback).to receive(:before).once.ordered
# expect(callback).to receive(:before_exit_open).once.ordered # these should be before the state changes
# expect(callback).to receive(:exit_open).once.ordered
# expect(callback).to receive(:before_enter_closed).once.ordered
# expect(callback).to receive(:enter_closed).once.ordered
# expect(callback).to receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
# expect(callback).to receive(:after_exit_open).once.ordered # these should be after the state changes
# expect(callback).to receive(:after_enter_closed).once.ordered
# expect(callback).to receive(:after).once.ordered

callback.aasm.current_state
puts "---------------------- close!"

callback.close!
end

it "should properly pass arguments" do
cb = CallbackNewDslArgs.new
cb.should_receive(:exit_open).once.ordered
cb.should_receive(:before).with(:arg1, :arg2).once.ordered
cb.should_receive(:transition_proc).with(:arg1, :arg2).once.ordered
cb.should_receive(:before_exit_open).once.ordered # these should be before the state changes
cb.should_receive(:exit_open).once.ordered
cb.should_receive(:before_enter_closed).once.ordered
cb.should_receive(:enter_closed).once.ordered
cb.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes
Expand Down

0 comments on commit 43a63a7

Please sign in to comment.