From 3e684025653690e9944bb200a45627d5a9087b18 Mon Sep 17 00:00:00 2001 From: Adrian Serafin Date: Fri, 20 May 2022 13:25:51 +0200 Subject: [PATCH] allow event arguments to be taken into account when selecting the event --- lib/workflow.rb | 2 +- lib/workflow/event.rb | 6 +++--- lib/workflow/event_collection.rb | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/workflow.rb b/lib/workflow.rb index 558e28117..bc3f445f1 100644 --- a/lib/workflow.rb +++ b/lib/workflow.rb @@ -95,7 +95,7 @@ def halted_because end def process_event!(name, *args) - event = current_state.events.first_applicable(name, self) + event = current_state.events.first_applicable(name, self, args) raise NoTransitionAllowed.new( "There is no event #{name.to_sym} defined for the #{current_state} state") \ if event.nil? diff --git a/lib/workflow/event.rb b/lib/workflow/event.rb index ed5048ad0..fd3187007 100644 --- a/lib/workflow/event.rb +++ b/lib/workflow/event.rb @@ -15,12 +15,12 @@ def initialize(name, transitions_to, condition = nil, meta = {}, &action) end end - def condition_applicable?(object) + def condition_applicable?(object, event_arguments) if condition if condition.is_a?(Symbol) - object.send(condition) + object.send(condition, *event_arguments) else - condition.call(object) + condition.call(object, *event_arguments) end else true diff --git a/lib/workflow/event_collection.rb b/lib/workflow/event_collection.rb index 5a20cfc10..4860bf6e9 100644 --- a/lib/workflow/event_collection.rb +++ b/lib/workflow/event_collection.rb @@ -26,9 +26,9 @@ def include?(name_or_obj) end end - def first_applicable(name, object_context) + def first_applicable(name, object_context, event_arguments) (self[name] || []).detect do |event| - event.condition_applicable?(object_context) && event + event.condition_applicable?(object_context, event_arguments) && event end end