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

[Admin] Use Rails.application.mounted_helpers in base component #6039

Merged
merged 2 commits into from
Jan 7, 2025
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: 0 additions & 18 deletions admin/app/components/solidus_admin/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,5 @@ def self.stimulus_id
end

delegate :stimulus_id, to: :class

class << self
private

def engines_with_routes
Rails::Engine.subclasses.map(&:instance).reject do |engine|
engine.routes.empty?
end
end
end

# 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,12 +1,12 @@
<%= page do %>
<% if @tabs %>
<% if tabs %>
<%= page_header do %>
<%= render_title %>
<% end %>

<%= page_header do %>
<% rendered_tabs = capture do %>
<% @tabs.each do %>
<% renderable_tabs.each do %>
<%= render(component("ui/button").new(tag: :a, scheme: :ghost, text: _1.text, 'aria-current': _1.current, href: _1.href)) %>
<% end %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ def columns; []; end

def initialize(page:)
@page = page
@tabs = tabs&.map { |tab| Tab.new(**tab) }
end

def row_fade(_record)
false
end

def renderable_tabs
return unless tabs

tabs.map { |tab| Tab.new(**tab) }
end

def title
model_class.model_name.human.pluralize
end
Expand Down
6 changes: 6 additions & 0 deletions admin/lib/solidus_admin/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,11 @@ class Engine < ::Rails::Engine
SolidusAdmin::Engine.root.join("app/components"),
]
end

initializer "solidus_admin.routing_proxies" do |app|
ActiveSupport.on_load(:after_routes_loaded) do
SolidusAdmin::BaseComponent.include app.routes.mounted_helpers
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end
18 changes: 7 additions & 11 deletions admin/spec/components/solidus_admin/base_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@ def call

describe "#spree" do
it "gives access to spree routing helpers" do
without_partial_double_verification do
allow(Spree::Core::Engine.routes.url_helpers).to receive(:foo_path).and_return("/foo/bar")
end
component = described_class.new

expect(component.spree.foo_path).to eq("/foo/bar")
expect(described_class.new).to respond_to(:spree)
end
end

describe "#solidus_admin" do
it "gives access to solidus_admin routing helpers" do
without_partial_double_verification do
allow(SolidusAdmin::Engine.routes.url_helpers).to receive(:foo_path).and_return("/foo/bar")
end
component = described_class.new
expect(described_class.new).to respond_to(:solidus_admin)
end
end

expect(component.solidus_admin.foo_path).to eq("/foo/bar")
describe "#main_app" do
it "gives access to main_app routing helpers" do
expect(described_class.new).to respond_to(:main_app)
end
end

Expand Down
Loading