Skip to content

Commit

Permalink
Merge 3c92e24 into 68d4c79
Browse files Browse the repository at this point in the history
  • Loading branch information
rossta committed Apr 30, 2017
2 parents 68d4c79 + 3c92e24 commit 1e5e6b8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/generators/serviceworker/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def update_application_layout
snippet = %(<link rel="manifest" href="/manifest.json" />)
snippet << %(\n<meta name="apple-mobile-web-app-capable" content="yes">)
unless layout
warn "Could not locate application layout. To insert manifest tags manually, use:\n\n#{snippet}\n"
conditional_warn "Could not locate application layout. To insert manifest tags manually, use:\n\n#{snippet}\n"
return
end
insert_into_file layout, snippet, before: "</head>\n"
Expand Down Expand Up @@ -85,6 +85,14 @@ def public_dir(*paths)
def join(*paths)
File.expand_path(File.join(*paths), destination_root)
end

def conditional_warn(warning)
silenced? or warn warning
end

def silenced?
ENV["RAILS_ENV"] == "test"
end
end
end
end
10 changes: 9 additions & 1 deletion lib/serviceworker/rails/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ def config
end

def asset_path(path)
::ActionController::Base.helpers.asset_path(path)
if controller_helpers.respond_to?(:compute_asset_path)
controller_helpers.compute_asset_path(path)
else
controller_helpers.asset_path(path, host: proc {})
end
end

def controller_helpers
::ActionController::Base.helpers
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/sample/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true

class TestAssetHost
cattr_accessor :host

def self.proc_method
proc { TestAssetHost.host }
end
end

config.action_controller.asset_host = TestAssetHost.proc_method
end
13 changes: 13 additions & 0 deletions test/serviceworker/rails_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def setup
get "/"
end

def teardown
TestAssetHost.host = nil
end

def test_homepage
assert last_response.ok?
assert_match(/Hello, World/, last_response.body)
Expand Down Expand Up @@ -84,4 +88,13 @@ def test_precompiled_serviceworker_request
assert_match(/console.log\(.*'Hello from ServiceWorker!'.*\);/, last_response.body)
end
end

def test_cdn_asset_host
TestAssetHost.host = "https://assets.example.com"
Rails.application.config.assets.stub(:compile, false) do
get "/serviceworker.js"

assert last_response.ok?
end
end
end

0 comments on commit 1e5e6b8

Please sign in to comment.