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



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

def _t(key, *args)
  I18n.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.



69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/alchemy/base_helper.rb', line 69

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

#parse_sitemap_name(page) ⇒ Object



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

def parse_sitemap_name(page)
  if multi_language?
    pathname = "/#{Language.current.code}/#{page.urlname}"
  else
    pathname = "/#{page.urlname}"
  end
  pathname
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



62
63
64
# File 'app/helpers/alchemy/base_helper.rb', line 62

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



35
36
37
# File 'app/helpers/alchemy/base_helper.rb', line 35

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


49
50
51
52
53
54
55
# File 'app/helpers/alchemy/base_helper.rb', line 49

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.



24
25
26
27
28
29
30
31
32
# File 'app/helpers/alchemy/base_helper.rb', line 24

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