Skip to content

Commit

Permalink
Adding support for an AssetHost (#795)
Browse files Browse the repository at this point in the history
* Adding support for an AssetHost

Added an asset_host option to the Lucky::Server.settings
That setting is prepended to the asset path. It can be configured in the
application the same way host and port are.
  • Loading branch information
russ authored and paulcsmith committed Jun 7, 2019
1 parent f6d4b97 commit c4db6b9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ dependencies:
development_dependencies:
ameba:
github: veelenga/ameba
version: ~> 0.9
version: ~> 0.9.0

scripts:
postinstall: script/precompile_tasks
Expand Down
12 changes: 12 additions & 0 deletions spec/lucky/asset_helpers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ describe Lucky::AssetHelpers do
it "strips the prefixed '/assets/ in path" do
TestPage.new.strips_prefixed_asset_path.should eq "/assets/images/inside-assets-folder.png"
end

it "prepends the asset_host configuration option" do
Lucky::Server.temp_config(asset_host: "https://production.com") do
TestPage.new.asset_path.should eq "https://production.com/images/logo-with-hash.png"
end
end
end

describe "dynamic asset helper" do
Expand All @@ -66,5 +72,11 @@ describe Lucky::AssetHelpers do
TestPage.new.missing_dynamic_asset_path
end
end

it "prepends the asset_host configuration option" do
Lucky::Server.temp_config(asset_host: "https://production.com") do
TestPage.new.dynamic_asset_path.should eq "https://production.com/images/logo-with-hash.png"
end
end
end
end
4 changes: 2 additions & 2 deletions src/lucky/asset_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Lucky::AssetHelpers

{% if path.is_a?(StringLiteral) %}
{% if Lucky::AssetHelpers::ASSET_MANIFEST[path] %}
{{ Lucky::AssetHelpers::ASSET_MANIFEST[path] }}
Lucky::Server.settings.asset_host + {{ Lucky::AssetHelpers::ASSET_MANIFEST[path] }}
{% else %}
{% asset_paths = Lucky::AssetHelpers::ASSET_MANIFEST.keys.join(",") %}
{{ run "../run_macros/missing_asset", path, asset_paths }}
Expand Down Expand Up @@ -48,7 +48,7 @@ module Lucky::AssetHelpers
def dynamic_asset(path)
fingerprinted_path = Lucky::AssetHelpers::ASSET_MANIFEST[path]?
if fingerprinted_path
fingerprinted_path
Lucky::Server.settings.asset_host + fingerprinted_path
else
raise "Missing asset: #{path}"
end
Expand Down
1 change: 1 addition & 0 deletions src/lucky/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class Lucky::Server
setting secret_key_base : String
setting host : String
setting port : Int32
setting asset_host : String = ""
end
end

0 comments on commit c4db6b9

Please sign in to comment.