Skip to content

Commit 90f2d07

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 1a1e2e0 commit 90f2d07

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
@@ -18,7 +18,7 @@ module InlineSvg
1818
class Configuration
1919
class Invalid < ArgumentError; end
2020

21-
attr_reader :asset_file, :asset_finder, :custom_transformations, :svg_not_found_css_class
21+
attr_reader :asset_file, :custom_transformations, :svg_not_found_css_class
2222

2323
def initialize
2424
@custom_transformations = {}
@@ -40,8 +40,15 @@ def asset_file=(custom_asset_file)
4040
end
4141
end
4242

43+
def asset_finder
44+
set_asset_finder_from_callable
45+
@asset_finder
46+
end
47+
4348
def asset_finder=(finder)
44-
@asset_finder = if finder.respond_to?(:find_asset)
49+
@asset_finder = if finder.respond_to?(:call)
50+
finder
51+
elsif finder.respond_to?(:find_asset)
4552
finder
4653
elsif finder.class.name == "Propshaft::Assembly"
4754
InlineSvg::PropshaftAssetFinder
@@ -51,7 +58,6 @@ def asset_finder=(finder)
5158
# See: https://github.com/jamesmartin/inline_svg/issues/25
5259
InlineSvg::StaticAssetFinder
5360
end
54-
asset_finder
5561
end
5662

5763
def svg_not_found_css_class=(css_class)
@@ -81,6 +87,11 @@ def incompatible_transformation?(klass)
8187
!klass.is_a?(Class) || !klass.respond_to?(:create_with_value) || !klass.instance_methods.include?(:transform)
8288
end
8389

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

8697
@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)