在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。
它是通过一些技巧在state_machine中定义的。具体在这里:
由于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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句