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 icon name for given message type

Parameters:

  • message_type (String)

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

Returns:

  • (String)

    The icon name



94
95
96
97
98
99
100
101
102
103
# 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"
  when "hint" then "info"
  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_name, options = {}) ⇒ String

Render a Remix icon

Parameters:

  • icon_name (String)

    icon name

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • - (Object)

    style: nil [String] icon style. line or fill

  • - (Object)

    size: nil [String] icon size

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_name, options = {})
  options = {style: "line", fixed_width: true}.merge(options)
  style = options[:style] && "-#{ri_style(options[:style])}"
  classes = [
    "icon",
    "ri-#{ri_icon(icon_name)}#{style}",
    options[:size] ? "ri-#{options[:size]}" : nil,
    options[:fixed_width] ? "ri-fw" : 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