Module: SvgIconHelper

Defined in:
app/helpers/svg_icon_helper.rb

Overview

app/helpers/svg_icon_helper.rb

Instance Method Summary collapse

Instance Method Details

#fetch_svg_icon(name, size: 24, type: :outline, classes: [], color: 'currentColor', stroke_width: 2) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/svg_icon_helper.rb', line 14

def fetch_svg_icon(name, size: 24, type: :outline, classes: [], color: 'currentColor', stroke_width: 2)
  return if name.blank?

  classes = if size <= 24
              ['icon', classes].flatten.compact.join(' ')
            else
              classes.compact.flatten.join(' ')
            end
  key = "icon_partial_path:#{name}:#{type}"
  (partial, stroke) = Rails.cache.fetch(key, expires_in: SystemConfiguration.long_cache) do
    preferred_path = I18n.exists?("nav.icons.#{name}") ? 'icons/' + I18n.t("nav.icons.#{name}") : "icons/#{type}/#{name}"
    outline_path = "icons/outline/#{name}"
    filled_path = "icons/filled/#{name}"

    [preferred_path, outline_path, filled_path].collect do |path|
      [preferred_path, color] if lookup_context.exists?(path, [], true)
    end.compact.first
  end
  if partial.blank?
    partial = 'icons/outline/bug'
    stroke = 'red'
  end

  tag.svg(xmlns: "http://www.w3.org/2000/svg",
          viewBox: "0 0 24 24",
          width: size,
          height: size,
          class: classes,
          fill: 'none',
          stroke: stroke,
          'stroke-width': stroke_width,
          'stroke-linecap': 'round',
          'stroke-linejoin': 'round',
          title: name) do
    render partial
  end
rescue StandardError => e
  puts "Error: #{e}"
  tag.svg(xmlns: "http://www.w3.org/2000/svg",
          viewBox: "0 0 24 24",
          width: size,
          height: size,
          class: classes,
          fill: 'none',
          stroke: 'red',
          'stroke-width': stroke_width,
          'stroke-linecap': 'round',
          'stroke-linejoin': 'round',
          data: { bs_toggle: :tooltip },
          title: "#{type}/#{name}") do
    render 'icons/outline/bug'
  end
end

#svg_icon(name, size: 24, type: :outline, classes: [], color: 'currentColor', stroke_width: 2) ⇒ View

This method is abstract.

Render the appropriate icon based on the name and type

Returns:

  • (View)


5
6
7
8
9
10
11
12
# File 'app/helpers/svg_icon_helper.rb', line 5

def svg_icon(name, size: 24, type: :outline, classes: [], color: 'currentColor', stroke_width: 2)
  return if name.blank?

  key = ['svg_icon', name, size.to_s, type.to_s, classes.to_s, color, stroke_width.to_s].join(':')
  Rails.cache.fetch(key, expires_in: SystemConfiguration.long_cache) do
    fetch_svg_icon(name, size: size, type: type, classes: classes, color: color, stroke_width: stroke_width)
  end
end