Skip to content

Commit

Permalink
Fix assets lookup for precompiled assets
Browse files Browse the repository at this point in the history
  - 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
  • Loading branch information
SampsonCrowley committed Jun 12, 2019
1 parent 2742687 commit 280df27
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
33 changes: 33 additions & 0 deletions lib/wicked_pdf/wicked_pdf_helper/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/wicked_pdf_test.rb
Original file line number Diff line number Diff line change
@@ -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 = '<html><body>Hello World</body></html>'.freeze

# Provide a public accessor to the normally-private parse_options function.
Expand Down
2 changes: 2 additions & 0 deletions wicked_pdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 280df27

Please sign in to comment.