Repository URL to install this package:
Version:
4.0.0.pre.1 ▾
|
# frozen_string_literal: true
# Sinatra banner module for showing site-wide messages
module Sinatra
# Banner implementation
module Banner
# Class to handle parsing the environment for banner configuration and exposing it to the view
class Banner
def self.from_env
text = ENV['BANNER_TEXT']
opts = {}
opts[:type] = ENV['BANNER_TYPE'] if ENV.key? 'BANNER_TYPE'
opts[:dismissable] = ENV['BANNER_DISMISSABLE'].to_s =~ /^[1ty]/i if ENV.key? 'BANNER_DISMISSABLE'
new(text, **opts)
end
def initialize(text, type: 'warning', dismissable: false)
@text = text.to_s.strip
type = type.downcase.strip
type = 'warning' unless %w[primary secondary success danger warning info light dark].include? type
@type = type
@dismissable = dismissable
end
attr_reader :text, :type
def visible?
!@text.empty?
end
def dismissable?
@dismissable
end
end
# Container for helper methods
module Helpers
def banner
@banner ||= Banner.from_env
end
end
def self.registered(app) # rubocop:disable Metrics/MethodLength
app.helpers Helpers
app.template :banner do
%(
<% if banner.visible? %>
<div class="alert alert-<%= banner.type %> alert-dismissible fade show" role="alert">
<%= banner.text %>
<% if banner.dismissable? %>
<% if settings.respond_to?(:bootstrap_major_version) && settings.bootstrap_major_version == 5 %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
<% else %>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<% end %>
<% end %>
</div>
<% end %>
)
end
end
end
end