From be0baaedb04069f73af05af63f55356e2002db70 Mon Sep 17 00:00:00 2001 From: Mostafa Ahangarha Date: Wed, 6 Dec 2023 13:19:05 +0330 Subject: [PATCH] Revert "Remove any references to relative_url_root" This reverts commit 2677f81290dabb4cbd8e24c2590706ba94f777ed. We will address it later. --- CHANGELOG.md | 2 +- lib/install/config/shakapacker.yml | 2 + lib/shakapacker/compiler.rb | 1 + lib/shakapacker/configuration.rb | 7 ++++ .../compiler_spec.rb | 3 ++ spec/shakapacker/compiler_spec.rb | 3 ++ spec/shakapacker/configuration_spec.rb | 42 +++++++++++++++++++ 7 files changed, 59 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3b09656..740b1add 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ _Please add entries here for your pull requests that are not yet released._ ### Added - Experimental support for other JS package managers using `package_json` gem [PR 349](https://github.com/shakacode/shakapacker/pull/349) by [G-Rath](https://github.com/g-rath). -- Use `config/shakapacker.yml` as the secondary source for `asset_host` configuration [PR 376](https://github.com/shakacode/shakapacker/pull/376) by [ahangarha](https://github.com/ahangarha). +- Use `config/shakapacker.yml` as the secondary source for `asset_host` and `relative_url_root` configurations [PR 376](https://github.com/shakacode/shakapacker/pull/376) by [ahangarha](https://github.com/ahangarha). ### Fixed - Recommend `server` option instead of deprecated `https` option when `--https` is provided [PR 380](https://github.com/shakacode/shakapacker/pull/380) by [G-Rath](https://github.com/g-rath) diff --git a/lib/install/config/shakapacker.yml b/lib/install/config/shakapacker.yml index 7b91f233..3e080bef 100644 --- a/lib/install/config/shakapacker.yml +++ b/lib/install/config/shakapacker.yml @@ -55,6 +55,8 @@ default: &default # SHAKAPACKER_ASSET_HOST will override both configurations. # asset_host: custom-path + # relative_url_root: custom-path + development: <<: *default compile: true diff --git a/lib/shakapacker/compiler.rb b/lib/shakapacker/compiler.rb index ff54fe1a..4c0dcb09 100644 --- a/lib/shakapacker/compiler.rb +++ b/lib/shakapacker/compiler.rb @@ -108,6 +108,7 @@ def webpack_env env.merge( "SHAKAPACKER_ASSET_HOST" => instance.config.asset_host, + "SHAKAPACKER_RELATIVE_URL_ROOT" => instance.config.relative_url_root, "SHAKAPACKER_CONFIG" => instance.config_path.to_s ) end diff --git a/lib/shakapacker/configuration.rb b/lib/shakapacker/configuration.rb index e1824229..101aa845 100644 --- a/lib/shakapacker/configuration.rb +++ b/lib/shakapacker/configuration.rb @@ -123,6 +123,13 @@ def asset_host ).to_s end + def relative_url_root + ENV.fetch( + "SHAKAPACKER_RELATIVE_URL_ROOT", + fetch(:relative_url_root) || ActionController::Base.relative_url_root + ).to_s + end + private def data @data ||= load diff --git a/spec/backward_compatibility_specs/compiler_spec.rb b/spec/backward_compatibility_specs/compiler_spec.rb index e5ec75a5..7d80d17d 100644 --- a/spec/backward_compatibility_specs/compiler_spec.rb +++ b/spec/backward_compatibility_specs/compiler_spec.rb @@ -48,9 +48,12 @@ it "accepts external env variables" do expect(Webpacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "" + expect(Webpacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "" ENV["WEBPACKER_ASSET_HOST"] = "foo.bar" + ENV["WEBPACKER_RELATIVE_URL_ROOT"] = "/baz" expect(Webpacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar" + expect(Webpacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz" end end diff --git a/spec/shakapacker/compiler_spec.rb b/spec/shakapacker/compiler_spec.rb index 7fb7a406..1fbd3b4a 100644 --- a/spec/shakapacker/compiler_spec.rb +++ b/spec/shakapacker/compiler_spec.rb @@ -48,9 +48,12 @@ it "accepts external env variables" do expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "" + expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "" allow(ENV).to receive(:fetch).with("SHAKAPACKER_ASSET_HOST", nil).and_return("foo.bar") + allow(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return("/baz") expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_ASSET_HOST"]).to eq "foo.bar" + expect(Shakapacker.compiler.send(:webpack_env)["SHAKAPACKER_RELATIVE_URL_ROOT"]).to eq "/baz" end end diff --git a/spec/shakapacker/configuration_spec.rb b/spec/shakapacker/configuration_spec.rb index a2746383..0b454871 100644 --- a/spec/shakapacker/configuration_spec.rb +++ b/spec/shakapacker/configuration_spec.rb @@ -373,4 +373,46 @@ end end end + + describe "#relative_url_root" do + let(:config) do + Shakapacker::Configuration.new( + root_path: ROOT_PATH, + config_path: Pathname.new(File.expand_path("./test_app/config/shakapacker.yml", __dir__)), + env: "production" + ) + end + + it "returns the value of SHAKAPACKER_RELATIVE_URL_ROOT if set" do + expect(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return("custom_value") + + expect(config.relative_url_root).to eq "custom_value" + end + + context "without SHAKAPACKER_RELATIVE_URL_ROOT set" do + it "returns relative_url_root in shakapacker.yml if set" do + expect(config).to receive(:fetch).with(:relative_url_root).and_return("value-in-config-file") + expect(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", "value-in-config-file").and_return("value-in-config-file") + + expect(config.relative_url_root).to eq "value-in-config-file" + end + + context "without relative_url_root set in the shakapacker.yml" do + it "returns ActionController::Base.relative_url_root if SHAKAPACKER_RELATIVE_URL_ROOT is not set" do + expect(ActionController::Base).to receive(:relative_url_root).and_return("abcd") + expect(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", "abcd").and_return("abcd") + + expect(config.relative_url_root).to eq "abcd" + end + + context "without ActionController::Base.relative_url_root returing any value" do + it "returns an empty string" do + expect(ENV).to receive(:fetch).with("SHAKAPACKER_RELATIVE_URL_ROOT", nil).and_return(nil) + + expect(config.relative_url_root).to eq "" + end + end + end + end + end end