Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spree Upgrade] Fix admin new order page flow (from order details to customer details) #3436

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/spree/admin/orders_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ def index
# within the page then fetches the data it needs from Api::OrdersController
end

# Re-implement spree method so that it redirects to edit instead of rendering edit
# This allows page reloads while adding variants to the order (/edit), without being redirected to customer details page (/update)
def update
unless @order.update_attributes(params[:order]) && @order.line_items.present?
@order.errors.add(:line_items, Spree.t('errors.messages.blank')) if @order.line_items.empty?
luisramos0 marked this conversation as resolved.
Show resolved Hide resolved
return redirect_to edit_admin_order_path(@order), :flash => { :error => @order.errors.full_messages.join(', ') }
end

@order.update!
if @order.complete?
redirect_to edit_admin_order_path(@order)
else
# Jump to next step if order is not complete
redirect_to admin_order_customer_path(@order)
end
end

# Overwrite to use confirm_email_for_customer instead of confirm_email.
# This uses a new template. See mailers/spree/order_mailer_decorator.rb.
def resend
Expand Down
82 changes: 61 additions & 21 deletions spec/controllers/spree/admin/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,69 @@
include AuthenticationWorkflow
include OpenFoodNetwork::EmailHelper

context "updating an order with line items" do
let!(:order) { create(:order) }
let(:line_item) { create(:line_item) }
context "#update" do
let(:params) do
{ id: order,
order: { number: order.number,
distributor_id: order.distributor_id,
order_cycle_id: order.order_cycle_id } }
end

before { login_as_admin }

it "updates distribution charges" do
order.line_items << line_item
order.save
Spree::Order.any_instance.should_receive(:update_distribution_charge!)
spree_put :update, {
id: order,
order: {
number: order.number,
distributor_id: order.distributor_id,
order_cycle_id: order.order_cycle_id,
line_items_attributes: [
{
id: line_item.id,
quantity: line_item.quantity
}
]
}
}
context "complete order" do
let(:order) { create :completed_order_with_totals }

it "updates distribution charges and redirects to order details page" do
Spree::Order.any_instance.should_receive(:update_distribution_charge!)

spree_put :update, params

expect(response).to redirect_to spree.edit_admin_order_path(order)
end
end

context "incomplete order" do
let(:order) { create(:order) }
let(:line_item) { create(:line_item) }

context "without line items" do
it "redirects to order details page with flash error" do
spree_put :update, params

expect(flash[:error]).to eq "Line items can't be blank"
expect(response).to redirect_to spree.edit_admin_order_path(order)
end
end

context "with line items" do
before do
order.line_items << line_item
order.save
params[:order][:line_items_attributes] = [{ id: line_item.id, quantity: line_item.quantity }]
end

context "and no errors" do
it "updates distribution charges and redirects to customer details page" do
Spree::Order.any_instance.should_receive(:update_distribution_charge!)

spree_put :update, params

expect(response).to redirect_to spree.admin_order_customer_path(order)
end
end

context "with invalid distributor" do
it "redirects to order details page with flash error" do
params[:order][:distributor_id] = create(:distributor_enterprise).id

spree_put :update, params

expect(flash[:error]).to eq "Distributor or order cycle cannot supply the products in your cart"
expect(response).to redirect_to spree.edit_admin_order_path(order)
end
end
end
end
end

Expand Down