Class: UltraSettings::WebView

Inherits:
Object
  • Object
show all
Includes:
RenderHelper
Defined in:
lib/ultra_settings/web_view.rb

Overview

Helper class for rendering the settings information in an HTML page.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RenderHelper

#html_escape, #render_partial

Constructor Details

#initialize(color_scheme: :light) ⇒ WebView

Initialize a new WebView with the specified color scheme.

Parameters:

  • color_scheme (Symbol, nil) (defaults to: :light)

    The color scheme to use in the UI. This can be :light, :dark, or :system. When nil, a toggle control is rendered and [data-theme=dark] is used as the dark mode CSS selector.



15
16
17
18
19
20
21
# File 'lib/ultra_settings/web_view.rb', line 15

def initialize(color_scheme: :light)
  @color_scheme = color_scheme&.to_sym
  @dark_mode_selector = @color_scheme.nil? ? "[data-theme=dark]" : nil
  @layout_template = ViewHelper.erb_template("layout.html.erb")
  @layout_css = scheme_layout_css(@color_scheme, @dark_mode_selector)
  @locale = nil
end

Instance Attribute Details

#layout_cssObject (readonly)

Returns the value of attribute layout_css.



8
9
10
# File 'lib/ultra_settings/web_view.rb', line 8

def layout_css
  @layout_css
end

Instance Method Details

#contentString

Get the content for the settings page.

Returns:

  • (String)

    The HTML content for the settings.



50
51
52
53
54
55
56
# File 'lib/ultra_settings/web_view.rb', line 50

def content
  UltraSettings::ApplicationView.new(
    color_scheme: @color_scheme || :light,
    dark_mode_selector: @dark_mode_selector,
    locale: @locale || UltraSettings::MiniI18n::DEFAULT_LOCALE
  ).render
end

#render_layoutString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Render the layout template in the context of this instance.

Returns:

  • (String)

    The rendered HTML page.



43
44
45
# File 'lib/ultra_settings/web_view.rb', line 43

def render_layout
  @layout_template.result(binding)
end

#render_settings(request = nil, locale: UltraSettings::MiniI18n::DEFAULT_LOCALE) ⇒ String

Render the complete settings page HTML.

This instance may be shared between concurrent requests, so per-request state is set on a copy of the view rather than on the shared instance.

Parameters:

  • request (Rack::Request, nil) (defaults to: nil)

    The current Rack request. It is accepted for backward compatibility but is not used by the view; access control and locale resolution are the caller's responsibility.

  • locale (String) (defaults to: UltraSettings::MiniI18n::DEFAULT_LOCALE)

    The locale code for translations.

Returns:

  • (String)

    The rendered HTML page.



33
34
35
36
37
# File 'lib/ultra_settings/web_view.rb', line 33

def render_settings(request = nil, locale: UltraSettings::MiniI18n::DEFAULT_LOCALE)
  renderer = dup
  renderer.instance_variable_set(:@locale, locale)
  renderer.render_layout
end

#t(key) ⇒ String

Look up a translation key for the current locale.

Parameters:

  • key (String)

    dotted translation key

Returns:

  • (String)


62
63
64
# File 'lib/ultra_settings/web_view.rb', line 62

def t(key)
  UltraSettings::MiniI18n.t(key, locale: @locale || UltraSettings::MiniI18n::DEFAULT_LOCALE)
end

#text_directionString

Return the text direction (+"ltr"+ or "rtl") for the current locale.

Returns:

  • (String)


69
70
71
# File 'lib/ultra_settings/web_view.rb', line 69

def text_direction
  UltraSettings::MiniI18n.text_direction(@locale || UltraSettings::MiniI18n::DEFAULT_LOCALE)
end