Use RESTful routes for orders controller actions

This commit is contained in:
Matt-Yorkley
2023-06-06 15:03:55 +01:00
parent 2bfb57a1a1
commit 67c3e09dba
7 changed files with 24 additions and 90 deletions

View File

@@ -8,9 +8,8 @@ module Spree
include OpenFoodNetwork::SpreeApiKeyLoader
helper CheckoutHelper
before_action :load_order, only: [:edit, :update, :fire, :resend,
:invoice, :print, :distribution]
before_action :load_distribution_choices, only: [:new, :edit, :update, :distribution]
before_action :load_order, only: [:edit, :update, :fire, :resend, :invoice, :print]
before_action :load_distribution_choices, only: [:new, :create, :edit, :update]
before_action :require_distributor_abn, only: :invoice
before_action :restore_saved_query!, only: :index
@@ -24,22 +23,7 @@ module Spree
end
def new
@order = Order.create
@order.created_by = spree_current_user
@order.generate_order_number
@order.save
redirect_to spree.distribution_admin_order_path(@order)
end
def distribution
return if order_params.blank?
on_update
@order.assign_attributes(order_params)
return unless @order.save(context: :set_distribution_step)
redirect_to spree.admin_order_customer_path(@order)
@order = Spree::Order.new
end
def edit
@@ -47,6 +31,16 @@ module Spree
@order.errors.clear
end
def create
@order = Spree::Order.new(order_params.merge(created_by: spree_current_user))
if @order.save(context: :require_distribution)
redirect_to spree.admin_order_customer_path(@order)
else
render :new
end
end
def update
on_update

View File

@@ -104,8 +104,9 @@ module Spree
validates :email, presence: true,
format: /\A([\w.%+\-']+)@([\w\-]+\.)+(\w{2,})\z/i,
if: :require_email
validates :order_cycle, presence: true, on: :set_distribution_step
validates :distributor, presence: true, on: :set_distribution_step
validates :order_cycle, presence: true, on: :require_distribution
validates :distributor, presence: true, on: :require_distribution
make_permalink field: :number

View File

@@ -1,27 +0,0 @@
- content_for :page_actions do
%li= button_link_to t(:back_to_orders_list), spree.admin_orders_path, :icon => 'icon-arrow-left'
= admin_inject_shops(module: 'admin.orders')
= admin_inject_order_cycles
- content_for :page_title do
= t(:new_order)
\#
= @order.number
= render 'spree/admin/shared/order_tabs', :current => 'Customer Details'
= csrf_meta_tags
%div
= render 'spree/shared/error_messages', :target => @order
%div{"ng-app" => "admin.orders", "ng-controller" => "orderCtrl"}
= form_for @order, url: distribution_admin_order_path(@order), method: :put do |f|
= render 'spree/admin/orders/_form/distribution_fields'
-# This param passed to stop validation error in next page due to no line items in order yet:
= hidden_field_tag 'suppress_error_msg', "true"
= hidden_field_tag "set_distribution_step", "true"
= button_tag :class => 'secondary radius expand small', :id => 'update-button' do
%i.icon-arrow-right
= t(:next)

View File

@@ -1,7 +1,5 @@
- content_for :page_title do
= t(:new_order)
\#
= @order.number
- content_for :page_actions do
%li= button_link_to t(:back_to_orders_list), spree.admin_orders_path, :icon => 'icon-arrow-left'
@@ -9,17 +7,14 @@
= admin_inject_shops(module: 'admin.orders')
= admin_inject_order_cycles
= render 'spree/admin/shared/order_tabs', :current => 'Order Details'
= csrf_meta_tags
= render 'spree/admin/shared/order_tabs', current: 'Order Details'
%div
= render 'spree/shared/error_messages', :target => @order
= render 'spree/shared/error_messages', target: @order
%div{"ng-app" => "admin.orders", "ng-controller" => "orderCtrl"}
%div{"ng-show" => "distributionChosen()"}
= render 'add_product'
- unless @order.line_items.any?
%div
= render 'form'
= form_for @order, url: spree.admin_orders_path, method: :post do
= render 'spree/admin/orders/_form/distribution_fields'
= button_tag class: 'secondary radius expand small', id: 'update-button' do
%i.icon-arrow-right
= t(:next)

View File

@@ -10,6 +10,6 @@
:taxons_search => main_app.api_v0_taxons_url(:format => 'json'),
:orders_api => main_app.api_v0_orders_url,
:states_search => main_app.api_v0_states_url(:format => 'json'),
:cancel_order => spree.fire_admin_order_url(id: @order ? @order.number : "", e: 'cancel')
:cancel_order => spree.fire_admin_order_url(id: @order&.number ? @order.number : "", e: 'cancel')
}.to_json %>;
</script>

View File

@@ -89,8 +89,6 @@ Spree::Core::Engine.routes.draw do
get :resend
get :invoice
get :print
get :distribution
put :distribution
end
collection do

View File

@@ -116,33 +116,6 @@ describe '
end
end
context "when order has a distributor and order cycle" do
before do
order.distributor = distributor
order.order_cycle = order_cycle
order.save!
login_as_admin
visit spree.distribution_admin_order_path(order)
end
it "can access the `/distribution` step" do
expect(current_path).to eq spree.distribution_admin_order_path(order)
expect(page).to have_content "DISTRIBUTION"
end
it "shows the distributor and order cycle" do
expect(page).to have_content distributor.name
expect(page).to have_content order_cycle.name
end
it "shows links to other steps" do
expect(page).to have_content "CUSTOMER DETAILS"
expect(page).to have_content "ORDER DETAILS"
expect(page).to have_content "PAYMENTS"
expect(page).to have_content "ADJUSTMENTS"
end
end
context "when creating an order with a customer-only" do
let(:customer2) { create(:customer, enterprise: distributor) }
let(:customer3) { create(:customer, enterprise: distributor) }