From 411ddabf50769a836d321b8cb607a1a5a1f37464 Mon Sep 17 00:00:00 2001 From: Sven Schwyn Date: Sat, 15 Dec 2012 10:47:13 +0100 Subject: [PATCH] Limit lookup scope for private callbacks. Background: Commit 71937c04ef introduced private callbacks, however, the use of "private_method_defined?" returns true all the way down the class hierarchy and thus causes trouble with a very common transition: Given an event "event :fail transitions_to :failed". If you don't explicitly define the "fail" event handler, "private_method_defined?" will still return true since Kernel#fail is defined for any object. Transitioning with "fail!" then causes a runtime error. --- lib/workflow.rb | 2 +- test/main_test.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/workflow.rb b/lib/workflow.rb index 7901fd2..c5defcc 100644 --- a/lib/workflow.rb +++ b/lib/workflow.rb @@ -286,7 +286,7 @@ def run_action(action, *args) end def has_callback?(action) - self.respond_to?(action) or self.class.private_method_defined?(action) + self.respond_to?(action) or self.private_methods(false).map(&:to_sym).include?(action) end def run_action_callback(action_name, *args) diff --git a/test/main_test.rb b/test/main_test.rb index f65edd7..93736f3 100644 --- a/test/main_test.rb +++ b/test/main_test.rb @@ -311,6 +311,22 @@ def assign(args) a.assign!(args) end + test '#58 Limited private transition callback lookup' do + args = mock() + c = Class.new + c.class_eval do + include Workflow + workflow do + state :new do + event :fail, :transitions_to => :failed + end + state :failed + end + end + a = c.new + a.fail!(args) + end + test 'Single table inheritance (STI)' do class BigOrder < Order end