+
Skip to content

Stopping event without raising from after callback #835

@paulodeon

Description

@paulodeon
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 no way to achieve this in aasm without adding these wrappers?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载