Skip to content

Commit 48a03bc

Browse files
Judahmeekahangarha
authored andcommitted
simplify host_hash
1 parent ade1356 commit 48a03bc

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

lib/shakapacker/digest_strategy.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,12 @@ def record_compilation_digest
5454

5555
def compilation_digest_path
5656
path = "last-compilation-digest-#{Shakapacker.env}"
57-
path += "-#{generate_host_hash}" if generate_host_hash.present?
57+
if Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"].present?
58+
host_hash = Digest::SHA1.hexdigest(Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"])
59+
path += "-#{host_hash}"
60+
end
5861

5962
config.cache_path.join(path)
6063
end
61-
62-
def generate_host_hash
63-
# Using hash for memoizing the host hash is to make testing easier.
64-
# The default value, prevents generating hash in the situation where no value for asset_host
65-
# and SHAKAPACKER_ASSET_HOST are provided, leading to not add hash to the asset path.
66-
@generated_host_hashes ||= { [nil, nil] => "" }
67-
68-
keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]]
69-
70-
@generated_host_hashes[keys] ||= Digest::SHA1.hexdigest(keys.join("-"))
71-
end
7264
end
7365
end

spec/shakapacker/digest_strategy_spec.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,31 @@ def remove_compilation_digest_path
2929
end
3030

3131
it "is stale when host changes" do
32-
# allow(Shakapacker.config).to receive(:fetch).with(any_args).and_call_original
33-
# allow(Shakapacker.config).to receive(:fetch).with(:compiler_strategy_asset_host_sensitive).and_return(true)
32+
old_host = Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"]
3433

35-
ENV["SHAKAPACKER_ASSET_HOST"] = "the-host"
34+
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "the-host"
3635

3736
@digest_strategy.after_compile_hook
3837

39-
ENV["SHAKAPACKER_ASSET_HOST"] = "new-host"
38+
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "new-host"
4039

4140
expect(@digest_strategy.stale?).to be true
4241
expect(@digest_strategy.fresh?).to be_falsey
4342

44-
ENV["SHAKAPACKER_ASSET_HOST"] = nil
43+
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = old_host
4544
end
4645

4746
it "generates correct compilation_digest_path" do
48-
actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
49-
expected_path = "last-compilation-digest-#{Shakapacker.env}"
50-
51-
expect(actual_path).to eq expected_path
52-
end
53-
54-
it "generates correct compilation_digest_path with " do
55-
# allow(Shakapacker.config).to receive(:fetch).with(any_args).and_call_original
56-
# allow(Shakapacker.config).to receive(:fetch).with(:compiler_strategy_asset_host_sensitive).and_return(true)
47+
old_host = Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"]
5748

58-
ENV["SHAKAPACKER_ASSET_HOST"] = "custom-path"
49+
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "custom-path"
5950

6051
actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
61-
host_hash = Digest::SHA1.hexdigest("-custom-path")
52+
host_hash = Digest::SHA1.hexdigest("custom-path")
6253
expected_path = "last-compilation-digest-#{Shakapacker.env}-#{host_hash}"
6354

6455
expect(actual_path).to eq expected_path
56+
57+
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = old_host
6558
end
6659
end

0 commit comments

Comments
 (0)