Re implement orders ctrl update method so we can change redirection logic and add specs for it

This commit is contained in:
luisramos0
2019-01-31 20:55:28 +00:00
parent 8fdd1db2c3
commit ad0e5bb093
2 changed files with 78 additions and 21 deletions

View File

@@ -27,6 +27,23 @@ Spree::Admin::OrdersController.class_eval do
# 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?
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

View File

@@ -4,29 +4,69 @@ describe Spree::Admin::OrdersController, type: :controller do
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