Module: BarkestCore::ApplicationHelper

Defined in:
app/helpers/barkest_core/application_helper.rb

Overview

This module contains some generic helper functions for use in all views and all controllers.

Instance Method Summary collapse

Instance Method Details

#error_summary(model) ⇒ Object

Renders an error summary for the specified model.

Any model that includes an errors method that returns an full_messages collection can be passed to this method.

If more than 6 errors exist, then the first 3 will be shown with a link to display all of the error messages.



33
34
35
# File 'app/helpers/barkest_core/application_helper.rb', line 33

def error_summary(model)
  render partial: 'shared/error_messages', locals: { model: model }
end

#page_title(title = nil) ⇒ Object

Generates a title string for a page using a standard format.

In this case, the standard format is either ‘application name’ or ‘application name - page specific title’.

  • title Specifies the page specific title.



21
22
23
24
# File 'app/helpers/barkest_core/application_helper.rb', line 21

def page_title(title = nil)
  return Rails.application.app_name if title.blank?
  "#{Rails.application.app_name} - #{title}"
end

#render_alert(type, message) ⇒ Object

Renders an alert message.

  • type The type of message [info, success, warn, error, danger, etc]

  • message The message to display.

To provide messages including HTML, you need to prefix the type with ‘safe_’.

render_alert(safe_info, '<strong>This</strong> is a message containing <code>HTML</code> content.')

The message can be a string, hash, or array. When an array is specified, then each array element is enumerated and joined together. The real power comes in when you specify a hash. A hash will print the key as a label and then enumerate the value (string, hash, or array) in an unordered list. Hash values are processed recursively, allowing you to create alerts with lists within lists.

render_alert(info, { 'Section 1' => [ 'Line 1', 'Line 2', 'Line 3' ] })

<label>Section 1</label>
<ul>
  <li>Line 1</li>
  <li>Line 2</li>
  <li>Line 3</li>
</ul>

render_alert(info, { 'Block A' => { 'Block A:1' => [ 'Line 1', 'Line 2' ] }})

<label>Block A</label>
<ul>
  <li>
    <label>Block A:1</label>
    <ul>
      <li>Line 1</li>
      <li>Line 2</li>
    </ul>
  </li>
</ul>


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/helpers/barkest_core/application_helper.rb', line 74

def render_alert(type, message)
  if type.to_s.index('safe_')
    type = type.to_s[5..-1]
    message = message.to_s.html_safe
  end

  type = type.to_sym

  type = :info if type == :notice
  type = :danger if type == :alert

  return nil unless [:info, :success, :danger, :warning].include?(type)

  "<div class=\"alert alert-#{type} alert-dismissible\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>#{render_alert_message(message)}</div>".html_safe
end

#render_for_namespace(view) ⇒ Object

<%= render_for_namespace ‘layout_partial_name’ %>

Only renders if the current controller is namespaced. If the specified partial doesn’t exist, no error is raised.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/barkest_core/application_helper.rb', line 95

def render_for_namespace view
  nmspc = params[:controller].include?('/') ? params[:controller].rpartition('/')[0] : nil
  # recurse down the namespace tree to see if we get any hits.
  until nmspc.blank?
    template = "layouts/#{nmspc}/_#{view}"
    partial = "layouts/#{nmspc}/#{view}"
    if lookup_context.template_exists?(template)
      return render(partial)
    end
    nmspc = nmspc.include?('/') ? nmspc.rpartition('/')[0] : nil
  end
end