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

[admin] Extract common admin resources patterns to a helper #5534

Merged
merged 1 commit into from
Dec 4, 2023
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
93 changes: 20 additions & 73 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
23 changes: 23 additions & 0 deletions admin/lib/solidus_admin/admin_resources.rb
Original file line number Diff line number Diff line change
@@ -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
Loading