Skip to content

Commit

Permalink
Deprecated prefix option in favor of default_url_options.script_name
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Dec 10, 2024
1 parent ae81df9 commit c0794a5
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [2.3.4]

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

``` javascript
Expand Down
4 changes: 1 addition & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,8 @@ Options to configure routes formatting. These options are available both in Ruby

* `default_url_options` - default parameters used when generating URLs
* Example: `{format: "json", trailing_slash: true, protocol: "https", subdomain: "api", host: "example.com", port: 3000}`
* See [`url_for` doc](https://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html#method-i-url_for) for list of supported options
* Default: `{}`
* `prefix` - string that will prepend any generated URL. Usually used when app URL root includes a path component.
* Example: `/rails-app`
* Default: `Rails.application.config.relative_url_root`
* `serializer` - a JS function that serializes a Javascript Hash object into URL paramters like `{a: 1, b: 2} => "a=1&b=2"`.
* Default: `nil`. Uses built-in serializer compatible with Rails
* Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
Expand Down
7 changes: 6 additions & 1 deletion lib/js_routes/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Configuration
sig { returns(FileName) }
attr_accessor :file
sig { returns(Prefix) }
attr_accessor :prefix
attr_reader :prefix
sig { returns(T::Boolean) }
attr_accessor :url_links
sig { returns(T::Boolean) }
Expand Down Expand Up @@ -91,6 +91,11 @@ def [](attribute)
public_send(attribute)
end

def prefix=(value)
JsRoutes::Utils.deprecator.warn("JsRoutes configuration prefix is deprecated in favor of default_url_options.script_name.")
@prefix = value
end

sig { params(attributes: Options).returns(JsRoutes::Configuration) }
def merge(attributes)
clone.assign(attributes)
Expand Down
9 changes: 9 additions & 0 deletions lib/js_routes/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def self.shakapacker
nil
end
end

sig { returns(T.untyped) }
def self.deprecator
if defined?(Rails) && Rails.version >= "7.1.0"
Rails.deprecator
else
ActiveSupport::Deprecation
end
end
end

end
3 changes: 3 additions & 0 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ RubyVariables.WRAPPER(
return ReservedOptions.includes(key);
}
configure(new_config) {
if (new_config.prefix) {
console.warn("JsRoutes configuration prefix option is deprecated in favor of default_url_options.script_name.");
}
this.configuration = { ...this.configuration, ...new_config };
return this.configuration;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,11 @@ RubyVariables.WRAPPER(
}

configure(new_config: Partial<Configuration>): Configuration {
if (new_config.prefix) {
console.warn(
"JsRoutes configuration prefix option is deprecated in favor of default_url_options.script_name."
);
}
this.configuration = { ...this.configuration, ...new_config };
return this.configuration;
}
Expand Down
1 change: 1 addition & 0 deletions spec/js_routes/module_types/dts/routes.spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ declare type KeywordUrlOptions = Optional<{
port: string | number;
anchor: string;
trailing_slash: boolean;
script_name: string;
params: RouteParameters;
}>;
declare type RouteOptions = KeywordUrlOptions & RouteParameters;
Expand Down
65 changes: 34 additions & 31 deletions spec/js_routes/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,44 +116,54 @@
end
end

context "when prefix with trailing slash is specified" do
describe "prefix option" do
around do |e|
JsRoutes::Utils.deprecator.silence do
e.run
end
end

let(:_options) { {:prefix => "/myprefix/" } }
context "with trailing slash is specified" do
let(:_options) { {:prefix => "/myprefix/" } }

it "should render routing with prefix" do
it "should render routing with prefix" do
expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}")
end
end

it "should render routing with prefix set in JavaScript" do
evaljs("Routes.configure({prefix: '/newprefix/'})")
expectjs("Routes.config().prefix").to eq("/newprefix/")
expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
it "should render routing with prefix set in JavaScript" do
evaljs("Routes.configure({prefix: '/newprefix/'})")
expectjs("Routes.config().prefix").to eq("/newprefix/")
expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
end
end

end

context "when prefix with http:// is specified" do

let(:_options) { {:prefix => "http://localhost:3000" } }
context "with http:// is specified" do
let(:_options) { {:prefix => "http://localhost:3000" } }

it "should render routing with prefix" do
expectjs("Routes.inbox_path(1)").to eq(_options[:prefix] + test_routes.inbox_path(1))
it "should render routing with prefix" do
expectjs("Routes.inbox_path(1)").to eq(_options[:prefix] + test_routes.inbox_path(1))
end
end
end

context "when prefix without trailing slash is specified" do
context "without trailing slash is specified" do
let(:_options) { {:prefix => "/myprefix" } }

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

it "should render routing with prefix" do
expectjs("Routes.inbox_path(1)").to eq("/myprefix#{test_routes.inbox_path(1)}")
it "should render routing with prefix set in JavaScript" do
evaljs("Routes.configure({prefix: '/newprefix/'})")
expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
end
end

it "should render routing with prefix set in JavaScript" do
evaljs("Routes.configure({prefix: '/newprefix/'})")
expectjs("Routes.inbox_path(1)").to eq("/newprefix#{test_routes.inbox_path(1)}")
context "combined with url links and default_url_options" do
let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } }
it "should generate path and url links" do
expectjs("Routes.inbox_url(1)").to eq("http://example.com/api#{test_routes.inbox_path(1)}")
end
end

end

context "when default format is specified" do
Expand Down Expand Up @@ -381,13 +391,6 @@
end
end

context "with prefix option" do
let(:_options) { { :prefix => "/api", :url_links => true, :default_url_options => {:host => 'example.com'} } }
it "should generate path and url links" do
expectjs("Routes.inbox_url(1)").to eq("http://example.com/api#{test_routes.inbox_path(1)}")
end
end

context "with compact option" do
let(:_options) { { :compact => true, :url_links => true, :default_url_options => {:host => 'example.com'} } }
it "does not affect url helpers" do
Expand Down

0 comments on commit c0794a5

Please sign in to comment.