-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit changes the way the asset finder is configured. Instead of setting the asset finder from an initializer to a specific class, the asset finder is now detected unless configured otherwise. This allows InlineSvg to work with Sprockets, Propshaft, or a fallback static asset finder without any configuration.
- Loading branch information
Showing
8 changed files
with
107 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
module InlineSvg | ||
class PropshaftAssetFinder | ||
def self.assets | ||
Rails.application.assets | ||
end | ||
|
||
def self.find_asset(filename) | ||
new(filename) | ||
end | ||
|
||
def self.match? | ||
assets.instance_of?(Propshaft::Assembly) | ||
rescue NameError | ||
end | ||
|
||
def initialize(filename) | ||
@filename = filename | ||
end | ||
|
||
def pathname | ||
asset_path = ::Rails.application.assets.load_path.find(@filename) | ||
asset_path.path unless asset_path.nil? | ||
asset_path = self.class.assets.load_path.find(@filename) | ||
asset_path&.path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module InlineSvg | ||
module SprocketsAssetFinder | ||
def self.assets | ||
Rails.application.assets | ||
end | ||
|
||
def self.find_asset(*args, **options) | ||
assets.find_asset(*args, **options) | ||
end | ||
|
||
def self.match? | ||
assets.respond_to?(:find_asset) | ||
rescue NameError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require_relative "../lib/inline_svg" | ||
|
||
describe InlineSvg::SprocketsAssetFinder do | ||
it "returns assets from Sprockets" do | ||
sprockets = double("Sprockets", find_asset: "some asset") | ||
stub_const("Rails", double("Rails")) | ||
allow(Rails).to receive_message_chain(:application, :assets).and_return(sprockets) | ||
|
||
expect(described_class.find_asset("some-file")).to eq "some asset" | ||
end | ||
end |