diff --git a/.travis.yml b/.travis.yml index 5b457122..5b45c766 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,8 @@ language: - ruby +before_install: + - sudo apt-get update -qq + - gem update bundler bundler_args: --verbose sudo: required rvm: @@ -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 diff --git a/lib/wicked_pdf/wicked_pdf_helper/assets.rb b/lib/wicked_pdf/wicked_pdf_helper/assets.rb index c95750df..4ddc790b 100644 --- a/lib/wicked_pdf/wicked_pdf_helper/assets.rb +++ b/lib/wicked_pdf/wicked_pdf_helper/assets.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/functional/wicked_pdf_helper_assets_test.rb b/test/functional/wicked_pdf_helper_assets_test.rb index 85bcc6ee..e7f8e8bf 100644 --- a/test/functional/wicked_pdf_helper_assets_test.rb +++ b/test/functional/wicked_pdf_helper_assets_test.rb @@ -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 @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index acc4ee78..c92cff8a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 \ No newline at end of file