From 27d603e934acdcf2ee5c733b24518e92bccc4487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mugnolo?= Date: Thu, 26 Oct 2023 07:50:15 -0400 Subject: [PATCH] Memoize assets in adapters --- lib/inline_svg/propshaft_asset_finder.rb | 6 +++++- lib/inline_svg/sprockets_asset_finder.rb | 6 +++++- spec/inline_svg_spec.rb | 10 ++++++---- spec/propshaft_asset_finder_spec.rb | 4 ++++ spec/sprockets_asset_finder_spec.rb | 4 ++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/inline_svg/propshaft_asset_finder.rb b/lib/inline_svg/propshaft_asset_finder.rb index ebd094f..051c5ef 100644 --- a/lib/inline_svg/propshaft_asset_finder.rb +++ b/lib/inline_svg/propshaft_asset_finder.rb @@ -1,7 +1,7 @@ module InlineSvg class PropshaftAssetFinder def self.assets - Rails.application.assets + @assets ||= Rails.application.assets end def self.find_asset(filename) @@ -13,6 +13,10 @@ def self.match? rescue NameError end + def self.reset + @assets = nil + end + def initialize(filename) @filename = filename end diff --git a/lib/inline_svg/sprockets_asset_finder.rb b/lib/inline_svg/sprockets_asset_finder.rb index 34e7c85..d427f87 100644 --- a/lib/inline_svg/sprockets_asset_finder.rb +++ b/lib/inline_svg/sprockets_asset_finder.rb @@ -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) @@ -12,5 +12,9 @@ def self.match? assets.respond_to?(:find_asset) rescue NameError end + + def self.reset + @assets = nil + end end end diff --git a/spec/inline_svg_spec.rb b/spec/inline_svg_spec.rb index 471f698..453f919 100644 --- a/spec/inline_svg_spec.rb +++ b/spec/inline_svg_spec.rb @@ -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") @@ -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 diff --git a/spec/propshaft_asset_finder_spec.rb b/spec/propshaft_asset_finder_spec.rb index abb5fa4..d48eeec 100644 --- a/spec/propshaft_asset_finder_spec.rb +++ b/spec/propshaft_asset_finder_spec.rb @@ -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 diff --git a/spec/sprockets_asset_finder_spec.rb b/spec/sprockets_asset_finder_spec.rb index db65d83..965e7ad 100644 --- a/spec/sprockets_asset_finder_spec.rb +++ b/spec/sprockets_asset_finder_spec.rb @@ -8,4 +8,8 @@ expect(described_class.find_asset("some-file")).to eq "some asset" end + + after do + InlineSvg::SprocketsAssetFinder.reset + end end