Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(Admin): Dynamic routing proxies #5933

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -38,12 +38,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,19 @@ 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
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