Skip to content

Commit aa1fadd

Browse files
committed
Deprecate the only_path option on *_path helpers.
In cases where this option is set to `true`, the option is redundant and can be safely removed; otherwise, the corresponding `*_url` helper should be used instead. Fixes #17294. See also #17363. [Dan Olson, Godfrey Chan]
1 parent 8607577 commit aa1fadd

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

actionpack/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Deprecate the `only_path` option on `*_path` helpers.
2+
3+
In cases where this option is set to `true`, the option is redundant and can
4+
be safely removed; otherwise, the corresponding `*_url` helper should be
5+
used instead.
6+
7+
Fixes #17294.
8+
9+
*Dan Olson*, *Godfrey Chan*
10+
111
* Improve Journey compliance to RFC 3986.
212

313
The scanner in Journey failed to recognize routes that use literals

actionpack/lib/action_dispatch/routing/route_set.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ def add(name, route)
134134
@url_helpers_module.send :undef_method, url_name
135135
end
136136
routes[key] = route
137-
define_url_helper @path_helpers_module, route, path_name, route.defaults, name, PATH
138-
define_url_helper @url_helpers_module, route, url_name, route.defaults, name, FULL
137+
define_url_helper @path_helpers_module, route, path_name, route.defaults, name, LEGACY
138+
define_url_helper @url_helpers_module, route, url_name, route.defaults, name, UNKNOWN
139139

140140
@path_helpers << path_name
141141
@url_helpers << url_name
@@ -322,6 +322,30 @@ def define_url_helper(mod, route, name, opts, route_key, url_strategy)
322322
PATH = ->(options) { ActionDispatch::Http::URL.path_for(options) }
323323
FULL = ->(options) { ActionDispatch::Http::URL.full_url_for(options) }
324324
UNKNOWN = ->(options) { ActionDispatch::Http::URL.url_for(options) }
325+
LEGACY = ->(options) {
326+
if options.key?(:only_path)
327+
if options[:only_path]
328+
ActiveSupport::Deprecation.warn \
329+
"You are calling a `*_path` helper with the `only_path` option " \
330+
"expicitly set to `true`. This option will stop working on " \
331+
"path helpers in Rails 5. Simply remove the `only_path: true` " \
332+
"argument from your call as it is redundant when applied to a " \
333+
"path helper."
334+
335+
PATH.call(options)
336+
else
337+
ActiveSupport::Deprecation.warn \
338+
"You are calling a `*_path` helper with the `only_path` option " \
339+
"expicitly set to `false`. This option will stop working on " \
340+
"path helpers in Rails 5. Use the corresponding `*_url` helper " \
341+
"instead."
342+
343+
FULL.call(options)
344+
end
345+
else
346+
PATH.call(options)
347+
end
348+
}
325349
# :startdoc:
326350

327351
attr_accessor :formatter, :set, :named_routes, :default_scope, :router

actionpack/test/dispatch/routing/route_set_test.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,86 @@ def call(env)
6969
end
7070
end
7171

72+
test "only_path: true with *_url and no :host option" do
73+
draw do
74+
get 'foo', to: SimpleApp.new('foo#index')
75+
end
76+
77+
assert_equal '/foo', url_helpers.foo_url(only_path: true)
78+
end
79+
80+
test "only_path: false with *_url and no :host option" do
81+
draw do
82+
get 'foo', to: SimpleApp.new('foo#index')
83+
end
84+
85+
assert_raises ArgumentError do
86+
assert_equal 'http://example.com/foo', url_helpers.foo_url(only_path: false)
87+
end
88+
end
89+
90+
test "only_path: false with *_url and local :host option" do
91+
draw do
92+
get 'foo', to: SimpleApp.new('foo#index')
93+
end
94+
95+
assert_equal 'http://example.com/foo', url_helpers.foo_url(only_path: false, host: 'example.com')
96+
end
97+
98+
test "only_path: false with *_url and global :host option" do
99+
@set.default_url_options = { host: 'example.com' }
100+
101+
draw do
102+
get 'foo', to: SimpleApp.new('foo#index')
103+
end
104+
105+
assert_equal 'http://example.com/foo', url_helpers.foo_url(only_path: false)
106+
end
107+
108+
test "only_path: true with *_path" do
109+
draw do
110+
get 'foo', to: SimpleApp.new('foo#index')
111+
end
112+
113+
assert_deprecated do
114+
assert_equal '/foo', url_helpers.foo_path(only_path: true)
115+
end
116+
end
117+
118+
test "only_path: false with *_path with global :host option" do
119+
@set.default_url_options = { host: 'example.com' }
120+
121+
draw do
122+
get 'foo', to: SimpleApp.new('foo#index')
123+
end
124+
125+
assert_deprecated do
126+
assert_equal 'http://example.com/foo', url_helpers.foo_path(only_path: false)
127+
end
128+
end
129+
130+
test "only_path: false with *_path with local :host option" do
131+
draw do
132+
get 'foo', to: SimpleApp.new('foo#index')
133+
end
134+
135+
assert_deprecated do
136+
assert_equal 'http://example.com/foo', url_helpers.foo_path(only_path: false, host: 'example.com')
137+
end
138+
end
139+
140+
test "only_path: false with *_path with no :host option" do
141+
draw do
142+
get 'foo', to: SimpleApp.new('foo#index')
143+
end
144+
145+
assert_deprecated do
146+
assert_raises ArgumentError do
147+
assert_equal 'http://example.com/foo', url_helpers.foo_path(only_path: false)
148+
end
149+
end
150+
end
151+
72152
test "explicit keys win over implicit keys" do
73153
draw do
74154
resources :foo do

0 commit comments

Comments
 (0)