Created
June 10, 2014 20:49
-
-
Save djones/80394768b4e2cd0505ab to your computer and use it in GitHub Desktop.
Handoff in iOS 8 and OS X Yosemite with Spree Commerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate this migration by running: | |
# | |
# bundle exec rails g migration add_handoff_to_spree_orders handoff:string | |
# bundle exec rake db:migrate | |
# | |
# db/migrate/add_handoff_to_orders.rb | |
class AddHandoffToOrders < ActiveRecord::Migration | |
def change | |
add_column :spree_orders, :handoff, :string | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/spree/base_controller_decorator.rb | |
Spree::BaseController.class_eval do | |
before_action :handoff, :if => proc { params[:o].present? } | |
def url_options | |
return super unless current_order.present? | |
{ o: current_order.number }.merge(super) | |
end | |
protected | |
def handoff | |
if (source = Spree::Order.find_by_number(params[:o])) | |
current_order(create_order_if_necessary: true) | |
if current_order.handoff != params[:o] and source.number != current_order.number | |
transfer_order_contents(source, current_order) | |
end | |
end | |
end | |
def transfer_order_contents(source, destination) | |
populator = Spree::OrderPopulator.new(destination, current_currency) | |
destination.empty! | |
source.line_items.each do |li| | |
populator.populate(li.variant_id, li.quantity) | |
end | |
destination.ensure_updated_shipments | |
destination.update_attribute(:handoff, params[:o]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment