带有参数的回调后的AASM

hfyxw5xn  于 2022-10-15  发布在  Ruby
关注(0)|答案(4)|浏览(114)

我在我的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
  ...

当我调用@post.accept_approval!(:active, current_user)并且After回调被触发时,我可以在我的控制台中检查user是什么(它被传递到proc中),它是nil
这里发生了什么事?什么才是这一转变的正确叫法?

nimxete2

nimxete21#

在回调一节中查看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
pgky5nke

pgky5nke2#

适用于当前版本(4.3.0):

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

  transitions from: :active, to: :finished
end
agxfikkp

agxfikkp3#

event :accept_approval do
  transitions from: :pending_approval, to: :active
end

post.accept_approval!{post.set_approvaler(current_user)}
BLOCK到BANG方法将在转换成功后调用,如果任何活动记录操作,它将被 Package 到转换事务中,您可以要求锁定以防止选项requires_lock: true出现并发问题。

dldeef67

dldeef674#

:after移至transitions部分,以便:

event :accept_approval do
  transitions from: :pending_approval, 
    to: :active,
    :after => Proc.new { |user| binding.pry }
end

那就叫它@post.accept_approval!(current_user)

相关问题