Class: Avo::Icons::SvgFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/avo/icons/svg_finder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ SvgFinder

Returns a new instance of SvgFinder.



8
9
10
# File 'lib/avo/icons/svg_finder.rb', line 8

def initialize(filename)
  @filename = filename
end

Class Method Details

.find_asset(filename) ⇒ Object



4
5
6
# File 'lib/avo/icons/svg_finder.rb', line 4

def self.find_asset(filename)
  new(filename)
end

Instance Method Details

#default_strategyObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/avo/icons/svg_finder.rb', line 55

def default_strategy
  # If the app uses Propshaft, grab it from there
  if defined?(Propshaft)
    asset_path = ::Rails.application.assets.load_path.find(@filename)
    asset_path&.path
  elsif ::Rails.application.config.assets.compile
    # Grab the asset from the compiled asset manifest
    asset = ::Rails.application.assets[@filename]
    Pathname.new(asset.filename) if asset.present?
  else
    # Grab the asset from the manifest
    manifest = ::Rails.application.assets_manifest
    asset_path = manifest.assets[@filename]
    unless asset_path.nil?
      ::Rails.root.join(manifest.directory, asset_path)
    end
  end
end

#pathnameObject

Use the default static finder logic. If that doesn’t find anything, search according to our pattern:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/avo/icons/svg_finder.rb', line 13

def pathname
  Avo::Icons.cached_svgs[@filename] ||= begin
    found_asset = default_strategy

    # Use the found asset
    if found_asset.present?
      found_asset
    else
      paths.find do |path|
        File.exist? path
      end
    end
  end
end

#pathsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/avo/icons/svg_finder.rb', line 28

def paths
  base_paths = [
    Rails.root.join("app", "assets", "svgs", @filename),
    Rails.root.join(@filename),
    Avo::Icons.root.join("assets", "svgs", @filename),
    Avo::Icons.root.join("assets", "svgs", "heroicons", "outline", @filename),
    Avo::Icons.root.join("assets", "svgs", "heroicons", "solid", @filename),
    Avo::Icons.root.join("assets", "svgs", "heroicons", "mini", @filename),
    Avo::Icons.root.join("assets", "svgs", "heroicons", "micro", @filename),
    Avo::Icons.root.join("assets", "svgs", "tabler", "outline", @filename),
    Avo::Icons.root.join("assets", "svgs", "tabler", "filled", @filename),
    # Add all paths from Rails including engines
    *Rails.application.config.assets&.paths&.map { |path| File.join(path, @filename) }
  ]

  # Add custom paths from configuration
  custom_paths = Avo::Icons.configuration.custom_paths.map do |custom_path|
    if custom_path.is_a?(Proc)
      custom_path.call(@filename)
    else
      File.join(custom_path.to_s, @filename)
    end
  end

  (base_paths + custom_paths).map(&:to_s).uniq
end