From e38e92dc7109cd92d649211d26c2b139619ad16a Mon Sep 17 00:00:00 2001 From: Szymon Kieloch Date: Sat, 23 Jul 2016 00:44:04 +0200 Subject: [PATCH] Add support for the filename method from sprockets environment --- lib/inline_svg/finds_asset_paths.rb | 2 +- spec/finds_asset_paths_spec.rb | 45 ++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/inline_svg/finds_asset_paths.rb b/lib/inline_svg/finds_asset_paths.rb index c2e094f..e37da11 100644 --- a/lib/inline_svg/finds_asset_paths.rb +++ b/lib/inline_svg/finds_asset_paths.rb @@ -2,7 +2,7 @@ module InlineSvg class FindsAssetPaths def self.by_filename(filename) asset = configured_asset_finder.find_asset(filename) - asset && asset.pathname + asset.try(:pathname) || asset.try(:filename) end def self.configured_asset_finder diff --git a/spec/finds_asset_paths_spec.rb b/spec/finds_asset_paths_spec.rb index 57d99be..e9856cb 100644 --- a/spec/finds_asset_paths_spec.rb +++ b/spec/finds_asset_paths_spec.rb @@ -2,16 +2,47 @@ require_relative '../lib/inline_svg' describe InlineSvg::FindsAssetPaths do - it "returns fully qualified file paths from Sprockets" do - sprockets = double('SprocketsDouble') + context "when sprockets finder returns an object which supports only the pathname method" do + it "returns fully qualified file paths from Sprockets" do + sprockets = double('SprocketsDouble') - expect(sprockets).to receive(:find_asset).with('some-file'). - and_return(double(pathname: Pathname('/full/path/to/some-file'))) + expect(sprockets).to receive(:find_asset).with('some-file'). + and_return(double(pathname: Pathname('/full/path/to/some-file'))) - InlineSvg.configure do |config| - config.asset_finder = sprockets + InlineSvg.configure do |config| + config.asset_finder = sprockets + end + + expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file') + end + end + + context "when sprockets finder returns an object which supports only the filename method" do + it "returns fully qualified file paths from Sprockets" do + sprockets = double('SprocketsDouble') + + expect(sprockets).to receive(:find_asset).with('some-file'). + and_return(double(filename: Pathname('/full/path/to/some-file'))) + + InlineSvg.configure do |config| + config.asset_finder = sprockets + end + + expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file') end + end + + context "when asset is not found" do + it "returns nil" do + sprockets = double('SprocketsDouble') - expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to eq Pathname('/full/path/to/some-file') + expect(sprockets).to receive(:find_asset).with('some-file').and_return(nil) + + InlineSvg.configure do |config| + config.asset_finder = sprockets + end + + expect(InlineSvg::FindsAssetPaths.by_filename('some-file')).to be_nil + end end end