From 28b50f2ef8b6c6c17d1087bb2a6cbed989a46587 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Fri, 15 Nov 2024 21:32:12 +0100 Subject: [PATCH] Feat(Admin): Dynamic routing proxies This commit allows the menu of the new admin to accomodate routes from other engines than solidus backend and solidus admin. This is needed for `solidus_promotions`, which is built as a separate Rails Engine, but it is also convenient for `solidus_paypal_commerce_platform` or even for integrating gems like AlchemyCMS lateron. Co-Authored-By: thomas@vondeyen.com --- .../components/solidus_admin/base_component.rb | 18 ++++++++++++++---- .../layout/navigation/item/component.html.erb | 4 ++-- .../layout/navigation/item/component.rb | 10 ++++------ .../solidus_promotions_benefit/component.rb | 4 ---- .../promotions/index/component.rb | 4 ---- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/admin/app/components/solidus_admin/base_component.rb b/admin/app/components/solidus_admin/base_component.rb index 7bfe1b52852..4843c5c0f90 100644 --- a/admin/app/components/solidus_admin/base_component.rb +++ b/admin/app/components/solidus_admin/base_component.rb @@ -34,12 +34,22 @@ def self.stimulus_id delegate :stimulus_id, to: :class - def spree - @spree ||= Spree::Core::Engine.routes.url_helpers + class << self + private + + def engines_with_routes + Rails::Engine.subclasses.map(&:instance).reject do |engine| + engine.routes.empty? + end + end end - def solidus_admin - @solidus_admin ||= SolidusAdmin::Engine.routes.url_helpers + # For each engine with routes, define a method that returns the routes proxy. + # This allows us to use the routes in the context of a component class. + engines_with_routes.each do |engine| + define_method(engine.engine_name) do + engine.routes.url_helpers + end end end end diff --git a/admin/app/components/solidus_admin/layout/navigation/item/component.html.erb b/admin/app/components/solidus_admin/layout/navigation/item/component.html.erb index d3270a08c07..575872b242b 100644 --- a/admin/app/components/solidus_admin/layout/navigation/item/component.html.erb +++ b/admin/app/components/solidus_admin/layout/navigation/item/component.html.erb @@ -1,7 +1,7 @@
  • "> " + aria-current="<%= @item.current?(self, @fullpath) ? "page" : "false" %>" class=" flex gap-3 items-center py-1 px-3 rounded @@ -20,7 +20,7 @@ <% if @item.children? %>
      "> - <%= render self.class.with_collection(@item.children, url_helpers: @url_helpers, fullpath: @fullpath) %> + <%= render self.class.with_collection(@item.children, fullpath: @fullpath) %>
    <% end %>
  • diff --git a/admin/app/components/solidus_admin/layout/navigation/item/component.rb b/admin/app/components/solidus_admin/layout/navigation/item/component.rb index 17ac8a62f74..4cf71cf7466 100644 --- a/admin/app/components/solidus_admin/layout/navigation/item/component.rb +++ b/admin/app/components/solidus_admin/layout/navigation/item/component.rb @@ -6,22 +6,20 @@ class SolidusAdmin::Layout::Navigation::Item::Component < SolidusAdmin::BaseComp # @param item [SolidusAdmin::MenuItem] # @param fullpath [String] the current path - # @param url_helpers [#solidus_admin, #spree] context for generating paths + # @param url_helpers @see SolidusAdmin::BaseComponent def initialize( item:, - fullpath: "#", - url_helpers: Struct.new(:spree, :solidus_admin).new(spree, solidus_admin) + fullpath: "#" ) @item = item - @url_helpers = url_helpers @fullpath = fullpath end def path - @item.path(@url_helpers) + @item.path(self) end def active? - @item.active?(@url_helpers, @fullpath) + @item.active?(self, @fullpath) end end diff --git a/promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/source/solidus_promotions_benefit/component.rb b/promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/source/solidus_promotions_benefit/component.rb index 3f24c7c478f..0ceee3408cf 100644 --- a/promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/source/solidus_promotions_benefit/component.rb +++ b/promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/source/solidus_promotions_benefit/component.rb @@ -10,8 +10,4 @@ def detail def promotion_name source.promotion.name end - - def solidus_promotions - @solidus_promotions ||= SolidusPromotions::Engine.routes.url_helpers - end end diff --git a/promotions/lib/components/admin/solidus_promotions/promotions/index/component.rb b/promotions/lib/components/admin/solidus_promotions/promotions/index/component.rb index 19cf4b79772..4d64c3164e9 100644 --- a/promotions/lib/components/admin/solidus_promotions/promotions/index/component.rb +++ b/promotions/lib/components/admin/solidus_promotions/promotions/index/component.rb @@ -101,8 +101,4 @@ def columns } ] end - - def solidus_promotions - @solidus_promotions ||= SolidusPromotions::Engine.routes.url_helpers - end end