狂欢顺序的state_machine中的“next”在哪里?

时间:2013-12-10 02:48:55

标签: ruby-on-rails spree

在Spree的源代码中,有一个next订购方法

def update
  if @order.update_attributes(object_params)
    persist_user_address

    unless @order.next
      flash[:error] = @order.errors.full_messages.join("\n")
      redirect_to checkout_state_path(@order.state) and return
    end
# ...
end

但我不知道它在哪里,我无法在Order模型中找到它,Checkout模块(https://github.com/spree/spree/blob/master/core/app/models/spree/order/checkout.rb)和state_machine gem。

1 个答案:

答案 0 :(得分:3)

它通过一些技巧在state_machine中定义。具体在这里:

https://github.com/pluginaweek/state_machine/blob/8a3ba81801782ac4ef9d4ace1209d8d50ffccdb0/lib/state_machine/machine.rb#L764-L766

很难找到因为Ruby中可用的元编程,它在Spree中使用得相当多,而且在state_machine gem中广泛使用。

值得庆幸的是,Pry是查找内容定位的绝佳工具。

~/d/s/sandbox (2-0-stable|✔) ❯❯❯ pry -r ./config/environment
[1] pry(main)> show-source Spree::Order.first.next

From: /home/gmacdougall/.rvm/gems/ruby-1.9.3-p392/gems/state_machine-1.2.0/lib/state_machine/machine.rb @ line 764:
Owner: Spree::Order :state instance helpers
Visibility: public
Number of lines: 3

define_method(method) do |*block_args|
  block.call((scope == :instance ? self.class : self).state_machine(name), self, *block_args)
end