From 920672ca6e86ce75c6415bdb68fc6fdd1eb1ea0d Mon Sep 17 00:00:00 2001 From: Daniele Palombo <387690+DanielePalombo@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:21:24 +0100 Subject: [PATCH] Configure backend only if available --- .../spree/admin/prototypes_controller.rb | 37 +++++++++++-------- config/initializers/spree.rb | 28 +++++++------- .../admin/products_controller_decorator.rb | 2 +- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/app/controllers/spree/admin/prototypes_controller.rb b/app/controllers/spree/admin/prototypes_controller.rb index 63b2849..8075d03 100644 --- a/app/controllers/spree/admin/prototypes_controller.rb +++ b/app/controllers/spree/admin/prototypes_controller.rb @@ -2,26 +2,31 @@ module Spree module Admin - class PrototypesController < ResourceController - def show - if request.xhr? - render layout: false - else - redirect_to admin_prototypes_path + if defined? ::Spree::Backend + class PrototypesController < ResourceController + def show + if request.xhr? + render layout: false + else + redirect_to admin_prototypes_path + end end - end - def available - @prototypes = Prototype.order('name asc') - respond_with(@prototypes) do |format| - format.html { render layout: !request.xhr? } - format.js + def available + @prototypes = Prototype.order('name asc') + respond_with(@prototypes) do |format| + format.html { render layout: !request.xhr? } + format.js + end end - end - def select - @prototype ||= Prototype.find(params[:id]) - @prototype_properties = @prototype.properties + def select + @prototype ||= Prototype.find(params[:id]) + @prototype_properties = @prototype.properties + end + end + else + class PrototypesController end end end diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index f4eb050..dd1ca54 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -1,18 +1,20 @@ Rails.application.config.to_prepare do Spree::PermittedAttributes.product_attributes << :prototype_id - Spree::Backend::Config.configure do |config| - product_tab = config.menu_items.detect { |menu_item| - menu_item.label == :products - } - if product_tab.respond_to?(:children) - product_tab.children << Spree::BackendConfiguration::MenuItem.new( - condition: -> { can?(:admin, Spree::Prototype) }, - url: :admin_prototypes_path, - label: :prototypes, - match_path: '/prototypes' - ) - else - product_tab.sections << :prototypes + if defined?(Spree::Backend) + Spree::Backend::Config.configure do |config| + product_tab = config.menu_items.detect { |menu_item| + menu_item.label == :products + } + if product_tab.respond_to?(:children) + product_tab.children << Spree::BackendConfiguration::MenuItem.new( + condition: -> { can?(:admin, Spree::Prototype) }, + url: :admin_prototypes_path, + label: :prototypes, + match_path: '/prototypes' + ) + else + product_tab.sections << :prototypes + end end end end diff --git a/lib/backend/controllers/spree/admin/products_controller_decorator.rb b/lib/backend/controllers/spree/admin/products_controller_decorator.rb index 589bdbc..2199e26 100644 --- a/lib/backend/controllers/spree/admin/products_controller_decorator.rb +++ b/lib/backend/controllers/spree/admin/products_controller_decorator.rb @@ -19,7 +19,7 @@ def load_prototype @prototype = Spree::Prototype.find(params[resource][:prototype_id]) end - ::Spree::Admin::ProductsController.prepend self + ::Spree::Admin::ProductsController.prepend self if defined? ::Spree::Admin::ProductsController end end end