문제

I'm trying to use the shipment "ready" state as a callback for a custom method, in my "shipment_decorator.rb" I have something like this

Spree::Shipment.class_eval do
  Spree::Shipment.state_machine.after_transition to: :ready, do: :create_external_shipment

  def create_external_shipment
    'my custom method'
  end
end

The problem is that for some reason I don't understand, Spree is not doing my custom method.

I already try with state_machine.after_transition to: :ready, do: :create_external_shipment, self.state_machine.after_transition to: :ready, do: :create_external_shipment, I even copy paste the entire model to make a full override and still is not working.

do I missing something here?

I'm using Spree 2.2 stable and I don't have any extensions adding behavior to Shipments

도움이 되었습니까?

해결책

I'm pretty sure that the shipment state machine is not being used in this case.

Your shipment is likely being set to ready by this:

https://github.com/spree/spree/blob/v2.2.1/core/app/models/spree/shipment.rb#L223-L226

which is called from here:

https://github.com/spree/spree/blob/v2.2.1/core/app/models/spree/order_updater.rb#L21

which is coming from:

https://github.com/spree/spree/blob/v2.2.1/core/app/models/spree/payment.rb#L24

Because this is happening through ActiveRecord callbacks, the state machine is not being invoked and your shipment is being set to ready through other means and not triggering your state machine event. You'll probably need to find an alternate place to hook your logic in.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top