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_or_html_attributes = {}, html_attributes = {}) ⇒ InlineSvgComponent

Returns a new instance of InlineSvgComponent.



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

def initialize(path_or_html_attributes = {}, html_attributes = {})
  if path_or_html_attributes.is_a?(Hash)
    html_attributes = path_or_html_attributes
    path_or_html_attributes = nil
  end

  unless html_attributes.key? :raise
    html_attributes[:raise] = FoxTail::Base.fox_tail_config.raise_on_asset_not_found
  end

  super(html_attributes)

  @path = path_or_html_attributes&.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



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/components/fox_tail/inline_svg_component.rb', line 25

def call
  doc = Nokogiri::XML.parse content? ? content : asset_contents
  sanitized_html_attributes.each { |k, v| doc.child[k.to_s] = v }
  node = doc.at_css "svg"
  node[:class] = html_class
  yield node if block_given?
  node.to_s.html_safe # rubocop:disable Rails/OutputSafety
rescue FoxTail::AssetNotFound => e
  raise e if raise_error?

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