Skip to content

Commit

Permalink
Memoize assets in adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
xymbol committed Oct 26, 2023
1 parent 9a039fb commit 27d603e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/inline_svg/propshaft_asset_finder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module InlineSvg
class PropshaftAssetFinder
def self.assets
Rails.application.assets
@assets ||= Rails.application.assets
end

def self.find_asset(filename)
Expand All @@ -13,6 +13,10 @@ def self.match?
rescue NameError
end

def self.reset
@assets = nil
end

def initialize(filename)
@filename = filename
end
Expand Down
6 changes: 5 additions & 1 deletion lib/inline_svg/sprockets_asset_finder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module InlineSvg
module SprocketsAssetFinder
def self.assets
Rails.application.assets
@assets ||= Rails.application.assets
end

def self.find_asset(*args, **options)
Expand All @@ -12,5 +12,9 @@ def self.match?
assets.respond_to?(:find_asset)
rescue NameError
end

def self.reset
@assets = nil
end
end
end
10 changes: 6 additions & 4 deletions spec/inline_svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ def self.named(filename); end
end

context "asset finder" do
before do
InlineSvg.reset_configuration!
end

context "when Sprockets is detected" do
it "uses the Sprockets asset finder" do
sprockets = double("Sprockets", find_asset: "some asset")
Expand Down Expand Up @@ -74,6 +70,12 @@ def self.named(filename); end
end
}.to raise_error(InlineSvg::Configuration::Invalid, /asset_finder should implement the #find_asset method/)
end

after do
InlineSvg.reset_configuration!
InlineSvg::SprocketsAssetFinder.reset
InlineSvg::PropshaftAssetFinder.reset
end
end

context "configuring a custom asset file" do
Expand Down
4 changes: 4 additions & 0 deletions spec/propshaft_asset_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
expect(InlineSvg::PropshaftAssetFinder.find_asset('some-file').pathname).to eq Pathname('/full/path/to/some-file')
end
end

after do
InlineSvg::PropshaftAssetFinder.reset
end
end
4 changes: 4 additions & 0 deletions spec/sprockets_asset_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@

expect(described_class.find_asset("some-file")).to eq "some asset"
end

after do
InlineSvg::SprocketsAssetFinder.reset
end
end

0 comments on commit 27d603e

Please sign in to comment.