Skip to content

Commit

Permalink
Merge pull request #480 from Bonias/fix-for-sprockets-3-0
Browse files Browse the repository at this point in the history
Fix #476 issue - problem with sprockets 3.0
  • Loading branch information
unixmonkey committed Jan 8, 2016
2 parents cc48749 + 2ec3559 commit de5b0ff
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language:
- ruby
before_install:
- sudo apt-get update -qq
- gem update bundler
bundler_args: --verbose
sudo: required
rvm:
Expand All @@ -26,6 +29,7 @@ matrix:
include:
- rvm: 1.8.7
before_install:
- sudo apt-get update -qq
- gem update --system 1.8.25
- gem --version
gemfile: gemfiles/2.3.gemfile
Expand Down
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
61 changes: 52 additions & 9 deletions test/functional/wicked_pdf_helper_assets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase

if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
source = File.new('test/fixtures/wicked.css', 'r')
destination = Rails.root.join('app', 'assets', 'stylesheets', 'wicked.css')
File.open(destination, 'w') { |f| f.write(source) }
assert_match /data:text\/css;base64,.+/, wicked_pdf_asset_base64('wicked.css')
end

Expand All @@ -18,28 +15,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
6 changes: 6 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@
require 'wicked_pdf'

Rails.backtrace_cleaner.remove_silencers!

if (assets_dir = Rails.root.join('app/assets')) && File.directory?(assets_dir)
destination = assets_dir.join('stylesheets/wicked.css')
source = File.read('test/fixtures/wicked.css')
File.open(destination, 'w') { |f| f.write(source) }
end

1 comment on commit de5b0ff

@mathieujobin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this did not solve my issue with version 1.0.6 and

    sprockets (3.7.2)
    sprockets-rails (3.2.1)

Please sign in to comment.