Skip to content

Commit 71937c0

Browse files
committed
Added support for private callbacks
1 parent 8496110 commit 71937c0

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/workflow.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,14 @@ def run_after_transition(from, to, event, *args)
266266
def run_action(action, *args)
267267
instance_exec(*args, &action) if action
268268
end
269+
270+
def has_callback?(action)
271+
self.respond_to?(action) or self.private_methods.include?(action)
272+
end
269273

270274
def run_action_callback(action_name, *args)
271-
self.send action_name.to_sym, *args if self.respond_to?(action_name.to_sym)
275+
action = action_name.to_sym
276+
self.send(action, *args) if has_callback?(action)
272277
end
273278

274279
def run_on_entry(state, prior_state, triggering_event, *args)

test/main_test.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def assert_state(title, expected_state, klass = Order)
267267
test 'implicit transition callback' do
268268
args = mock()
269269
args.expects(:my_tran).once # this is validated at the end
270+
args.expects(:another_tran).once
270271
c = Class.new
271272
c.class_eval do
272273
include Workflow
@@ -277,10 +278,20 @@ def my_transition(args)
277278
state :one do
278279
event :my_transition, :transitions_to => :two
279280
end
280-
state :two
281+
state :two do
282+
event :another_transition, :transitions_to => :three
283+
end
284+
state :three
285+
end
286+
287+
private
288+
def another_transition(args)
289+
args.another_tran
281290
end
282291
end
283-
c.new.my_transition!(args)
292+
a = c.new
293+
a.my_transition!(args)
294+
a.another_transition!(args)
284295
end
285296

286297
test 'Single table inheritance (STI)' do

0 commit comments

Comments
 (0)