Skip to content

Commit

Permalink
Limit lookup scope for private callbacks.
Browse files Browse the repository at this point in the history
Background: Commit 71937c0 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.
  • Loading branch information
Sven Schwyn committed Dec 15, 2012
1 parent 89f0b08 commit 411ddab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions test/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 411ddab

Please sign in to comment.