Skip to content

Commit

Permalink
Feat(Admin): Dynamic routing proxies
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
mamhoff committed Nov 15, 2024
1 parent e6e0243 commit 28b50f2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
18 changes: 14 additions & 4 deletions admin/app/components/solidus_admin/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<li class="group <%= "active" if active? %>">
<a
href="<%= path %>"
aria-current="<%= @item.current?(@url_helpers, @fullpath) ? "page" : "false" %>"
aria-current="<%= @item.current?(self, @fullpath) ? "page" : "false" %>"
class="
flex gap-3 items-center
py-1 px-3 rounded
Expand All @@ -20,7 +20,7 @@

<% if @item.children? %>
<ul class="flex flex-col gap-0.5 pt-0.5 <%= "hidden" unless active? %>">
<%= render self.class.with_collection(@item.children, url_helpers: @url_helpers, fullpath: @fullpath) %>
<%= render self.class.with_collection(@item.children, fullpath: @fullpath) %>
</ul>
<% end %>
</li>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,4 @@ def columns
}
]
end

def solidus_promotions
@solidus_promotions ||= SolidusPromotions::Engine.routes.url_helpers
end
end

0 comments on commit 28b50f2

Please sign in to comment.