module AASM

def aasm_fire_event(state_machine_name, event_name, options, *args, &block)

def aasm_fire_event(state_machine_name, event_name, options, *args, &block)
  event = self.class.aasm(state_machine_name).state_machine.events[event_name]
  begin
    old_state = aasm(state_machine_name).state_object_for_name(aasm(state_machine_name).current_state)
    fire_default_callbacks(event, *process_args(event, aasm(state_machine_name).current_state, *args))
    if may_fire_to = event.may_fire?(self, *args)
      fire_exit_callbacks(old_state, *process_args(event, aasm(state_machine_name).current_state, *args))
      if new_state_name = event.fire(self, {:may_fire => may_fire_to}, *args)
        aasm_fired(state_machine_name, event, old_state, new_state_name, options, *args, &block)
      else
        aasm_failed(state_machine_name, event_name, old_state, event.failed_callbacks)
      end
    else
      aasm_failed(state_machine_name, event_name, old_state, event.failed_callbacks)
    end
  rescue StandardError => e
    event.fire_callbacks(:error, self, e, *process_args(event, aasm(state_machine_name).current_state, *args)) ||
    event.fire_global_callbacks(:error_on_all_events, self, e, *process_args(event, aasm(state_machine_name).current_state, *args)) ||
    raise(e)
    false
  ensure
    event.fire_callbacks(:ensure, self, *process_args(event, aasm(state_machine_name).current_state, *args))
    event.fire_global_callbacks(:ensure_on_all_events, self, *process_args(event, aasm(state_machine_name).current_state, *args))
  end
end