Skip to content

Commit

Permalink
Extract common admin resources patters to a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Dec 4, 2023
1 parent 8a2da21 commit b90b31b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 73 deletions.
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

0 comments on commit b90b31b

Please sign in to comment.