2014-03-29 2 views
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? 

작성해야하는 코드의 양을 줄이기 위해! 가드 기능에서 취소 할 수 있습니까? 아니면 별도의 not_cancelled를 작성해야합니까? 기능 (내가 의심하는 경우)입니다. aasm도이 경우에 cancelled? 메소드를 호출하지 않도록Ruby gem aasm을 사용하면 부울 결과를 반환하는 가드 함수에 not (!) 연산자를 정의 할 수 있습니까?

나는 보석 'aasm'로 루비 2.1를 사용하고는, '~> 3.1.1'

답변

1

첫째, !:cancelled? 표현은 항상 false로 평가합니다. 코드의 양을 줄이려면 다음과 같이 할 수 있습니다.

transitions :from => :new, :to => :closed, :guard => :cancelled? 
transitions :from => :new, :to => :open, :guard => Proc.new { |ticket| !ticket.cancelled? }