From f8944e5f522f95dbbc76c560251f2dd55926557c Mon Sep 17 00:00:00 2001 From: Chris Maddox Date: Thu, 17 Oct 2013 16:35:51 -0700 Subject: [PATCH] fix callbacks when binding to object --- .../integrations/active_model.rb | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/state_machine/integrations/active_model.rb b/lib/state_machine/integrations/active_model.rb index db5368c3..3d6f316c 100644 --- a/lib/state_machine/integrations/active_model.rb +++ b/lib/state_machine/integrations/active_model.rb @@ -481,12 +481,28 @@ def load_observer_extensions # Adds a set of default callbacks that utilize the Observer extensions def add_default_callbacks if supports_observers? - callbacks[:before] << Callback.new(:before) {|object, transition| notify(:before, object, transition)} - callbacks[:after] << Callback.new(:after) {|object, transition| notify(:after, object, transition)} - callbacks[:failure] << Callback.new(:failure) {|object, transition| notify(:after_failure_to, object, transition)} + callbacks[:before] << Callback.new(:before, &observer_callback(:before, self.name)) + callbacks[:after] << Callback.new(:after, &observer_callback(:after, self.name)) + callbacks[:failure] << Callback.new(:failure, &observer_callback(:failure, self.name)) end end + # Bind the state machine name and callback type to the callback binding + def observer_callback(callback_type, state_machine_name) + lambda do |type, state_machine_name| + if StateMachine::Callback.bind_to_object + lambda do |transition| + state_machine = self.class.state_machine(state_machine_name) + state_machine.send(:notify, callback_type, self, transition) + end + else + lambda do |object, transition| + notify(callback_type, object, transition) + end + end + end.call(callback_type, state_machine_name) + end + # Skips defining reader/writer methods since this is done automatically def define_state_accessor name = self.name