From 280df27e052841266e89466ce62063d4d81cb573 Mon Sep 17 00:00:00 2001 From: Sampson Crowley Date: Tue, 11 Jun 2019 22:13:40 -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 | 33 ++++++++++++++++++++++ test/unit/wicked_pdf_test.rb | 2 +- wicked_pdf.gemspec | 2 ++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/wicked_pdf/wicked_pdf_helper/assets.rb b/lib/wicked_pdf/wicked_pdf_helper/assets.rb index 32a06be6..da34d698 100644 --- a/lib/wicked_pdf/wicked_pdf_helper/assets.rb +++ b/lib/wicked_pdf/wicked_pdf_helper/assets.rb @@ -6,6 +6,34 @@ class WickedPdf module WickedPdfHelper module Assets ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/ + AssetStruct = 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 + `file --b --mime-type '#{filename}'`.strip + end + end def wicked_pdf_asset_base64(path) asset = find_asset(path) @@ -108,6 +136,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 + AssetStruct.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/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