Search code examples
ruby-on-railsrubyaasm

Stopping event in AASM from after callback without raising exception


aasm column: :status, whiny_transitions: false do
  state :requested, initial: true
  state :approved

  event :approve, after: :after_approve do
    transitions from: :requested, to: :approved
  end
end

def after_approve
  raise "This is a test"
end

Given the above I want to be able to call obj.approve! and still have it return true or false so I can handle the exceptions in the model, add errors to the object and handle it in the controller.

I have tried returning false, error handlers etc but it seems there is no way to stop the event from the after callback without raising, nor is there any way to catch the exception handle it and return false (that I can find).

As a workaround I've had to create a new method which wraps the call to approve!

  def approve_with_errors
    approve!
  rescue StandardError => e
    errors.add(:base, "Error: #{e}")
    false
  end

Is there any way to achieve this in aasm without adding these wrappers?

As a side note, on Rails in after callbacks you need to raise ActiveRecord::RecordInvalid, self and the callback chain will be aborted but the exception is not passed up the stack. To do the same in before_ callbacks you can throw(:abort) https://github.com/rails/rails/issues/33192

I've also created an issue in AASM Github


Solution

  • If you raise ActiveRecord::Rollback (not RecordInvalid) then Rails won't propagate that exception up the chain after rolling the DB back. So...

        event :approve, after: :after_approve do
          transitions from: :requested, to: :approved
    
          error do |e|
            errors.add :base, "Error: #{e.message}"
            raise ActiveRecord::Rollback
          end
        end
    
    # ...
    
    > obj.approve!
      ...
      TRANSACTION (0.3ms)  ROLLBACK
    nil
    > obj.errors
    #<ActiveModel::Errors [#<ActiveModel::Error attribute=base, type=Error: This is a test, options={}>]>