Skip to content

Commit 28b50f2

Browse files
committed
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: [email protected]
1 parent e6e0243 commit 28b50f2

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

admin/app/components/solidus_admin/base_component.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,22 @@ def self.stimulus_id
3434

3535
delegate :stimulus_id, to: :class
3636

37-
def spree
38-
@spree ||= Spree::Core::Engine.routes.url_helpers
37+
class << self
38+
private
39+
40+
def engines_with_routes
41+
Rails::Engine.subclasses.map(&:instance).reject do |engine|
42+
engine.routes.empty?
43+
end
44+
end
3945
end
4046

41-
def solidus_admin
42-
@solidus_admin ||= SolidusAdmin::Engine.routes.url_helpers
47+
# For each engine with routes, define a method that returns the routes proxy.
48+
# This allows us to use the routes in the context of a component class.
49+
engines_with_routes.each do |engine|
50+
define_method(engine.engine_name) do
51+
engine.routes.url_helpers
52+
end
4353
end
4454
end
4555
end

admin/app/components/solidus_admin/layout/navigation/item/component.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<li class="group <%= "active" if active? %>">
22
<a
33
href="<%= path %>"
4-
aria-current="<%= @item.current?(@url_helpers, @fullpath) ? "page" : "false" %>"
4+
aria-current="<%= @item.current?(self, @fullpath) ? "page" : "false" %>"
55
class="
66
flex gap-3 items-center
77
py-1 px-3 rounded
@@ -20,7 +20,7 @@
2020

2121
<% if @item.children? %>
2222
<ul class="flex flex-col gap-0.5 pt-0.5 <%= "hidden" unless active? %>">
23-
<%= render self.class.with_collection(@item.children, url_helpers: @url_helpers, fullpath: @fullpath) %>
23+
<%= render self.class.with_collection(@item.children, fullpath: @fullpath) %>
2424
</ul>
2525
<% end %>
2626
</li>

admin/app/components/solidus_admin/layout/navigation/item/component.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ class SolidusAdmin::Layout::Navigation::Item::Component < SolidusAdmin::BaseComp
66

77
# @param item [SolidusAdmin::MenuItem]
88
# @param fullpath [String] the current path
9-
# @param url_helpers [#solidus_admin, #spree] context for generating paths
9+
# @param url_helpers @see SolidusAdmin::BaseComponent
1010
def initialize(
1111
item:,
12-
fullpath: "#",
13-
url_helpers: Struct.new(:spree, :solidus_admin).new(spree, solidus_admin)
12+
fullpath: "#"
1413
)
1514
@item = item
16-
@url_helpers = url_helpers
1715
@fullpath = fullpath
1816
end
1917

2018
def path
21-
@item.path(@url_helpers)
19+
@item.path(self)
2220
end
2321

2422
def active?
25-
@item.active?(@url_helpers, @fullpath)
23+
@item.active?(self, @fullpath)
2624
end
2725
end

promotions/lib/components/admin/solidus_admin/orders/show/adjustments/index/source/solidus_promotions_benefit/component.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,4 @@ def detail
1010
def promotion_name
1111
source.promotion.name
1212
end
13-
14-
def solidus_promotions
15-
@solidus_promotions ||= SolidusPromotions::Engine.routes.url_helpers
16-
end
1713
end

promotions/lib/components/admin/solidus_promotions/promotions/index/component.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,4 @@ def columns
101101
}
102102
]
103103
end
104-
105-
def solidus_promotions
106-
@solidus_promotions ||= SolidusPromotions::Engine.routes.url_helpers
107-
end
108104
end

0 commit comments

Comments
 (0)