-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change asset finder configuration #155
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The goal is to have these classes behave like lazy, auto-detect adapters. Also, to draw a line between the asset finder and the asset result. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
module InlineSvg | ||
class PropshaftAssetFinder | ||
def self.find_asset(filename) | ||
new(filename) | ||
class Asset | ||
attr_reader :asset_finder, :filename | ||
|
||
def initialize(filename, asset_finder) | ||
@asset_finder = asset_finder | ||
@filename = filename | ||
end | ||
|
||
delegate :assets, to: :asset_finder | ||
|
||
def pathname | ||
asset_path = assets.load_path.find(@filename) | ||
asset_path&.path | ||
end | ||
end | ||
|
||
attr_reader :assets | ||
|
||
def initialize(assets = ::Rails.application.assets) | ||
@assets = assets | ||
end | ||
|
||
class << self | ||
delegate :find_asset, to: :new | ||
end | ||
|
||
def initialize(filename) | ||
@filename = filename | ||
def find_asset(filename) | ||
Asset.new(filename, self) | ||
end | ||
|
||
def pathname | ||
asset_path = ::Rails.application.assets.load_path.find(@filename) | ||
asset_path.path unless asset_path.nil? | ||
def match? | ||
defined?(::Propshaft) && assets.instance_of?(Propshaft::Assembly) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module InlineSvg | ||
class SprocketsAssetFinder | ||
attr_reader :assets | ||
|
||
def initialize(assets = ::Rails.application.assets) | ||
@assets = assets | ||
end | ||
|
||
class << self | ||
delegate :find_asset, to: :new | ||
end | ||
|
||
delegate :find_asset, to: :assets | ||
|
||
def match? | ||
assets.respond_to?(:find_asset) | ||
end | ||
end | ||
end |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could name this "Rails static asset finder" as it depends on a Rails configuration, i.e. it cannot work stand-alone or with a non-Rails environment. |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, we could raise if no asset finder is set or detected.