diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 7513e01a28e..f28ab79387b 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -1,25 +1,25 @@ # frozen_string_literal: true SolidusAdmin::Engine.routes.draw do + require "solidus_admin/admin_resources" + extend SolidusAdmin::AdminResources + resource :account, only: :show + resources :countries, only: [] do + get 'states', to: 'countries#states' + end - resources( - :products, - only: [:index, :show, :edit, :update], - constraints: ->{ _1.path != "/admin/products/new" }, - ) do + # Needs a constraint to avoid interpreting "new" as a product's slug + admin_resources :products, only: [ + :index, :show, :edit, :update, :destroy + ], constraints: ->{ _1.path != "/admin/products/new" } do collection do - delete :destroy put :discontinue put :activate end end - resources :countries, only: [] do - get 'states', to: 'countries#states' - end - - resources :orders, except: [:destroy] do + admin_resources :orders, except: [:destroy] do resources :line_items, only: [:destroy, :create, :update] resource :customer resource :ship_address, only: [:show, :edit, :update], controller: "addresses", type: "ship" @@ -31,66 +31,13 @@ end end - resources :users, only: [:index] do - collection do - delete :destroy - end - end - - resources :promotions, only: [:index] do - collection do - delete :destroy - end - end - - resources :properties, only: [:index] do - collection do - delete :destroy - end - end - - resources :option_types, only: [:index] do - collection do - delete :destroy - end - member do - patch :move - end - end - - resources :taxonomies, only: [:index] do - collection do - delete :destroy - end - member do - patch :move - end - end - - resources :promotion_categories, only: [:index] do - collection do - delete :destroy - end - end - - resources :tax_categories, only: [:index] do - collection do - delete :destroy - end - end - - resources :tax_rates, only: [:index] do - collection do - delete :destroy - end - end - - resources :payment_methods, only: [:index] do - collection do - delete :destroy - end - member do - patch :move - end - end + admin_resources :users, only: [:index, :destroy] + admin_resources :promotions, only: [:index, :destroy] + admin_resources :properties, only: [:index, :destroy] + admin_resources :option_types, only: [:index, :destroy], sortable: true + admin_resources :taxonomies, only: [:index, :destroy], sortable: true + admin_resources :promotion_categories, only: [:index, :destroy] + admin_resources :tax_categories, only: [:index, :destroy] + admin_resources :tax_rates, only: [:index, :destroy] + admin_resources :payment_methods, only: [:index, :destroy], sortable: true end diff --git a/admin/lib/solidus_admin/admin_resources.rb b/admin/lib/solidus_admin/admin_resources.rb new file mode 100644 index 00000000000..106e9b7ae92 --- /dev/null +++ b/admin/lib/solidus_admin/admin_resources.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module SolidusAdmin::AdminResources + def admin_resources(resource, **options) + batch_actions = %i[destroy] + batch_actions &= options[:only] if options[:only] + batch_actions -= options[:except] if options[:except] + + resources(resource, options) do + yield if block_given? + + collection do + delete :destroy if batch_actions.include?(:destroy) + end + + member do + patch :move if options[:sortable] + end + + yield if block_given? + end + end +end