带参数的回调后的aasm

Kyl*_*cot 4 ruby ruby-on-rails aasm acts-as-state-machine ruby-on-rails-4

我在Rails 4应用程序中使用了aasm(以前称为acts_as_state_machine)gem。我的Post模特上有这样的东西

  ...
  aasm column: :state do
    state :pending_approval, initial: true
    state :active
    state :pending_removal

    event :accept_approval, :after => Proc.new { |user| binding.pry } do
      transitions from: :pending_approval, to: :active
    end
  end
  ...
Run Code Online (Sandbox Code Playgroud)

当我调用@post.accept_approval!(:active, current_user)并且触发after回调时,在我的控制台中,我可以检查什么user(已传递到Proc中)及其nil

这里发生了什么?调用此转换的正确方法是什么?

小智 5

在回调部分中查看aasm文档。

...
  aasm column: :state do
    state :pending_approval, initial: true
    state :active
    state :pending_removal

    after_all_transition :log_all_events

    event :accept_approval, after: :log_approval do
      transitions from: :pending_approval, to: :active
    end
  end
  ...
  del log_all_events(user)
    logger.debug "aasm #{aasm.current_event} from #{user}"
  end

  def log_approval(user)
    logger.debug "aasm log_aproove from #{user}"
  end
Run Code Online (Sandbox Code Playgroud)

您可以使用所需的参数调用事件:

  @post.accept_approval! current_user
Run Code Online (Sandbox Code Playgroud)


Luc*_*ton 3

它适用于当前版本(4.3.0):

event :finish do
  before do |user|
    # do something with user
  end

  transitions from: :active, to: :finished
end
Run Code Online (Sandbox Code Playgroud)