Skip to content

Commit

Permalink
Revert "Remove any references to relative_url_root"
Browse files Browse the repository at this point in the history
This reverts commit 2677f81.
We will address it later.
  • Loading branch information
ahangarha committed Dec 6, 2023
1 parent 2677f81 commit be0baae
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/install/config/shakapacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/shakapacker/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/shakapacker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/backward_compatibility_specs/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions spec/shakapacker/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions spec/shakapacker/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit be0baae

Please sign in to comment.