Skip to content

Commit c0794a5

Browse files
committed
Deprecated prefix option in favor of default_url_options.script_name
1 parent ae81df9 commit c0794a5

File tree

8 files changed

+60
-35
lines changed

8 files changed

+60
-35
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [2.3.4]
44

55
* Migrate to yarn 4
6+
* Deprecated `prefix` option in favor of `default_url_options.script_name`.
67
* Add support for `script_name` [Rails helper option](https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for).
78

89
``` javascript

Readme.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,8 @@ Options to configure routes formatting. These options are available both in Ruby
353353

354354
* `default_url_options` - default parameters used when generating URLs
355355
* Example: `{format: "json", trailing_slash: true, protocol: "https", subdomain: "api", host: "example.com", port: 3000}`
356+
* See [`url_for` doc](https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for) for list of supported options
356357
* Default: `{}`
357-
* `prefix` - string that will prepend any generated URL. Usually used when app URL root includes a path component.
358-
* Example: `/rails-app`
359-
* Default: `Rails.application.config.relative_url_root`
360358
* `serializer` - a JS function that serializes a Javascript Hash object into URL paramters like `{a: 1, b: 2} => "a=1&b=2"`.
361359
* Default: `nil`. Uses built-in serializer compatible with Rails
362360
* Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.

lib/js_routes/configuration.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Configuration
1919
sig { returns(FileName) }
2020
attr_accessor :file
2121
sig { returns(Prefix) }
22-
attr_accessor :prefix
22+
attr_reader :prefix
2323
sig { returns(T::Boolean) }
2424
attr_accessor :url_links
2525
sig { returns(T::Boolean) }
@@ -91,6 +91,11 @@ def [](attribute)
9191
public_send(attribute)
9292
end
9393

94+
def prefix=(value)
95+
JsRoutes::Utils.deprecator.warn("JsRoutes configuration prefix is deprecated in favor of default_url_options.script_name.")
96+
@prefix = value
97+
end
98+
9499
sig { params(attributes: Options).returns(JsRoutes::Configuration) }
95100
def merge(attributes)
96101
clone.assign(attributes)

lib/js_routes/utils.rb

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ def self.shakapacker
1313
nil
1414
end
1515
end
16+
17+
sig { returns(T.untyped) }
18+
def self.deprecator
19+
if defined?(Rails) && Rails.version >= "7.1.0"
20+
Rails.deprecator
21+
else
22+
ActiveSupport::Deprecation
23+
end
24+
end
1625
end
1726

1827
end

lib/routes.js

+3
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ RubyVariables.WRAPPER(
490490
return ReservedOptions.includes(key);
491491
}
492492
configure(new_config) {
493+
if (new_config.prefix) {
494+
console.warn("JsRoutes configuration prefix option is deprecated in favor of default_url_options.script_name.");
495+
}
493496
this.configuration = { ...this.configuration, ...new_config };
494497
return this.configuration;
495498
}

lib/routes.ts

+5
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,11 @@ RubyVariables.WRAPPER(
708708
}
709709

710710
configure(new_config: Partial<Configuration>): Configuration {
711+
if (new_config.prefix) {
712+
console.warn(
713+
"JsRoutes configuration prefix option is deprecated in favor of default_url_options.script_name."
714+
);
715+
}
711716
this.configuration = { ...this.configuration, ...new_config };
712717
return this.configuration;
713718
}

spec/js_routes/module_types/dts/routes.spec.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ declare type KeywordUrlOptions = Optional<{
5050
port: string | number;
5151
anchor: string;
5252
trailing_slash: boolean;
53+
script_name: string;
5354
params: RouteParameters;
5455
}>;
5556
declare type RouteOptions = KeywordUrlOptions & RouteParameters;

spec/js_routes/options_spec.rb

+34-31
Original file line numberDiff line numberDiff line change
@@ -116,44 +116,54 @@
116116
end
117117
end
118118

119-
context "when prefix with trailing slash is specified" do
119+
describe "prefix option" do
120+
around do |e|
121+
JsRoutes::Utils.deprecator.silence do
122+
e.run
123+
end
124+
end
120125

121-
let(:_options) { {:prefix => "/myprefix/" } }
126+
context "with trailing slash is specified" do
127+
let(:_options) { {:prefix => "/myprefix/" } }
122128

123-
it "should render routing with prefix" do
129+
it "should render routing with prefix" do
124130
expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}")
125-
end
131+
end
126132

127-
it "should render routing with prefix set in JavaScript" do
128-
evaljs("Routes.configure({prefix: '/newprefix/'})")
129-
expectjs("Routes.config().prefix").to eq("/newprefix/")
130-
expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
133+
it "should render routing with prefix set in JavaScript" do
134+
evaljs("Routes.configure({prefix: '/newprefix/'})")
135+
expectjs("Routes.config().prefix").to eq("/newprefix/")
136+
expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
137+
end
131138
end
132139

133-
end
134-
135-
context "when prefix with http:// is specified" do
136-
137-
let(:_options) { {:prefix => "http://localhost:3000" } }
140+
context "with http:// is specified" do
141+
let(:_options) { {:prefix => "http://localhost:3000" } }
138142

139-
it "should render routing with prefix" do
140-
expectjs("Routes.inbox_path(1)").to eq(_options[:prefix] + test_routes.inbox_path(1))
143+
it "should render routing with prefix" do
144+
expectjs("Routes.inbox_path(1)").to eq(_options[:prefix] + test_routes.inbox_path(1))
145+
end
141146
end
142-
end
143147

144-
context "when prefix without trailing slash is specified" do
148+
context "without trailing slash is specified" do
149+
let(:_options) { {:prefix => "/myprefix" } }
145150

146-
let(:_options) { {:prefix => "/myprefix" } }
151+
it "should render routing with prefix" do
152+
expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}")
153+
end
147154

148-
it "should render routing with prefix" do
149-
expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}")
155+
it "should render routing with prefix set in JavaScript" do
156+
evaljs("Routes.configure({prefix: '/newprefix/'})")
157+
expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
158+
end
150159
end
151160

152-
it "should render routing with prefix set in JavaScript" do
153-
evaljs("Routes.configure({prefix: '/newprefix/'})")
154-
expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
161+
context "combined with url links and default_url_options" do
162+
let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } }
163+
it "should generate path and url links" do
164+
expectjs("Routes.inbox_url(1)").to eq("http://example.com/api#{test_routes.inbox_path(1)}")
165+
end
155166
end
156-
157167
end
158168

159169
context "when default format is specified" do
@@ -381,13 +391,6 @@
381391
end
382392
end
383393

384-
context "with prefix option" do
385-
let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } }
386-
it "should generate path and url links" do
387-
expectjs("Routes.inbox_url(1)").to eq("http://example.com/api#{test_routes.inbox_path(1)}")
388-
end
389-
end
390-
391394
context "with compact option" do
392395
let(:_options) { { :compact => true, :url_links => true, :default_url_options => {:host => 'example.com'} } }
393396
it "does not affect url helpers" do

0 commit comments

Comments
 (0)