2015-06-23 54 views
0
state_machine :device_state, :attribute => :device_state, :initial => :unregistered do 
    state :unregistered 
    state :registered 
    state :compliancy_pending 
    state :not_compliant 
    state :end_of_contract 

    after_transition all => :registered do |device, transition| 
     device.update_attributes! :gcm_error => nil 
    end 

    after_failure do |device, transition| 
     Rails.logger.warn "Device #{device.serial_number} failed to transition on #{transition.event} with state #{transition.from_name}" 
    end 

    event :unregister_device do 
     transition all => :unregistered 
    end 

    event :end_of_contract_device do 
     transition all => :end_of_contract 
    end 

    event :register_device do 
     transition [:unregistered, :registered] => :registered 
     transition :compliancy_pending => :compliancy_pending 
     transition :not_compliant => :not_compliant 
    end 

    event :requires_dispatch do 
     transition [:registered, :compliancy_pending, :not_compliant] => :registered 
    end 

    event :dispatching do 
     transition :registered => :compliancy_pending 
     transition [:compliancy_pending] => :compliancy_pending, :unless => :notification_timed_out? 
     transition [:registered, :compliancy_pending] => :not_compliant, :if => :notification_timed_out? #gcm_timed_out 
    end 

    event :device_responded do 
     transition [:registered, :not_compliant, :compliancy_pending] => :registered 
    end 
    end 

以上是狀態機,此事件也是在此模型中指定的。 這個模型是device.rb如何從模型A中存在的模型B觸發事件

但我從wap從end_of_contract模型中引發事件。

可以請你告訴我如何在另一個模塊中使用這個狀態機來更新狀態。

說明: 有一個名爲device_state的列,屬於設備模型(device.rb)。 我也有一個模型end_of_contract.rb和end_of_contractscontroller.rb。 但我的任務是,當我調用end_of_contract操作時,它應該觸發device.rb中存在的事件,然後將此device.rb中的狀態更改爲end_of_contract。

最後,我是新來的鐵軌。 。 。

回答

0

只要您有設備的實例,您應該可以從任何地方調用該事件,例如,

@device = Device.find(params[:device_id]) 
@device.end_of_contract_device 
相關問題