Tony Han
Tony Han

Reputation: 2230

Where's "next" in state_machine of spree order?

In the Spree's source code, there's a next method of 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

But I don't know where's it, I can't find it in the Order model, the Checkout module(https://github.com/spree/spree/blob/master/core/app/models/spree/order/checkout.rb) and the state_machine gem.

Upvotes: 3

Views: 1820

Answers (1)

gmacdougall
gmacdougall

Reputation: 4911

It's defined in state_machine through some trickery. Specifically here:

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

It's hard to find because of the meta-programming available in Ruby, which is used a fair bit in Spree, and extensively in the state_machine gem.

Thankfully, Pry is a great tool for finding where things are defined.

~/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

Upvotes: 4

Related Questions