Skip to content

Commit 95fa021

Browse files
Merge pull request rails#45719 from bensheldon/subpath_engine_route_helpers
Allow Mounted Engine url_helpers to use config.relative_url_root
2 parents b8868d6 + 066ce1e commit 95fa021

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def define_generate_prefix(app, name)
652652

653653
script_namer = ->(options) do
654654
prefix_options = options.slice(*_route.segment_keys)
655-
prefix_options[:relative_url_root] = ""
655+
prefix_options[:script_name] = "" if options[:original_script_name]
656656

657657
if options[:_recall]
658658
prefix_options.reverse_merge!(options[:_recall].slice(*_route.segment_keys))

actionpack/lib/action_dispatch/routing/route_set.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,18 +779,14 @@ def generate(route_name, options, recall = {}, method_name = nil)
779779

780780
RESERVED_OPTIONS = [:host, :protocol, :port, :subdomain, :domain, :tld_length,
781781
:trailing_slash, :anchor, :params, :only_path, :script_name,
782-
:original_script_name, :relative_url_root]
782+
:original_script_name]
783783

784784
def optimize_routes_generation?
785785
default_url_options.empty?
786786
end
787787

788788
def find_script_name(options)
789-
options.delete(:script_name) || find_relative_url_root(options) || ""
790-
end
791-
792-
def find_relative_url_root(options)
793-
options.delete(:relative_url_root) || relative_url_root
789+
options.delete(:script_name) || relative_url_root || ""
794790
end
795791

796792
def path_for(options, route_name = nil, reserved = RESERVED_OPTIONS)

railties/test/railties/engine_test.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,11 @@ class Engine < ::Rails::Engine
15111511
app_file "app/controllers/bar_controller.rb", <<-RUBY
15121512
class BarController < ApplicationController
15131513
def index
1514-
render plain: bukkits.bukkit_path
1514+
text = <<~TEXT
1515+
bukkits.bukkit_path: \#{bukkits.bukkit_path}
1516+
Bukkits::Engine.routes.url_helpers.bukkit_path: \#{Bukkits::Engine.routes.url_helpers.bukkit_path}
1517+
TEXT
1518+
render plain: text
15151519
end
15161520
end
15171521
RUBY
@@ -1532,18 +1536,32 @@ def index
15321536
@plugin.write "app/controllers/bukkits/bukkit_controller.rb", <<-RUBY
15331537
class Bukkits::BukkitController < ActionController::Base
15341538
def index
1535-
render plain: main_app.bar_path
1539+
text = <<~TEXT
1540+
main_app.bar_path: \#{main_app.bar_path}
1541+
Rails.application.routes.url_helpers.bar_path: \#{Rails.application.routes.url_helpers.bar_path}
1542+
TEXT
1543+
render plain: text
15361544
end
15371545
end
15381546
RUBY
15391547

15401548
boot_rails
15411549

1550+
expected = <<~TEXT
1551+
main_app.bar_path: /foo/bar
1552+
Rails.application.routes.url_helpers.bar_path: /foo/bar
1553+
TEXT
15421554
get("/bukkits/bukkit", {}, { "SCRIPT_NAME" => "/foo" })
1543-
assert_equal "/foo/bar", last_response.body
1555+
assert_equal expected,
1556+
last_response.body
15441557

1558+
expected = <<~TEXT
1559+
bukkits.bukkit_path: /foo/bukkits/bukkit
1560+
Bukkits::Engine.routes.url_helpers.bukkit_path: /foo/bukkits/bukkit
1561+
TEXT
15451562
get("/bar", {}, { "SCRIPT_NAME" => "/foo" })
1546-
assert_equal "/foo/bukkits/bukkit", last_response.body
1563+
assert_equal expected,
1564+
last_response.body
15471565
end
15481566

15491567
test "isolated engine can be mounted under multiple static locations" do

0 commit comments

Comments
 (0)