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
94 95 96 97 98 99 100 101 102 |
# File 'app/helpers/alchemy/base_helper.rb', line 94 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.
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
)
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_class, options = {}) ⇒ String
Render a Fontawesome icon
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_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 %>
52 53 54 55 56 57 58 59 |
# File 'app/helpers/alchemy/base_helper.rb', line 52 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 |
# File 'app/helpers/alchemy/base_helper.rb', line 13 def warning(, text = nil) Logger.warn(, caller(1..1)) unless text.nil? (:warning) do text.html_safe end end end |