Module: Alchemy::BaseHelper
- Included in:
- Admin::BaseHelper, PagesHelper
- Defined in:
- app/helpers/alchemy/base_helper.rb
Instance Method Summary collapse
-
#message_icon_class(message_type) ⇒ String
Returns the FontAwesome icon name for given message type.
-
#page_or_find(page) ⇒ Object
Checks if the given argument is a String or a Page object.
-
#render_flash_notice(notice, style = :notice) ⇒ Object
Renders the flash partial (
alchemy/admin/partials/flash
). -
#render_icon(icon_class, options = {}) ⇒ String
Render a Fontawesome icon.
-
#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.
-
#shorten(text, length) ⇒ Object
An alias for truncate.
-
#warning(message, text = nil) ⇒ Object
Logs a message in the Rails logger (warn level) and optionally displays an error message to the user.
Instance Method Details
#message_icon_class(message_type) ⇒ String
Returns the FontAwesome icon name for given message type
90 91 92 93 94 95 96 97 98 |
# File 'app/helpers/alchemy/base_helper.rb', line 90 def () case .to_s when 'warning', 'warn', 'alert' then 'exclamation' when 'notice' then 'check' when 'error' then 'bug' else 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
)
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
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, = {}) = {style: 'solid'}.merge() classes = [ "icon fa-fw", "fa-#{icon_class}", "fa#{[:style].first}", [:size] ? "fa-#{[:size]}" : nil, [:transform] ? "fa-#{[:transform]}" : nil, [:class] ].compact content_tag('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 (type = :info, msg = nil, &blk) icon_class = (type) if block_given? content_tag :div, render_icon(icon_class) + capture(&blk), class: "#{type} message" else content_tag :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(, text = nil) Logger.warn(, caller(0..0)) unless text.nil? warning = content_tag('p', class: 'content_editor_error') do render_icon('warning') + text end return warning end end |