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 a1a8897
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/wicked_pdf/wicked_pdf_helper/assets.rb
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions lib/wicked_pdf/wicked_pdf_helper/assets/asset_wrapper.rb
Original file line number Diff line number Diff line change
@@ -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
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 a1a8897

Please sign in to comment.