From 71937c04efc90e3928e249b697f6c0ad9eff0a56 Mon Sep 17 00:00:00 2001 From: dwbutler Date: Mon, 27 Aug 2012 07:51:07 -0700 Subject: [PATCH] Added support for private callbacks --- lib/workflow.rb | 7 ++++++- test/main_test.rb | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/workflow.rb b/lib/workflow.rb index c2fddae..cd59de2 100644 --- a/lib/workflow.rb +++ b/lib/workflow.rb @@ -266,9 +266,14 @@ def run_after_transition(from, to, event, *args) def run_action(action, *args) instance_exec(*args, &action) if action end + + def has_callback?(action) + self.respond_to?(action) or self.private_methods.include?(action) + end def run_action_callback(action_name, *args) - self.send action_name.to_sym, *args if self.respond_to?(action_name.to_sym) + action = action_name.to_sym + self.send(action, *args) if has_callback?(action) end def run_on_entry(state, prior_state, triggering_event, *args) diff --git a/test/main_test.rb b/test/main_test.rb index 563fcb8..f7610a0 100644 --- a/test/main_test.rb +++ b/test/main_test.rb @@ -267,6 +267,7 @@ def assert_state(title, expected_state, klass = Order) test 'implicit transition callback' do args = mock() args.expects(:my_tran).once # this is validated at the end + args.expects(:another_tran).once c = Class.new c.class_eval do include Workflow @@ -277,10 +278,20 @@ def my_transition(args) state :one do event :my_transition, :transitions_to => :two end - state :two + state :two do + event :another_transition, :transitions_to => :three + end + state :three + end + + private + def another_transition(args) + args.another_tran end end - c.new.my_transition!(args) + a = c.new + a.my_transition!(args) + a.another_transition!(args) end test 'Single table inheritance (STI)' do