Created
June 9, 2013 02:36
-
-
Save Lordnibbler/5737397 to your computer and use it in GitHub Desktop.
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
# Adds a new item to the order (creating a new order if none already exists) | |
def populate | |
ap "#populate" | |
ap request | |
# clear cart for buy now action if necessary | |
ap params | |
ap @current_order if @current_order | |
@current_order.line_items.destroy_all if @current_order && (params[:order] and params[:order][:buy_now]) | |
# populate an order | |
populator = Spree::OrderPopulator.new(current_order(true), current_currency) | |
if populator.populate(params.slice(:products, :variants, :quantity)) | |
fire_event('spree.cart.add') | |
fire_event('spree.order.contents_changed') | |
if !(params[:order] and params[:order][:buy_now]) | |
# add to cart | |
respond_with(@order) do |format| | |
format.html { redirect_to cart_path } | |
end | |
else | |
# buy now | |
respond_with(@order) do |format| | |
format.html { redirect_to checkout_state_path(populator.order.state) } | |
end | |
end | |
else | |
flash[:error] = populator.errors.full_messages.join(" ") | |
redirect_to :back | |
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
require 'spec_helper' | |
describe Spree::OrdersController do | |
let(:user) { create(:user) } | |
let(:big_order) { create :order_with_line_items, line_items_count: 5 } | |
let(:small_order) { create :order_with_line_items, line_items_count: 1 } | |
context "#populate" do | |
# def spree_post(action, parameters = nil, session = nil, flash = nil) | |
context "buy now" do | |
it "should redirect_to spree.checkout_state_path if params[:order] and params[:order][:buy_now] are present" do | |
small_order.state = "address" | |
controller.instance_variable_set(:@current_order, small_order) | |
spree_post :populate, { :order => { :buy_now => "true" } } | |
response.should redirect_to spree.checkout_state_path(small_order.state) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment