Skip to content

Commit 7d1fc8c

Browse files
committed
Set asset finder from callable
It fixes #151 by allowing the asset finder to be a callable object. This is useful when the asset pipeline is not ready when the initializer is run, for example, when using Propshaft. I don't know if this is the best way to solve this problem. On the one hand, Propshaft could be done when its initializer is run, but on the other hand, this gem's railtie assumes the asset pipeline is ready.
1 parent 8c9e8d9 commit 7d1fc8c

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

lib/inline_svg.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module InlineSvg
1919
class Configuration
2020
class Invalid < ArgumentError; end
2121

22-
attr_reader :asset_file, :asset_finder, :custom_transformations, :svg_not_found_css_class
22+
attr_reader :asset_file, :custom_transformations, :svg_not_found_css_class
2323

2424
def initialize
2525
@custom_transformations = {}
@@ -41,8 +41,15 @@ def asset_file=(custom_asset_file)
4141
end
4242
end
4343

44+
def asset_finder
45+
set_asset_finder_from_callable
46+
@asset_finder
47+
end
48+
4449
def asset_finder=(finder)
45-
@asset_finder = if finder.respond_to?(:find_asset)
50+
@asset_finder = if finder.respond_to?(:call)
51+
finder
52+
elsif finder.respond_to?(:find_asset)
4653
finder
4754
elsif finder.class.name == "Propshaft::Assembly"
4855
InlineSvg::PropshaftAssetFinder
@@ -52,7 +59,6 @@ def asset_finder=(finder)
5259
# See: https://github.com/jamesmartin/inline_svg/issues/25
5360
InlineSvg::StaticAssetFinder
5461
end
55-
asset_finder
5662
end
5763

5864
def svg_not_found_css_class=(css_class)
@@ -82,6 +88,11 @@ def incompatible_transformation?(klass)
8288
!klass.is_a?(Class) || !klass.respond_to?(:create_with_value) || !klass.instance_methods.include?(:transform)
8389
end
8490

91+
def set_asset_finder_from_callable
92+
while @asset_finder&.respond_to?(:call)
93+
self.asset_finder = @asset_finder.call
94+
end
95+
end
8596
end
8697

8798
@configuration = InlineSvg::Configuration.new

lib/inline_svg/railtie.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ class Railtie < ::Rails::Railtie
1010

1111
config.after_initialize do |app|
1212
InlineSvg.configure do |config|
13-
# Configure the asset_finder:
14-
# Only set this when a user-configured asset finder has not been
15-
# configured already.
16-
if config.asset_finder.nil?
17-
# In default Rails apps, this will be a fully operational
18-
# Sprockets::Environment instance
19-
config.asset_finder = app.instance_variable_get(:@assets)
20-
end
13+
# Configure an asset finder for Rails. This will be evaluated when the
14+
# first SVG is rendered, giving time to the asset pipeline to be done
15+
# loading.
16+
config.asset_finder = proc {
17+
app.instance_variable_get(:@assets)
18+
}
2119
end
2220
end
2321
end

spec/inline_svg_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def self.named(filename); end
1919

2020
describe InlineSvg do
2121
describe "configuration" do
22+
before do
23+
InlineSvg.reset_configuration!
24+
end
25+
2226
context "when a block is not given" do
2327
it "complains" do
2428
expect do
@@ -29,14 +33,24 @@ def self.named(filename); end
2933

3034
context "asset finder" do
3135
it "allows an asset finder to be assigned" do
32-
sprockets = double('SomethingLikeSprockets', find_asset: 'some asset')
36+
sprockets = double("Something like sprockets", find_asset: "some asset")
3337
InlineSvg.configure do |config|
3438
config.asset_finder = sprockets
3539
end
3640

3741
expect(InlineSvg.configuration.asset_finder).to eq sprockets
3842
end
3943

44+
it "allows to give a callable object that returns an asset finder" do
45+
propshaft = double("Something like propshaft", class: double(name: "Propshaft::Assembly"))
46+
callable = -> { propshaft }
47+
InlineSvg.configure do |config|
48+
config.asset_finder = callable
49+
end
50+
51+
expect(InlineSvg.configuration.asset_finder).to eq InlineSvg::PropshaftAssetFinder
52+
end
53+
4054
it "falls back to StaticAssetFinder when the provided asset finder does not implement #find_asset" do
4155
InlineSvg.configure do |config|
4256
config.asset_finder = 'Not a real asset finder'

0 commit comments

Comments
 (0)