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
-
#add_to_head ⇒ String
Additional HTML code that should go into the <head> element on the page.
-
#api_base_url ⇒ String
The base URL for the REST API.
-
#application_header ⇒ Object
Render the header for the web pages using values set in the configuration.
-
#application_name ⇒ Object
Return the application name set by the configuration or a default value.
-
#color_scheme ⇒ Boolean?
Whether to use dark mode for the application UI.
-
#content_tag(tag, body, options) ⇒ Object
Render an HTML tag with body content.
-
#html_attributes(options) ⇒ Object
Format a hash into HTML attributes.
-
#html_escape(text) ⇒ String
Escape text for use in HTML.
-
#icon_button(icon, title:, color:, js_class:, url: nil, disabled: false, style: {}, link_style: nil) ⇒ String
Render an icon image as a link tag.
-
#icon_image(name, options = {}) ⇒ String
Render an image tag for one of the SVG images in the images directory.
-
#javascript_tag ⇒ Object
Render the scripts.js file as an inline <script> tag.
-
#layout_style_tag ⇒ Object
Render the styles.css as an inline <style> tag.
-
#render_partial(erb_file) ⇒ String
Render an ERB template.
-
#style_tag ⇒ Object
Render the styles.css as an inline <style> tag.
-
#tag(tag, options) ⇒ Object
Render an HTML tag without any body content.
Instance Method Details
#add_to_head ⇒ String
Additional HTML code that should go into the <head> element on the page.
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_url ⇒ String
The base URL for the REST API.
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_header ⇒ Object
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.application_logo) content = tag(:img, src: config.application_logo, alt: "") + content end if config.application_link content_tag(:a, content, href: config.application_link) else content end end |
#application_name ⇒ Object
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_scheme ⇒ Boolean?
Whether to use dark mode for the application UI.
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 content_tag(tag, body, ) "<#{tag} #{html_attributes()}>#{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() = [] .each do |name, value| << "#{name}=\"#{html_escape(value.to_s)}\"" end .join(" ") end |
#html_escape(text) ⇒ String
Escape text for use in HTML.
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.
108 109 110 111 112 |
# File 'lib/super_settings/application/helper.rb', line 108 def (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)) content_tag(: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.
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, = {}) svg = ICON_SVG[name.to_s] style = ([:style] || {}) css = DEFAULT_ICON_STYLE.merge(style).map { |name, value| "#{name}: #{value}" }.join("; ") = .merge(style: css, class: "super-settings-icon") if [:data].is_a?(Hash) [:data].each do |key, value| ["data-#{key}"] = value end .delete(:data) end content_tag(:span, svg, ) end |
#javascript_tag ⇒ Object
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_tag ⇒ Object
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.
64 65 66 67 |
# File 'lib/super_settings/application/helper.rb', line 64 def render_partial(erb_file) template = ERB.new(File.read(File.(erb_file, __dir__))) template.result(binding) end |
#style_tag ⇒ Object
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, ) "<#{tag} #{html_attributes()}>" end |