0

may_run?在 AASM 中,您可以在 AASM中的示例代码中调用as。

目的:

class Job
  include AASM

  aasm do
    state :sleeping, :initial => true
    state :running, :cleaning

    event :run do
      transitions :from => :sleeping, :to => :running
    end

    event :clean do
      transitions :from => :running, :to => :cleaning
    end

    event :sleep do
      transitions :from => [:running, :cleaning], :to => :sleeping
    end
  end

end

例子

job = Job.new
job.sleeping? # => true
job.may_run?  # => true
job.run
job.running?  # => true
job.sleeping? # => false
job.may_run?  # => false
job.run       # => raises AASM::InvalidTransition

may_如果我将对象和动作作为参数传递,我如何创建一个测试动作的助手。本质上,我想使用类似于以下的帮助器为方法调用添加前缀:

def state_action_url(job, state)
   if job.may_state?
      #link_to action
   else
      #render disabled link text
   end
end
4

1 回答 1

2

你可以像这样使用 smth:

def state_action_url(job, state)
   if job.public_send("may_#{state}?")
      #link_to action
   else
      #render disabled link text
   end
end
于 2016-03-31T17:09:47.057 回答