Module: Alchemy::BaseHelper

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

Instance Method Summary collapse

Instance Method Details

#_t(key, *args) ⇒ Object



3
4
5
6
# File 'app/helpers/alchemy/base_helper.rb', line 3

def _t(key, *args)
  ActiveSupport::Deprecation.warn("Alchemys `_t` method is deprecated! Use `Alchemy.t` instead.", caller.unshift)
  Alchemy.t(key, *args)
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.



61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/alchemy/base_helper.rb', line 61

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



54
55
56
# File 'app/helpers/alchemy/base_helper.rb', line 54

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

#render_icon(icon_class) ⇒ Object

Returns an icon



27
28
29
# File 'app/helpers/alchemy/base_helper.rb', line 27

def render_icon(icon_class)
  ('span', '', class: "icon #{icon_class}")
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 %>


41
42
43
44
45
46
47
# File 'app/helpers/alchemy/base_helper.rb', line 41

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

#shorten(text, length) ⇒ Object

An alias for truncate. Left here for downwards compatibilty.



10
11
12
# File 'app/helpers/alchemy/base_helper.rb', line 10

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.



16
17
18
19
20
21
22
23
24
# File 'app/helpers/alchemy/base_helper.rb', line 16

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