Module: SvgIcon::Helper

Defined in:
lib/svg_icon/helper.rb

Instance Method Summary collapse

Instance Method Details

#icon_html_attributes(options) ⇒ Object



25
26
27
28
29
# File 'lib/svg_icon/helper.rb', line 25

def icon_html_attributes(options)
  attrs = ""
  options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
  attrs.strip
end

#svg_icon(name, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/svg_icon/helper.rb', line 3

def svg_icon(name, options = {})
  options = options.dup

  icons = SvgIcon.icons_json
  height = icons["height"] || 24
  width = icons["width"] || 24

  options[:viewBox] = "0 0 #{width} #{height}"
  options[:version] = "1.1"

  default_class = SvgIcon.configuration.default_class

  options[:class] = "#{default_class} #{options[:class]}".strip if default_class.present?

  begin
    path = icons["icons"][name]["body"]
    "<svg xmlns='http://www.w3.org/2000/svg' #{icon_html_attributes(options)}>#{path}</svg>".html_safe
  rescue
    "<svg xmlns='http://www.w3.org/2000/svg' #{icon_html_attributes(options)}><!-- SVG icon file not found: '#{name}' --></svg>".html_safe
  end
end