Skip to content

Commit

Permalink
Fix #476 issue - problem with sprockets 3.0
Browse files Browse the repository at this point in the history
Remove `asset_exists?` method. Replace it with `File.file?` at `read_asset`.
Fix issue handled by `asset_exists?` at `wicked_pdf_stylesheet_link_tag` by
changing `asset_pathname` method.
`asset_exists?` coused problems with sprockets 3.0 so removing it fixes that.

Always process assets at `wicked_pdf_stylesheet_link_tag` with
`wicked_pdf_asset_path`. This fixes issue when
`url(http://example.com/rails.png)` was removed.

Rename `precompiled_asset?` to `precompiled_or_absolute_asset?` - this more
precisely describe what this method does. Additionally return `true` if
argument string starts with "http://" or "http://".

Change `asset_pathname` to always return some value when assets compilation
is enabled. This fixes problem when exception is raised when asset doesn't
exists.

Change `read_asset` to check if file exists before read it.

Fix test
  • Loading branch information
oleksandrbyk committed Jan 8, 2016
1 parent 2d2fb07 commit 2250c2f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
37 changes: 18 additions & 19 deletions lib/wicked_pdf/wicked_pdf_helper/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def wicked_pdf_stylesheet_link_tag(*sources)
if Regexp.last_match[1].starts_with?('data:')
"url(#{Regexp.last_match[1]})"
else
asset = Regexp.last_match[1]
"url(#{wicked_pdf_asset_path(asset)})" if asset_exists?(asset)
"url(#{wicked_pdf_asset_path(Regexp.last_match[1])})"
end
end.html_safe
end
Expand Down Expand Up @@ -57,15 +56,17 @@ def wicked_pdf_asset_path(asset)
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}

def asset_pathname(source)
if precompiled_asset?(source)
if (pathname = set_protocol(asset_path(source))) =~ URI_REGEXP
if precompiled_or_absolute_asset?(source)
asset = asset_path(source)
if (pathname = set_protocol(asset)) =~ URI_REGEXP
# asset_path returns an absolute URL using asset_host if asset_host is set
pathname
else
File.join(Rails.public_path, asset_path(source).sub(/\A#{Rails.application.config.action_controller.relative_url_root}/, ''))
File.join(Rails.public_path, asset.sub(/\A#{Rails.application.config.action_controller.relative_url_root}/, ''))
end
else
Rails.application.assets.find_asset(source).pathname
asset = Rails.application.assets.find_asset(source)
asset ? asset.pathname : File.join(Rails.public_path, source)
end
end

Expand All @@ -81,25 +82,27 @@ def set_protocol(source)
source
end

def precompiled_asset?(source)
Rails.configuration.assets.compile == false || source.to_s[0] == '/'
def precompiled_or_absolute_asset?(source)
Rails.configuration.assets.compile == false ||
source.to_s[0] == '/' ||
source.to_s.match(/\Ahttps?\:\/\//)
end

def read_asset(source)
if precompiled_asset?(source)
if set_protocol(asset_path(source)) =~ URI_REGEXP
read_from_uri(source)
elsif asset_exists?(source)
IO.read(asset_pathname(source))
if precompiled_or_absolute_asset?(source)
if (pathname = asset_pathname(source)) =~ URI_REGEXP
read_from_uri(pathname)
elsif File.file?(pathname)
IO.read(pathname)
end
else
Rails.application.assets.find_asset(source).to_s
end
end

def read_from_uri(source)
def read_from_uri(uri)
encoding = ':UTF-8' if RUBY_VERSION > '1.8'
asset = open(asset_pathname(source), "r#{encoding}") { |f| f.read }
asset = open(uri, "r#{encoding}") { |f| f.read }
asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
asset
end
Expand All @@ -110,9 +113,5 @@ def gzip(asset)
gzipper.read
rescue Zlib::GzipFile::Error
end

def asset_exists?(source)
Rails.application.assets.find_asset(source).present?
end
end
end
58 changes: 52 additions & 6 deletions test/functional/wicked_pdf_helper_assets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,74 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
end

test 'wicked_pdf_asset_path should return a url when assets are served by an asset server using HTTPS' do
expects(:asset_path => 'https://assets.domain.com/dummy.png', 'precompiled_asset?' => true)
Rails.configuration.assets.expects(:compile => false)
expects(:asset_path => 'https://assets.domain.com/dummy.png')
assert_equal 'https://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
end

test 'wicked_pdf_asset_path should return a url with a protocol when assets are served by an asset server with relative urls' do
Rails.configuration.assets.expects(:compile => false)
expects(:asset_path => '//assets.domain.com/dummy.png')
expects('precompiled_asset?' => true)
assert_equal 'http://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
end

test 'wicked_pdf_asset_path should return a url with a protocol when assets are served by an asset server with no protocol set' do
Rails.configuration.assets.expects(:compile => false)
expects(:asset_path => 'assets.domain.com/dummy.png')
expects('precompiled_asset?' => true)
assert_equal 'http://assets.domain.com/dummy.png', wicked_pdf_asset_path('dummy.png')
end

test 'wicked_pdf_asset_path should return a path when assets are precompiled' do
expects('precompiled_asset?' => false)
test 'wicked_pdf_asset_path should return a path' do
Rails.configuration.assets.expects(:compile => true)
path = wicked_pdf_asset_path('application.css')

assert path.include?('/assets/stylesheets/application.css')
assert path.include?('/app/assets/stylesheets/application.css')
assert path.include?('file:///')

Rails.configuration.assets.expects(:compile => false)
expects(:asset_path => '/assets/application-6fba03f13d6ff1553477dba03475c4b9b02542e9fb8913bd63c258f4de5b48d9.css')
path = wicked_pdf_asset_path('application.css')

assert path.include?('/public/assets/application-6fba03f13d6ff1553477dba03475c4b9b02542e9fb8913bd63c258f4de5b48d9.css')
assert path.include?('file:///')
end

# This assets does not exists so probably it doesn't matter what is
# returned, but lets ensure that returned value is the same when assets
# are precompiled and when they are not
test 'wicked_pdf_asset_path should return a path when asset does not exist' do
Rails.configuration.assets.expects(:compile => true)
path = wicked_pdf_asset_path('missing.png')

assert path.include?('/public/missing.png')
assert path.include?('file:///')

Rails.configuration.assets.expects(:compile => false)
expects(:asset_path => '/missing.png')
path = wicked_pdf_asset_path('missing.png')

assert path.include?('/public/missing.png')
assert path.include?('file:///')
end

test 'wicked_pdf_asset_path should return a url when asset is url' do
Rails.configuration.assets.expects(:compile => true)
expects(:asset_path => 'http://example.com/rails.png')
assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('http://example.com/rails.png')

Rails.configuration.assets.expects(:compile => false)
expects(:asset_path => 'http://example.com/rails.png')
assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('http://example.com/rails.png')
end

test 'wicked_pdf_asset_path should return a url when asset is url without protocol' do
Rails.configuration.assets.expects(:compile => true)
expects(:asset_path => '//example.com/rails.png')
assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('//example.com/rails.png')

Rails.configuration.assets.expects(:compile => false)
expects(:asset_path => '//example.com/rails.png')
assert_equal 'http://example.com/rails.png', wicked_pdf_asset_path('//example.com/rails.png')
end

test 'WickedPdfHelper::Assets::ASSET_URL_REGEX should match various URL data type formats' do
Expand Down

0 comments on commit 2250c2f

Please sign in to comment.