Class: FoxTail::InlineSvgComponent

Inherits:
BaseComponent show all
Defined in:
app/components/fox_tail/inline_svg_component.rb

Overview

Displays an SVG file inline

Direct Known Subclasses

IconBaseComponent, SpinnerComponent

Instance Attribute Summary collapse

Attributes inherited from BaseComponent

#html_attributes

Instance Method Summary collapse

Methods inherited from BaseComponent

classname_merger, stimulus_merger, use_stimulus?, #with_html_attributes, #with_html_class

Methods inherited from Base

fox_tail_config

Constructor Details

#initialize(path, html_attributes = {}) ⇒ InlineSvgComponent

Returns a new instance of InlineSvgComponent.



10
11
12
13
14
15
16
17
18
# File 'app/components/fox_tail/inline_svg_component.rb', line 10

def initialize(path, html_attributes = {})
  unless html_attributes.key? :raise
    html_attributes[:raise] = FoxTail::Base.fox_tail_config.raise_on_asset_not_found
  end

  super(html_attributes)

  @path = path.to_s
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'app/components/fox_tail/inline_svg_component.rb', line 6

def path
  @path
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/components/fox_tail/inline_svg_component.rb', line 20

def call
  doc = Nokogiri::XML.parse asset_contents
  sanitized_html_attributes.each { |k, v| doc.child[k.to_s] = v }
  doc.child[:class] = html_class
  yield doc if block_given?
  doc.child.to_s.html_safe
rescue FoxTail::AssetNotFound => e
  raise e if raise_error?

  Rails.logger.warn "[FoxTail::InlineSvgComponent] #{e.message}"
   :svg, nil, class: "hidden asset-not-found"
end