反复检查子对象是否已更改

时间:2016-02-16 21:08:11

标签: ruby-on-rails

所以我刚刚开始使用状态机。我的一个要求是发送请求,并等待回复。请求创建子属性,收件人可以拒绝或接受。

我想要监听的内容在childs approved属性中更改,这是一个布尔值。如果是,则进入X,否则进入Y.

现在我想弄清楚如何编写一个我可以在状态机中使用的方法,它基本上检查子属性是否已经改变(比如它等待并检查10秒)。我不介意让用户等待。

目前使用AASM的状态机看起来像这样:

  aasm column: 'state' do
    state :pending, initial: true
    state :connecting
    state :unavailable
    state :requesting
    state :declined
    state :processing
    state :finished
    state :errored

    event :connect, after: :find_recipient do
      transitions from: :pending, to: :connecting
    end

    event :unavailable do
      transitions from: :connecting, to: :unavailable
    end

    event :request do
      transitions from: :connecting, to: :requesting
    end

    event :decline do
      transitions from: :requesting, to: :declined
    end

    # if recipient is found
    event :process, after: :charge_card do
      transitions from: :connecting, to: :processing
    end

    # payment succeeded
    event :finish, after: :track_payment do
      transitions from: :processing, to: :finished
    end

    # payment failed
    event :fail do
      transitions from: :processing, to: :errored
    end
  end

所以目前我有一个后台工作,在创建Order时启动(这是这个模型。它使用order.connect!启动状态机

现在它转移到requesting,所以我想创建一个基本上类似的方法:

def await_reply
  # access self.request and see the value of request.approved 
  # if approved haven't changed then proceed to self.unavailable!
  # if approved is true then proceed to self.process!
  # if approved is false then proceed to self.decline!
end

1 个答案:

答案 0 :(得分:0)

你为什么要等? 您可以使用order.approved?来检查它是否已获批准。