[英]How to trigger an event using state_machine gem in a Rails application
[英]How to handle error in event machine using transitions gem on rails?
我正在Rails 4.2上的事件机(转换gem)上工作,我编写了一个名为send_data
的方法,当状态从暂挂更改为交付时,将触发send_data
。
def send_data
data = { 'remote_id' => order.remote_id,
'items' => order.line_items
}
SendLineItemsToWebshop.call(data)
end
SendLineItemsToWebshop
是另一个类,它调用call
方法并等待一些响应,如果响应来了,则事件将被触发(状态将被更改),否则状态将相同。
require 'bunny'
require 'thread'
class SendLineItemsToWebshop
def self.call(data)
conn = Bunny.new(automatically_recover: false)
conn.start
channel = conn.create_channel
response = call_client(channel, data)
channel.queue(ENV['BLISS_RPC_QUEUE_NAME']).pop
channel.close
conn.close
self.response(response)
end
def self.call_client(channel, data)
client = RpcClient.new(channel, ENV['BLISS_RPC_QUEUE_NAME'])
client.call(data)
end
def self.response(response)
return response
JSON.parse response
end
end
但是问题在于,调用事件deliver
时,它不检查send_data
的响应是否到来,而是更改状态。 这是我的送货事件:
event :deliver do
transitions :to => :delivered, :from => [:editing, :pending] , on_transition: [ :send_data ]
end
但我希望如果响应为false或nil,则过渡状态将不会更改。 仅当响应为真时,状态才会更改。 请帮我解决这个问题。
Transitions宝石具有良好的guard
功能。 添加一个简单的逻辑测试,如can_be_delivered?
并尝试这个:
event :deliver do
transitions :to => :delivered, :from => [:editing, :pending], guard: [:can_be_delivered?]
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.