From 71937c04efc90e3928e249b697f6c0ad9eff0a56 Mon Sep 17 00:00:00 2001 From: dwbutler Date: Mon, 27 Aug 2012 07:51:07 -0700 Subject: [PATCH 1/4] 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 From 2eb5fd0bfc2b0f44a89c89c455250de4de36cdef Mon Sep 17 00:00:00 2001 From: dwbutler Date: Mon, 27 Aug 2012 08:55:57 -0700 Subject: [PATCH 2/4] More descriptive test case for private callbacks --- test/main_test.rb | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/test/main_test.rb b/test/main_test.rb index f7610a0..585ab23 100644 --- a/test/main_test.rb +++ b/test/main_test.rb @@ -267,7 +267,6 @@ 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 @@ -278,10 +277,7 @@ def my_transition(args) state :one do event :my_transition, :transitions_to => :two end - state :two do - event :another_transition, :transitions_to => :three - end - state :three + state :two end private @@ -291,7 +287,28 @@ def another_transition(args) end a = c.new a.my_transition!(args) - a.another_transition!(args) + end + + test 'private transition callback' do + args = mock() + args.expects(:log).once + c = Class.new + c.class_eval do + include Workflow + workflow do + state :new do + event :assign, :transitions_to => :assigned + end + state :assigned + end + + private + def assign(args) + args.log('Assigned') + end + end + a = c.new + a.assign!(args) end test 'Single table inheritance (STI)' do From 82ad783f59e782a237725e4b54a5c93bef270a7d Mon Sep 17 00:00:00 2001 From: dwbutler Date: Mon, 27 Aug 2012 08:56:11 -0700 Subject: [PATCH 3/4] Ruby 1.8.7 compatibility --- lib/workflow.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/workflow.rb b/lib/workflow.rb index cd59de2..38f44c8 100644 --- a/lib/workflow.rb +++ b/lib/workflow.rb @@ -268,7 +268,7 @@ def run_action(action, *args) end def has_callback?(action) - self.respond_to?(action) or self.private_methods.include?(action) + self.respond_to?(action) or self.private_methods.map { |n| n.to_sym }.include?(action) end def run_action_callback(action_name, *args) From 7214ade952bb2df6bca280fd750c7104c9325f15 Mon Sep 17 00:00:00 2001 From: dwbutler Date: Tue, 28 Aug 2012 13:35:55 -0700 Subject: [PATCH 4/4] Better way to check for private methods --- lib/workflow.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/workflow.rb b/lib/workflow.rb index 38f44c8..ef881be 100644 --- a/lib/workflow.rb +++ b/lib/workflow.rb @@ -268,7 +268,7 @@ def run_action(action, *args) end def has_callback?(action) - self.respond_to?(action) or self.private_methods.map { |n| n.to_sym }.include?(action) + self.respond_to?(action) or self.class.private_method_defined?(action) end def run_action_callback(action_name, *args)