5

我在我的 rails 4 应用程序中使用aasm(以前的)gem。acts_as_state_machine我的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
  ...

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

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

4

3 回答 3

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

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

  @post.accept_approval! current_user
于 2016-02-01T13:38:08.290 回答
2

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

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

  transitions from: :active, to: :finished
end
于 2015-09-14T08:12:31.897 回答
1
event :accept_approval do
  transitions from: :pending_approval, to: :active
end

post.accept_approval!{post.set_approvaler(current_user)}

转换成功后将调用 block to bang 方法,如果有任何 activerecord 操作,它将被包装到转换事务中,您可以要求锁定以防止选项的并发问题 requires_lock: true

于 2017-10-12T10:16:31.443 回答