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
但是我想从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 :(得分:0)
只要您拥有设备实例,就应该能够从任何地方调用该事件,例如
@device = Device.find(params[:device_id])
@device.end_of_contract_device