I have a State Machine, in a Rails app (with ActiveRecord), defined with AASM and it has a lot of callbacks. Some of these callbacks contain repeated code. E.g, on every state change, we need to build and save a record and send a mail.
event :revision, after: :revision_event_callback do
# ...
end
event :basic_submit, after: :basic_submit_event_callback do
# ...
end
event :approve, after: :approve_event_callback do
# ...
end
def revision_event_callback
OrderStatusMailer.status_change_report(self, :revision)
state_changes.build(transition: :revision)
end
def basic_submit_event_callback
OrderStatusMailer.status_change_report(self, :basic_submit)
state_changes.build(transition: :basic_submit)
# ...
# basic_submit-specific events
end
def approve_event_callback
OrderStatusMailer.status_change_report(self, :approve)
state_changes.build(transition: :approve)
end
Intead, I'd like to have one generic callback, and, for the events that need it, a specific in addition.
def event_callback(event_name)
OrderStatusMailer.status_change_report(self, event_name)
state_changes.build(transition: event_name)
end
def basic_submit_event_callback
# basic_submit-specific events
end
Is this possible at all? And, quite important: how would that code have access to the name of the event that was triggered?