Module: SvgIconHelper

Defined in:
app/helpers/svg_icon_helper.rb

Overview

app/helpers/svg_icon_helper.rb

Instance Method Summary collapse

Instance Method Details

#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
13
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
# File 'app/helpers/svg_icon_helper.rb', line 5

def svg_icon(name, size: 24, type: :outline, classes: [], color: 'currentColor', stroke_width: 2)
  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
    path_with_type = "icons/#{name}-#{type}"
    path_without_type = "icons/#{name}"

    if I18n.exists?("nav.icons.#{name}")
      [['icons', I18n.t("nav.icons.#{name}")].join('/'), color]
    elsif lookup_context.exists?(path_with_type, [], true)
      [path_with_type, color]
    elsif lookup_context.exists?(path_without_type, [], true)
      [path_without_type, color]
    else
      %w(icons/bug-outline red)
    end
  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
  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: "#{name}-#{type}") do
    render 'icons/bug-outline'
  end
end