1
class Ticket
  include AASM

  state :new
  state :open
  state :closed

  event :open do
    transitions :from => :new,:to => :closed, :guard => :cancelled?
    transitions :from => :new,:to => :open, :guard => !:cancelled?
  end
  def cancelled?
    true
  end
  def not_cancelled?
    true
  end
end

##Would I need the below?
transitions :from => :new,:to => :open, :guard => :not_cancelled?

私が書かなければならないコードの量を減らすために、ガード関数で !:cancelled のようなものを持つことは可能ですか? それとも、別の not_cancelled を書く必要がありますか? 機能します(私が推測しているように)。

Ruby 2.1 と gem 'aasm', '~> 3.1.1' を使用しています

4

1 に答える 1

1

まず、!:cancelled?expression は常に評価されるfalseため、aasm はcancelled?メソッドを呼び出しません。コードの量を減らすために、次のようにすることができます

transitions :from => :new, :to => :closed, :guard => :cancelled?
transitions :from => :new, :to => :open, :guard => Proc.new { |ticket| !ticket.cancelled? }
于 2014-03-29T19:04:02.230 に答える