diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index f37cc88..5b17fdf 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -21,6 +21,7 @@ module AASM @aasm end + # TODO: maybe better: aasm.initial_state def aasm_initial_state(set_state=nil) if set_state # deprecated way to set the value @@ -68,7 +69,8 @@ module AASM aasm.states_for_select end - def aasm_human_event_name(event) + # aasm.event(:event_name).human? + def aasm_human_event_name(event) # event_name? AASM::SupportingClasses::Localizer.new.human_event_name(self, event) end end # ClassMethods diff --git a/lib/aasm/base.rb b/lib/aasm/base.rb index c6e398c..209fcf3 100644 --- a/lib/aasm/base.rb +++ b/lib/aasm/base.rb @@ -1,5 +1,6 @@ module AASM class Base + def initialize(clazz, options={}, &block) @clazz = clazz sm = AASM::StateMachine[@clazz] diff --git a/spec/models/callback_new_dsl.rb b/spec/models/callback_new_dsl.rb index 205ffd3..7fa517a 100644 --- a/spec/models/callback_new_dsl.rb +++ b/spec/models/callback_new_dsl.rb @@ -6,10 +6,12 @@ class CallbackNewDsl :before_enter => :before_enter_open, :after_enter => :after_enter_open, :before_exit => :before_exit_open, + :exit => :exit_open, :after_exit => :after_exit_open state :closed, :before_enter => :before_enter_closed, + :enter => :enter_closed, :after_enter => :after_enter_closed, :before_exit => :before_exit_closed, :after_exit => :after_exit_closed @@ -35,4 +37,7 @@ class CallbackNewDsl def before; end def after; end + + def enter_closed; end + def exit_open; end end diff --git a/spec/models/callback_old_dsl.rb b/spec/models/callback_old_dsl.rb index fb75fff..3df6f34 100644 --- a/spec/models/callback_old_dsl.rb +++ b/spec/models/callback_old_dsl.rb @@ -4,14 +4,16 @@ class CallbackOldDsl aasm_initial_state :open aasm_state :open, :before_enter => :before_enter_open, - :before_exit => :before_exit_open, - :after_enter => :after_enter_open, - :after_exit => :after_exit_open + :after_enter => :after_enter_open, + :before_exit => :before_exit_open, + :exit => :exit_open, + :after_exit => :after_exit_open aasm_state :closed, :before_enter => :before_enter_closed, - :before_exit => :before_exit_closed, - :after_enter => :after_enter_closed, - :after_exit => :after_exit_closed + :enter => :enter_closed, + :after_enter => :after_enter_closed, + :before_exit => :before_exit_closed, + :after_exit => :after_exit_closed aasm_event :close, :before => :before, :after => :after do transitions :to => :closed, :from => [:open] @@ -33,4 +35,7 @@ class CallbackOldDsl def before; end def after; end + + def enter_closed; end + def exit_open; end end diff --git a/spec/unit/callbacks_new_dsl_spec.rb b/spec/unit/callbacks_new_dsl_spec.rb deleted file mode 100644 index 8263e13..0000000 --- a/spec/unit/callbacks_new_dsl_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'spec_helper' - -describe 'callbacks for the new DSL' do - before(:each) do - @callback = CallbackNewDsl.new - end - - it "should get close callbacks" do - @callback.should_receive(:before).once.ordered - @callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes - @callback.should_receive(:before_enter_closed).once.ordered - @callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes - @callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes - @callback.should_receive(:after_enter_closed).once.ordered - @callback.should_receive(:after).once.ordered - - @callback.close! - end - - it "should get open callbacks" do - @callback.close! - - @callback.should_receive(:before).once.ordered - @callback.should_receive(:before_exit_closed).once.ordered # these should be before the state changes - @callback.should_receive(:before_enter_open).once.ordered - @callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes - @callback.should_receive(:after_exit_closed).once.ordered # these should be after the state changes - @callback.should_receive(:after_enter_open).once.ordered - @callback.should_receive(:after).once.ordered - - @callback.open! - 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 diff --git a/spec/unit/callbacks_old_dsl_spec.rb b/spec/unit/callbacks_spec.rb similarity index 60% rename from spec/unit/callbacks_old_dsl_spec.rb rename to spec/unit/callbacks_spec.rb index 9db7d24..9853aec 100644 --- a/spec/unit/callbacks_old_dsl_spec.rb +++ b/spec/unit/callbacks_spec.rb @@ -1,38 +1,42 @@ require 'spec_helper' describe 'callbacks for the old DSL' do - before(:each) do - @callback = CallbackOldDsl.new - end + let(:callback) {CallbackOldDsl.new} it "should get close callbacks" do - @callback.should_receive(:before).once.ordered - @callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes - @callback.should_receive(:before_enter_closed).once.ordered - @callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes - @callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes - @callback.should_receive(:after_enter_closed).once.ordered - @callback.should_receive(:after).once.ordered + callback.should_receive(:exit_open).once.ordered + callback.should_receive(:before).once.ordered + callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes + callback.should_receive(:before_enter_closed).once.ordered + callback.should_receive(:enter_closed).once.ordered + callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes + callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes + callback.should_receive(:after_enter_closed).once.ordered + callback.should_receive(:after).once.ordered - @callback.close! - end - - it "should get open callbacks" do - @callback.close! - - @callback.should_receive(:before).once.ordered - @callback.should_receive(:before_exit_closed).once.ordered # these should be before the state changes - @callback.should_receive(:before_enter_open).once.ordered - @callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes - @callback.should_receive(:after_exit_closed).once.ordered # these should be after the state changes - @callback.should_receive(:after_enter_open).once.ordered - @callback.should_receive(:after).once.ordered - - @callback.open! + callback.close! end end -describe AASM, '- event callbacks' do +describe 'callbacks for the new DSL' do + let(:callback) {CallbackNewDsl.new} + + it "be called in order" do + callback.should_receive(:exit_open).once.ordered + callback.should_receive(:before).once.ordered + callback.should_receive(:before_exit_open).once.ordered # these should be before the state changes + callback.should_receive(:before_enter_closed).once.ordered + callback.should_receive(:enter_closed).once.ordered + callback.should_receive(:aasm_write_state).once.ordered.and_return(true) # this is when the state changes + callback.should_receive(:after_exit_open).once.ordered # these should be after the state changes + callback.should_receive(:after_enter_closed).once.ordered + callback.should_receive(:after).once.ordered + + callback.close! + end +end + +describe 'event callbacks' do describe "with an error callback defined" do before do class Foo @@ -45,10 +49,11 @@ describe AASM, '- event callbacks' do end it "should run error_callback if an exception is raised and error_callback defined" do - def @foo.error_callback(e) - end + def @foo.error_callback(e); end + @foo.stub!(:enter).and_raise(e=StandardError.new) @foo.should_receive(:error_callback).with(e) + @foo.safe_close! end @@ -66,8 +71,7 @@ describe AASM, '- event callbacks' do describe "with aasm_event_fired defined" do before do @foo = Foo.new - def @foo.aasm_event_fired(event, from, to) - end + def @foo.aasm_event_fired(event, from, to); end end it 'should call it for successful bang fire' do @@ -90,8 +94,7 @@ describe AASM, '- event callbacks' do describe "with aasm_event_failed defined" do before do @foo = Foo.new - def @foo.aasm_event_failed(event, from) - end + def @foo.aasm_event_failed(event, from); end end it 'should call it when transition failed for bang fire' do