2011-11-16 2 views
2

저는 AASM을 사용하고 있습니다. 전환으로 정의 된 이벤트가 있습니다. 이벤트가 발생하고 모델이 from 상태 인 경우 작동합니다. 그러나 모델이 다른 상태에 있으면 InValidTransition 예외가 발생합니다.이벤트가 현재 상태에 적용 가능하지 않은 경우 (전환이 정의되지 않음) AASM에 이벤트를 알려주는 방법?

aasm_state :first 
aasm_sate :second 
aasm_state :third 

aasm_event :myevent do 
    transitions :from => :second, :to => :third 
end 

이제 mymodel.myevent! mymodel이있는 경우 : 첫 번째 또는 세 번째 상태 인 경우 aasm은 InValidTransition을 throw합니다. 이 주들에서 행사를 무시하도록 아삼에게 어떻게 말할 수 있습니까?

답변

0

상태 시스템의 포인트는 전환 할 수있는 상태를 제한하려는 것입니다. 위의 기능을 원하면 왜 상태 시스템의 제약 조건을 사용하고 있습니까? 당신은

def myevent 
    self.update_attribute(:state, 'third') if self.state == 'second' 
end 

로에게 동일한 작업을 수행 할 수 또는 당신이 당신이 원하는 것을해야 할 상태 머신을

aasm_event :myevent do 
    transitions :to => :second, :from => [:second] 
    transitions :to => :second, :from => [:first] 
end 
3
aasm :column => :state, :whiny_transitions => false do 
state :first 
state :second 
state :third 

event :myevent do 
    transitions :from => :second, :to => :third 
end 
end 

을 계속 사용하려는 경우이 작업을 수행 할 수 있습니다.

0

당신이 얻고 싶은 경우에 현재 상태에 따라 AASM 개체의 허용 이벤트는 할 수있다 :

#assuming your aasm object is saved to 'dummy' variable dummy.aasm.events(permitted: true)

당신은 또한하여 이름을 얻을 수 있습니다

dummy.aasm.events(permitted: true).map(&:name)