Class: SuperSettings::Application

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/super_settings/application.rb

Overview

Simple class for rendering ERB templates for the HTML application.

Constant Summary

Constants included from Helper

Helper::DEFAULT_ICON_STYLE, Helper::GEAR_SVG, Helper::ICON_BUTTON_STYLE, Helper::ICON_SVG

Instance Method Summary collapse

Methods included from Helper

#add_to_head, #api_base_url, application_header, #application_header, #application_name, #color_scheme, #content_tag, #dark_mode_selector, #html_attributes, #html_escape, #icon_button, #icon_image, #javascript_tag, #layout_style_tag, #read_only?, #render_partial, #style_tag, #t, #tag, #text_direction, #translations_json

Constructor Details

#initialize(layout: nil, add_to_head: nil, api_base_url: nil, color_scheme: nil, dark_mode_selector: nil, read_only: false, locale: nil) ⇒ Application

Returns a new instance of Application.

Parameters:

  • layout (String, Symbol) (defaults to: nil)

    path to an ERB template to use as the layout around the application UI. You can pass the symbol :default to use the default layout that ships with the gem.

  • add_to_head (String) (defaults to: nil)

    HTML code to add to the element on the page.

  • api_base_url (String) (defaults to: nil)

    the base URL for the REST API.

  • color_scheme (Symbol) (defaults to: nil)

    whether to use dark mode for the application UI. If nil, the user's system preference will be used.

  • dark_mode_selector (String) (defaults to: nil)

    a CSS selector that sets dark mode when it matches an element in the page. This is an alternative to using the color_scheme option.

  • read_only (Boolean) (defaults to: false)

    whether to render the application in read-only mode (edit controls hidden).

  • locale (String) (defaults to: nil)

    the locale code for translations (default: "en").



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/super_settings/application.rb', line 20

def initialize(layout: nil, add_to_head: nil, api_base_url: nil, color_scheme: nil, dark_mode_selector: nil, read_only: false, locale: nil)
  if layout
    layout = File.expand_path(File.join("application", "layout.html.erb"), __dir__) if layout == :default
    @layout = ERB.new(File.read(layout)) if layout
    @add_to_head = add_to_head
  else
    @layout = nil
    @add_to_head = nil
  end

  @api_base_url = api_base_url
  @color_scheme = color_scheme&.to_sym
  @dark_mode_selector = dark_mode_selector
  @read_only = !!read_only
  @locale = locale || SuperSettings::MiniI18n::DEFAULT_LOCALE
end

Instance Method Details

#renderString

Render the web UI application HTML.

Returns:

  • (String)

    the rendered HTML



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

def render
  template = ERB.new(File.read(File.expand_path(File.join("application", "index.html.erb"), __dir__)))
  html = template.result(binding)
  html = render_layout { html } if @layout
  html = html.html_safe if html.respond_to?(:html_safe)
  html
end