Sha256: 165463afa7e54f83fdc3658912f705614219fcf568f22ae39024c511a0c21ca7

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require_relative "application/helper"

module SuperSettings
  # Simple class for rendering ERB templates for the HTML application.
  class Application
    include Helper

    # @param layout [String, Symbol] 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.
    # @param add_to_head [String] HTML code to add to the <head> element on the page.
    # @param api_base_url [String] the base URL for the REST API.
    # @param color_scheme [Symbol] whether to use dark mode for the application UI. If +nil+, the user's system
    #                              preference will be used.
    def initialize(layout: nil, add_to_head: nil, api_base_url: nil, color_scheme: 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
    end

    # Render the web UI application HTML.
    #
    # @return [void]
    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

    private

    def render_layout
      @layout&.result(binding)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
super_settings-2.0.3 lib/super_settings/application.rb
super_settings-2.0.2 lib/super_settings/application.rb
super_settings-2.0.1 lib/super_settings/application.rb
super_settings-2.0.0 lib/super_settings/application.rb