Skip to content

Commit

Permalink
Support files in subdirectories correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rky-pl committed May 29, 2024
1 parent 6088b6b commit 25e2e73
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/wicked_pdf/wicked_pdf_helper/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,13 @@ def get_asset_path_from_manifest(path)
assets = Rails.application.assets_manifest.assets

if File.extname(path).empty?
assets.find { |asset, _v| File.basename(asset, File.extname(asset)) == path }&.last
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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/subdirectory/nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Nested js
15 changes: 15 additions & 0 deletions test/functional/wicked_pdf_helper_assets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
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'),
Expand All @@ -37,6 +41,17 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
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 "<style type='text/css'>/* Wicked styles */\n\n</style>",
Expand Down
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 25e2e73

Please sign in to comment.