Skip to content

Commit

Permalink
Merge pull request solidusio#1440 from mtomov/improve-populate-for-ajax
Browse files Browse the repository at this point in the history
Improve JS handling for OrdersController
  • Loading branch information
jhawthorn authored Sep 29, 2016
2 parents b0ee963 + a1be722 commit 9912c99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
36 changes: 20 additions & 16 deletions frontend/app/controllers/spree/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def show

def update
if @order.contents.update_cart(order_params)
@order.next if params.key?(:checkout) && @order.cart?

respond_with(@order) do |format|
format.html do
if params.key?(:checkout)
@order.next if @order.cart?
redirect_to checkout_state_path(@order.checkout_steps.first)
else
redirect_to cart_path
Expand All @@ -41,27 +42,30 @@ def edit

# Adds a new item to the order (creating a new order if none already exists)
def populate
order = current_order(create_order_if_necessary: true)
@order = current_order(create_order_if_necessary: true)
variant = Spree::Variant.find(params[:variant_id])
quantity = params[:quantity].to_i

# 2,147,483,647 is crazy. See issue https://github.com/spree/spree/issues/2695.
if quantity.between?(1, 2_147_483_647)
begin
order.contents.add(variant, quantity)
rescue ActiveRecord::RecordInvalid => e
error = e.record.errors.full_messages.join(", ")
end
else
error = Spree.t(:please_enter_reasonable_quantity)
if !quantity.between?(1, 2_147_483_647)
@order.errors.add(:base, Spree.t(:please_enter_reasonable_quantity))
end

if error
flash[:error] = error
redirect_back_or_default(spree.root_path)
else
respond_with(order) do |format|
format.html { redirect_to cart_path }
begin
@line_item = @order.contents.add(variant, quantity)
rescue ActiveRecord::RecordInvalid => e
@order.errors.add(:base, e.record.errors.full_messages.join(", "))
end

respond_with(@order) do |format|
format.html do
if @order.errors.any?
flash[:error] = @order.errors.full_messages.join(", ")
redirect_back_or_default(spree.root_path)
return
else
redirect_to cart_path
end
end
end
end
Expand Down
14 changes: 13 additions & 1 deletion frontend/spec/controllers/spree/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
it "should create a new order when none specified" do
post :populate
expect(cookies.signed[:guest_token]).not_to be_blank
expect(Spree::Order.find_by_guest_token(cookies.signed[:guest_token])).to be_persisted

order_by_token = Spree::Order.find_by_guest_token(cookies.signed[:guest_token])
assigned_order = assigns[:order]

expect(assigned_order).to eq order_by_token
expect(assigned_order).to be_persisted
end

context "with Variant" do
Expand Down Expand Up @@ -85,6 +90,13 @@
put :update, session: { order_id: 1 }
expect(response).to redirect_to(spree.cart_path)
end

it "should advance the order if :checkout button is pressed" do
allow(order).to receive(:update_attributes).and_return true
expect(order).to receive(:next)
put :update, { checkout: true }, { order_id: 1 }
expect(response).to redirect_to checkout_state_path('address')
end
end
end

Expand Down

0 comments on commit 9912c99

Please sign in to comment.