Skip to content

Commit

Permalink
Add support for the filename method from sprockets environment
Browse files Browse the repository at this point in the history
  • Loading branch information
simon2k committed Jul 22, 2016
1 parent 56d79ff commit e38e92d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/inline_svg/finds_asset_paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 38 additions & 7 deletions spec/finds_asset_paths_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e38e92d

Please sign in to comment.