diff --git a/lib/wicked_pdf/wicked_pdf_helper/assets.rb b/lib/wicked_pdf/wicked_pdf_helper/assets.rb
index 77163265..686d2d0c 100644
--- a/lib/wicked_pdf/wicked_pdf_helper/assets.rb
+++ b/lib/wicked_pdf/wicked_pdf_helper/assets.rb
@@ -202,13 +202,32 @@ def find_asset(path)
elsif defined?(Propshaft::Assembly) && Rails.application.assets.is_a?(Propshaft::Assembly)
PropshaftAsset.new(Rails.application.assets.load_path.find(path))
elsif Rails.application.respond_to?(:assets_manifest)
- asset_path = File.join(Rails.application.assets_manifest.dir, Rails.application.assets_manifest.assets[path])
+ relative_asset_path = get_asset_path_from_manifest(path)
+ return unless relative_asset_path
+
+ asset_path = File.join(Rails.application.assets_manifest.dir, relative_asset_path)
LocalAsset.new(asset_path) if File.file?(asset_path)
else
SprocketsEnvironment.find_asset(path, :base_path => Rails.application.root.to_s)
end
end
+ def get_asset_path_from_manifest(path)
+ assets = Rails.application.assets_manifest.assets
+
+ if File.extname(path).empty?
+ assets.find do |asset, _v|
+ directory = File.dirname(asset)
+ asset_path = File.basename(asset, File.extname(asset))
+ asset_path = File.join(directory, asset_path) if directory != '.'
+
+ asset_path == path
+ end&.last
+ else
+ assets[path]
+ end
+ end
+
# will prepend a http or default_protocol to a protocol relative URL
# or when no protcol is set.
def prepend_protocol(source)
diff --git a/test/fixtures/subdirectory/nested.js b/test/fixtures/subdirectory/nested.js
new file mode 100644
index 00000000..700bfaac
--- /dev/null
+++ b/test/fixtures/subdirectory/nested.js
@@ -0,0 +1 @@
+// Nested js
diff --git a/test/functional/wicked_pdf_helper_assets_test.rb b/test/functional/wicked_pdf_helper_assets_test.rb
index 2b8368b1..e4df813a 100644
--- a/test/functional/wicked_pdf_helper_assets_test.rb
+++ b/test/functional/wicked_pdf_helper_assets_test.rb
@@ -11,6 +11,10 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
teardown do
WickedPdf.config = @saved_config
+
+ # @see freerange/mocha#331
+ Rails.application.unstub(:assets)
+ Rails.application.unstub(:assets_manifest)
end
if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
@@ -18,12 +22,61 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
end
+ test 'wicked_pdf_asset_base64 works without file extension when using sprockets' do
+ assert_match %r{data:application\/javascript;base64,.+}, wicked_pdf_asset_base64('wicked')
+ end
+
+ test 'wicked_pdf_asset_base64 works with nested files and without file extension when using sprockets' do
+ assert_match %r{data:application\/javascript;base64,.+}, wicked_pdf_asset_base64('subdirectory/nested')
+ end
+
+ test 'wicked_pdf_asset_base64 works without file extension when using asset manifest' do
+ stub_manifest = OpenStruct.new(
+ :dir => Rails.root.join('app/assets'),
+ :assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
+ )
+ Rails.application.stubs(:assets).returns(nil)
+ Rails.application.stubs(:assets_manifest).returns(stub_manifest)
+
+ assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked')
+ end
+
+ test 'wicked_pdf_asset_base64 works with nested files and without file extension when using asset manifest' do
+ stub_manifest = OpenStruct.new(
+ :dir => Rails.root.join('app/assets'),
+ :assets => { 'subdirectory/nested.js' => 'javascripts/subdirectory/nested.js' }
+ )
+ Rails.application.stubs(:assets).returns(nil)
+ Rails.application.stubs(:assets_manifest).returns(stub_manifest)
+
+ assert_match %r{data:text\/javascript;base64,.+}, wicked_pdf_asset_base64('subdirectory/nested')
+ end
+
test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
Rails.configuration.assets.expects(:compile => true)
assert_equal "",
wicked_pdf_stylesheet_link_tag('wicked')
end
+ test 'wicked_pdf_stylesheet_link_tag should work without file extension when using sprockets' do
+ Rails.configuration.assets.expects(:compile => true)
+ assert_equal "",
+ wicked_pdf_stylesheet_link_tag('wicked')
+ end
+
+ test 'wicked_pdf_stylesheet_link_tag should work without file extension when using asset manifest' do
+ stub_manifest = OpenStruct.new(
+ :dir => Rails.root.join('app/assets'),
+ :assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
+ )
+
+ Rails.application.stubs(:assets).returns(nil)
+ Rails.application.stubs(:assets_manifest).returns(stub_manifest)
+
+ assert_equal "",
+ wicked_pdf_stylesheet_link_tag('wicked')
+ end
+
test 'wicked_pdf_stylesheet_link_tag should raise if the stylesheet is not available and config is set' do
Rails.configuration.assets.expects(:compile => true)
WickedPdf.config[:raise_on_missing_assets] = true
@@ -60,6 +113,21 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
end
+ test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in when assets are remote and using asset manifest' do
+ stub_manifest = OpenStruct.new(
+ :dir => Rails.root.join('app/assets'),
+ :assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
+ )
+
+ Rails.application.stubs(:assets).returns(nil)
+ Rails.application.stubs(:assets_manifest).returns(stub_manifest)
+
+ stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 200, :body => '/* Wicked styles */')
+ expects(:precompiled_or_absolute_asset? => true).twice
+ assert_equal "",
+ wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
+ end
+
test 'wicked_pdf_stylesheet_link_tag should raise if remote assets are not available and config is set' do
WickedPdf.config[:raise_on_missing_assets] = true
stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 404, :body => 'File not found')
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 4820d5b2..26bb92df 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -27,6 +27,11 @@
source = File.read('test/fixtures/wicked.js')
File.open(destination, 'w') { |f| f.write(source) }
+ Dir.mkdir(js_dir.join('subdirectory')) unless File.directory?(js_dir.join('subdirectory'))
+ destination = js_dir.join('subdirectory/nested.js')
+ source = File.read('test/fixtures/subdirectory/nested.js')
+ File.open(destination, 'w') { |f| f.write(source) }
+
config_dir = assets_dir.join('config')
Dir.mkdir(config_dir) unless File.directory?(config_dir)
source = File.read('test/fixtures/manifest.js')