Skip to content

Instantly share code, notes, and snippets.

@simmsy
Created March 23, 2014 20:33
Show Gist options
  • Save simmsy/9729425 to your computer and use it in GitHub Desktop.
Save simmsy/9729425 to your computer and use it in GitHub Desktop.
# Shipment
def after_ship
Spree::Config.shipment_handler_class.factory(self).perform
end
module Spree
class ShipmentHandler
class << self
def factory(shipment)
if sm_handler = "Spree::ShipmentHandler::#{shipment.shipping_method.to_s.split('::').last}".constantize rescue false
sm_handler.new(shipment)
else
new(shipment)
end
end
end
def initialize(shipment)
@shipment = shipment
end
def perform
@shipment.inventory_units.each &:ship!
@shipment.send_shipped_email
@shipment.touch :shipped_at
@shipment.update_order_shipment_state
end
end
end
module Spree
module ShipmentHandler
class DigitalDownload < Spree::ShipmentHandler
def perform
# Custom logic here
super
end
end
end
end
# Or totally override everything and set a new Shipment Handler and write a custom factory method
module App
class ShipmentHandler
class << self
def factory(shipment)
# Custom logic
# The only requirement forced by the implementation is that object returned responds to a perform method
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment