From a1a8897e35f8590dbb852478a68384103dd4fd8b Mon Sep 17 00:00:00 2001 From: Sampson Crowley Date: Wed, 12 Jun 2019 17:08:54 -0600 Subject: [PATCH] Fix assets lookup for precompiled assets - use asset_manifest if available - gems missing from dev dependencies - remove static preset asset path in unit test that breaks tests for anyone without ENV variable set --- lib/wicked_pdf/wicked_pdf_helper/assets.rb | 8 +++++ .../wicked_pdf_helper/assets/asset_wrapper.rb | 35 +++++++++++++++++++ test/unit/wicked_pdf_test.rb | 2 +- wicked_pdf.gemspec | 2 ++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/wicked_pdf/wicked_pdf_helper/assets/asset_wrapper.rb diff --git a/lib/wicked_pdf/wicked_pdf_helper/assets.rb b/lib/wicked_pdf/wicked_pdf_helper/assets.rb index 32a06be6..712e45da 100644 --- a/lib/wicked_pdf/wicked_pdf_helper/assets.rb +++ b/lib/wicked_pdf/wicked_pdf_helper/assets.rb @@ -1,4 +1,7 @@ require 'open-uri' + +require_relative './assets/asset_wrapper' + # If webpacker is used, need to check for version require 'webpacker/version' if defined?(Webpacker) @@ -108,6 +111,11 @@ def asset_pathname(source) def find_asset(path) if Rails.application.assets.respond_to?(:find_asset) Rails.application.assets.find_asset(path, :base_path => Rails.application.root.to_s) + elsif Rails.application.respond_to?(:assets_manifest) && Rails.application.assets_manifest.respond_to?(:find_sources) + asset = Rails.application.assets_manifest.find_sources(path, :base_path => Rails.application.root.to_s).first + if asset + AssetWrapper.new(asset, "#{Rails.application.assets_manifest.dir}/#{Rails.application.assets_manifest.assets[path]}") + end else Sprockets::Railtie.build_environment(Rails.application).find_asset(path, :base_path => Rails.application.root.to_s) end diff --git a/lib/wicked_pdf/wicked_pdf_helper/assets/asset_wrapper.rb b/lib/wicked_pdf/wicked_pdf_helper/assets/asset_wrapper.rb new file mode 100644 index 00000000..4fe2499b --- /dev/null +++ b/lib/wicked_pdf/wicked_pdf_helper/assets/asset_wrapper.rb @@ -0,0 +1,35 @@ +class WickedPdf + module WickedPdfHelper + module Assets + AssetWrapper = Struct.new(:asset, :filename) do + def source + asset.respond_to?(:source) ? source.source : asset + end + + def to_s + asset.to_s + end + + def content_type + asset.respond_to?(:content_type) ? source.content_type : get_mime_type + end + + def extension + filename.to_s[/^.*\/?.*?\.(\w+)(\?.*)?/, 1] + end + + private + + def get_mime_type + mime = Mime::Type.lookup_by_extension(extension) + + raise 'Mime not Found' unless mime + + mime + rescue StandardError + `file --b --mime-type '#{filename}'`.strip + end + end + end + end +end diff --git a/test/unit/wicked_pdf_test.rb b/test/unit/wicked_pdf_test.rb index 36c6fc7c..7a53436a 100644 --- a/test/unit/wicked_pdf_test.rb +++ b/test/unit/wicked_pdf_test.rb @@ -1,5 +1,5 @@ require 'test_helper' -WickedPdf.config = { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf' } +WickedPdf.config = { :exe_path => ENV['WKHTMLTOPDF_BIN'] } HTML_DOCUMENT = 'Hello World'.freeze # Provide a public accessor to the normally-private parse_options function. diff --git a/wicked_pdf.gemspec b/wicked_pdf.gemspec index 2393190f..2f33fd27 100644 --- a/wicked_pdf.gemspec +++ b/wicked_pdf.gemspec @@ -35,4 +35,6 @@ DESC spec.add_development_dependency 'sqlite3', '~> 1.3.6' spec.add_development_dependency 'mocha', '= 1.3' spec.add_development_dependency 'test-unit' + spec.add_development_dependency 'bootsnap' + spec.add_development_dependency 'rdoc' end