Module: SuperSettings::Helper

Included in:
Application
Defined in:
lib/super_settings/application/helper.rb

Overview

Helper functions used for rendering the Super Settings HTML application. These methods are mixed in to the Application class so they are accessible from the ERB templates.

Constant Summary collapse

ICON_SVG =
Dir.glob(File.join(__dir__, "images", "*.svg")).each_with_object({}) do |file, cache|
  svg = File.read(file).chomp
  cache[File.basename(file, ".svg")] = svg
end.freeze
ICON_BUTTON_STYLE =
{
  cursor: "pointer",
  width: "1.35rem",
  height: "1.35rem",
  "min-width": "20px",
  "min-height": "20px",
  "margin-top": "0.25rem",
  "margin-right": "0.5rem"
}.freeze
DEFAULT_ICON_STYLE =
{
  width: "1rem",
  height: "1rem",
  display: "inline-block"
}.freeze

Instance Method Summary collapse

Instance Method Details

#add_to_headString

Additional HTML code that should go into the <head> element on the page.

Returns:

  • (String)


155
156
157
# File 'lib/super_settings/application/helper.rb', line 155

def add_to_head
  @add_to_head if defined?(@add_to_head)
end

#api_base_urlString

The base URL for the REST API.

Returns:

  • (String)


162
163
164
# File 'lib/super_settings/application/helper.rb', line 162

def api_base_url
  @api_base_url if defined?(@api_base_url)
end

#application_headerObject

Render the header for the web pages using values set in the configuration.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/super_settings/application/helper.rb', line 120

def application_header
  config = SuperSettings.configuration.controller
  content = html_escape("#{application_name} Settings")
  if Coerce.present?(config.)
    content = tag(:img, src: config., alt: "") + content
  end
  if config.application_link
    (:a, content, href: config.application_link)
  else
    content
  end
end

#application_nameObject

Return the application name set by the configuration or a default value.



115
116
117
# File 'lib/super_settings/application/helper.rb', line 115

def application_name
  html_escape(SuperSettings.configuration.controller.application_name || "Application")
end

#color_schemeBoolean?

Whether to use dark mode for the application UI.

Returns:

  • (Boolean, nil)


169
170
171
# File 'lib/super_settings/application/helper.rb', line 169

def color_scheme
  @color_scheme if defined?(@color_scheme)
end

#content_tag(tag, body, options) ⇒ Object

Render an HTML tag with body content.



139
140
141
# File 'lib/super_settings/application/helper.rb', line 139

def (tag, body, options)
  "<#{tag} #{html_attributes(options)}>#{body}</#{tag}>"
end

#html_attributes(options) ⇒ Object

Format a hash into HTML attributes.



144
145
146
147
148
149
150
# File 'lib/super_settings/application/helper.rb', line 144

def html_attributes(options)
  html_options = []
  options.each do |name, value|
    html_options << "#{name}=\"#{html_escape(value.to_s)}\""
  end
  html_options.join(" ")
end

#html_escape(text) ⇒ String

Escape text for use in HTML.

Parameters:

  • text (String)

    the text to escape

Returns:

  • (String)

    the escaped text



73
74
75
# File 'lib/super_settings/application/helper.rb', line 73

def html_escape(text)
  ERB::Util.html_escape(text)
end

#icon_button(icon, title:, color:, js_class:, url: nil, disabled: false, style: {}, link_style: nil) ⇒ String

Render an icon image as a link tag.

Parameters:

  • icon (String)

    the name of the icon to render

  • title (String)

    the title/tooltip for the button

  • color (String)

    the color for the icon

  • js_class (String)

    CSS class for JavaScript behavior

  • url (String) (defaults to: nil)

    the URL for the link (optional)

  • disabled (Boolean) (defaults to: false)

    whether the button is disabled

  • style (Hash) (defaults to: {})

    CSS styles for the icon

  • link_style (String) (defaults to: nil)

    CSS styles for the link

Returns:

  • (String)

    the HTML for the icon button



108
109
110
111
112
# File 'lib/super_settings/application/helper.rb', line 108

def icon_button(icon, title:, color:, js_class:, url: nil, disabled: false, style: {}, link_style: nil)
  url = "#" if Coerce.blank?(url)
  image = icon_image(icon, alt: title, style: ICON_BUTTON_STYLE.merge(style).merge(color: color))
  (:a, image, href: url, class: js_class, disabled: disabled, style: link_style, title: title)
end

#icon_image(name, options = {}) ⇒ String

Render an image tag for one of the SVG images in the images directory. If the :color option is specified, it will be applied to the SVG image.

Parameters:

  • name (String)

    the name of the icon to render

  • options (Hash) (defaults to: {})

    options for the icon (style, color, etc.)

Returns:

  • (String)

    the HTML for the icon



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/super_settings/application/helper.rb', line 83

def icon_image(name, options = {})
  svg = ICON_SVG[name.to_s]
  style = (options[:style] || {})
  css = DEFAULT_ICON_STYLE.merge(style).map { |name, value| "#{name}: #{value}" }.join("; ")
  options = options.merge(style: css, class: "super-settings-icon")
  if options[:data].is_a?(Hash)
    options[:data].each do |key, value|
      options["data-#{key}"] = value
    end
    options.delete(:data)
  end
  (:span, svg, options)
end

#javascript_tagObject

Render the scripts.js file as an inline <script> tag.



29
30
31
32
33
34
35
36
37
38
# File 'lib/super_settings/application/helper.rb', line 29

def javascript_tag
  <<~HTML
    <script>
      #{File.read(File.join(__dir__, "scripts.js"))}
      #{File.read(File.join(__dir__, "api.js"))}
      #{"SuperSettingsAPI.authenticationUrl = '#{SuperSettings.authentication_url.gsub("'", "\\'")}';" if SuperSettings.authentication_url}
      #{SuperSettings.web_ui_javascript}
    </script>
  HTML
end

#layout_style_tagObject

Render the styles.css as an inline <style> tag.



51
52
53
54
55
56
57
58
# File 'lib/super_settings/application/helper.rb', line 51

def layout_style_tag
  <<~HTML
    <style type="text/css">
      #{render_partial("layout_vars.css.erb")}
      #{File.read(File.join(__dir__, "layout_styles.css"))}
    </style>
  HTML
end

#render_partial(erb_file) ⇒ String

Render an ERB template.

Parameters:

  • erb_file (String)

    the path to the ERB file to render

Returns:

  • (String)

    the rendered HTML



64
65
66
67
# File 'lib/super_settings/application/helper.rb', line 64

def render_partial(erb_file)
  template = ERB.new(File.read(File.expand_path(erb_file, __dir__)))
  template.result(binding)
end

#style_tagObject

Render the styles.css as an inline <style> tag.



41
42
43
44
45
46
47
48
# File 'lib/super_settings/application/helper.rb', line 41

def style_tag
  <<~HTML
    <style type="text/css">
      #{render_partial("style_vars.css.erb")}
      #{File.read(File.join(__dir__, "styles.css"))}
    </style>
  HTML
end

#tag(tag, options) ⇒ Object

Render an HTML tag without any body content.



134
135
136
# File 'lib/super_settings/application/helper.rb', line 134

def tag(tag, options)
  "<#{tag} #{html_attributes(options)}>"
end