ruby-on-rails - spree 订单的 state_machine 中的 "next"在哪里?

标签 ruby-on-rails spree

在Spree的源码中,有一个next的order方法

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 中定义的。具体在这里:

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

关于ruby-on-rails - spree 订单的 state_machine 中的 "next"在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20485114/

相关文章:

ruby-on-rails - ActionController 回调过滤器链是否一致?

ruby-on-rails-4 - 回形针无法更改默认路径

ruby-on-rails - Rails、Paperclip 和负载均衡器

ruby-on-rails - 构建一个 Rails 表单来过滤索引页面?

ruby-on-rails - 甚至更好地支持 Netbeans 中的 HAML?

ruby-on-rails - Rails 中的错误消息

ruby-on-rails - 如何向 Ruby on Rails Spree 商务应用程序添加新 View ?

ruby-on-rails - 如何从 spree 中删除模型验证

ruby-on-rails - rails : undefined method `model_name' for Fixnum:Class

javascript - 将 JS 变量传递给 Rails 中的 Controller