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


90
91
92
93
94
95
96
97
98
# File 'app/helpers/alchemy/base_helper.rb', line 90

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.


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

def page_or_find(page)
  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}")
    return
  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


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

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)

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

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 %>

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

def render_message(type = :info, msg = nil, &blk)
  icon_class = message_icon_class(type)
  if block_given?
     :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
21
# File 'app/helpers/alchemy/base_helper.rb', line 13

def warning(message, text = nil)
  Logger.warn(message, caller(0..0))
  unless text.nil?
    warning = ('p', class: 'content_editor_error') do
      render_icon('warning') + text
    end
    return warning
  end
end