Module: Alchemy::BaseHelper

Included in:
Admin::BaseHelper, PagesHelper
Defined in:
app/helpers/alchemy/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#message_icon_class(message_type) ⇒ String

Returns the FontAwesome icon name for given message type

Parameters:

  • message_type (String)

    The message type. One of warning, info, notice, error

Returns:

  • (String)

    The FontAwesome icon name



94
95
96
97
98
99
100
101
102
# File 'app/helpers/alchemy/base_helper.rb', line 94

def message_icon_class(message_type)
  case message_type.to_s
  when "warning", "warn", "alert" then "exclamation"
  when "notice" then "check"
  when "error" then "bug"
  else
    message_type
  end
end

#page_or_find(page) ⇒ Object

Checks if the given argument is a String or a Page object. If a String is given, it tries to find the page via page_layout Logs a warning if no page is given.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/helpers/alchemy/base_helper.rb', line 73

def page_or_find(page)
  unless Language.current
    warning("No default language set up")
    return nil
  end

  if page.is_a?(String)
    page = Language.current.pages.find_by(page_layout: page)
  end
  if page.blank?
    warning("No Page found for #{page.inspect}")
    nil
  else
    page
  end
end

#render_flash_notice(notice, style = :notice) ⇒ Object

Renders the flash partial (alchemy/admin/partials/flash)

Parameters:

  • notice (String)

    The notice you want to display

  • style (Symbol) (defaults to: :notice)

    The style of this flash. Valid values are :notice (default), :warn and :error



66
67
68
# File 'app/helpers/alchemy/base_helper.rb', line 66

def render_flash_notice(notice, style = :notice)
  render("alchemy/admin/partials/flash", flash_type: style, message: notice)
end

#render_icon(icon_class, options = {}) ⇒ String

Render a Fontawesome icon

Parameters:

  • icon_class (String)

    Fontawesome icon name

  • size:

    nil [String] Fontawesome icon size

  • transform:

    nil [String] Fontawesome transform style

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/alchemy/base_helper.rb', line 29

def render_icon(icon_class, options = {})
  options = {style: "solid"}.merge(options)
  classes = [
    "icon fa-fw",
    "fa-#{icon_class}",
    "fa#{options[:style].first}",
    options[:size] ? "fa-#{options[:size]}" : nil,
    options[:transform] ? "fa-#{options[:transform]}" : nil,
    options[:class]
  ].compact
  ("i", nil, class: classes)
end

#render_message(type = :info, msg = nil, &blk) ⇒ Object

Returns a div with an icon and the passed content The default message type is info, but you can also pass other types like :warning or :error

Usage:

<%= render_message :warning do
  <p>Caution! This is a warning!</p>
<% end %>


52
53
54
55
56
57
58
59
# File 'app/helpers/alchemy/base_helper.rb', line 52

def render_message(type = :info, msg = nil, &blk)
  icon_class = message_icon_class(type)
  if blk
     :div, render_icon(icon_class) + capture(&blk), class: "#{type} message"
  else
     :div, render_icon(icon_class) + msg, class: "#{type} message"
  end
end

#shorten(text, length) ⇒ Object

An alias for truncate. Left here for downwards compatibilty.



7
8
9
# File 'app/helpers/alchemy/base_helper.rb', line 7

def shorten(text, length)
  text.truncate(length: length)
end

#warning(message, text = nil) ⇒ Object

Logs a message in the Rails logger (warn level) and optionally displays an error message to the user.



13
14
15
16
17
18
19
20
# File 'app/helpers/alchemy/base_helper.rb', line 13

def warning(message, text = nil)
  Logger.warn(message, caller(1..1))
  unless text.nil?
    render_message(:warning) do
      text.html_safe
    end
  end
end