class SimpleMessages::Builder

Constants

ALERT_CLASS_MAPPING

Attributes

body[RW]
closable[RW]
header[RW]
html[RW]
kind[RW]

Public Class Methods

new(params = {}) click to toggle source
# File lib/simple_messages/builder.rb, line 7
def initialize(params = {})
  params.each do |attr, value|
    instance_variable_set "@#{attr}", value
  end

  set_defaults
end

Public Instance Methods

closable?() click to toggle source
# File lib/simple_messages/builder.rb, line 15
def closable?
  closable
end
close_wrapper() click to toggle source
# File lib/simple_messages/builder.rb, line 32
def close_wrapper
  wrapper = "<button type='button' class='close' data-dismiss='alert'>"
  wrapper << "<span aria-hidden='true'>&times;</span>"
  wrapper << "<span class='sr-only'>#{close_text}</span>"
  wrapper << "</button>"
  wrapper
end
has_header?() click to toggle source
# File lib/simple_messages/builder.rb, line 19
def has_header?
  header.present?
end
header_wrapper() click to toggle source
# File lib/simple_messages/builder.rb, line 40
def header_wrapper
  return unless has_header?

  "<strong>#{header}</strong><br>"
end
to_html() click to toggle source
# File lib/simple_messages/builder.rb, line 23
def to_html
  wrapper = "<div #{alert_attributes}>"
  wrapper << close_wrapper  if closable?
  wrapper << header_wrapper if has_header?
  wrapper << body_to_s
  wrapper << "</div>"
  wrapper
end

Private Instance Methods

adjust_html() click to toggle source
# File lib/simple_messages/builder.rb, line 60
def adjust_html
  @html ||= {}
  @html = { class: '', role: 'alert' }.merge(@html)
  @html[:class] << ' alert '
  @html[:class] << alert_kind_class
  @html[:class] << ' alert-dismissible' if closable?
  @html[:class].strip!
end
alert_attributes() click to toggle source
# File lib/simple_messages/builder.rb, line 69
def alert_attributes
  adjust_html

  html_to_attributes
end
alert_kind_class() click to toggle source
# File lib/simple_messages/builder.rb, line 75
def alert_kind_class
  "alert-#{ALERT_CLASS_MAPPING.fetch(kind.to_sym, kind)}"
end
body_to_s() click to toggle source
# File lib/simple_messages/builder.rb, line 79
def body_to_s
  return @body unless @body.is_a? Array

  @body.join '<br />'
end
closable_default() click to toggle source
# File lib/simple_messages/builder.rb, line 56
def closable_default
  @closable = true if @closable.nil?
end
close_text() click to toggle source
# File lib/simple_messages/builder.rb, line 85
def close_text
  I18n.t 'text.close', default: 'Close'
end
html_to_attributes() click to toggle source
# File lib/simple_messages/builder.rb, line 89
def html_to_attributes
  @html.collect { |a, v| "#{a}='#{v}'" }.join(' ')
end
kind_default() click to toggle source
# File lib/simple_messages/builder.rb, line 52
def kind_default
  @kind = 'success' if @kind.nil?
end
set_defaults() click to toggle source
# File lib/simple_messages/builder.rb, line 47
def set_defaults
  kind_default
  closable_default
end