Module: PagesHelper

Includes:
AssetsHelper, NavigationHelper
Defined in:
app/helpers/pages_helper.rb

Overview

This module provides helper methods rendering pages and slices.

Instance Method Summary collapse

Methods included from AssetsHelper

#image_if_present, #link_image_if_linkable

Methods included from NavigationHelper

#link_to_page, #navigation, #primary_navigation, #secondary_navigation

Instance Method Details

#add_tracking_code?Boolean

If a tracking code should be added to the page. Returns true when the app is in production mode and the user is not signed into the CMS

Returns:

  • (Boolean)


115
116
117
# File 'app/helpers/pages_helper.rb', line 115

def add_tracking_code?
  Rails.env.production? && ! admin_signed_in?
end

#container(container, options = {}) ⇒ String

Defines and renders a container in a layout.

For example adding the following to a layout will create two containers called container_one and container_two

<article class="container_one">
  <%= container "container_one" %>
</article>
<aside class="container_two">
  <%= container "container_two" %>
</aside>

Parameters:

  • container (String)

    Name of container

Returns:

  • (String)

    Contents of rendered slices in container



21
22
23
24
25
# File 'app/helpers/pages_helper.rb', line 21

def container(container, options = {})
  if @slice_renderer.present?
    @slice_renderer.render_container(container)
  end
end

#edit_in_cmsString

Quick link to edit the current page in the CMS

Returns:

  • (String)

    Edit in CMS link



56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/pages_helper.rb', line 56

def edit_in_cms
  if admin_signed_in?
    links = []
    links << link_to("Edit #{@page.class.to_s.underscore.humanize} in CMS", admin_page_path(@page), target: '_blank')
    links << link_to('Edit template in CMS', admin_page_path(@page.parent, entries: 1), target: '_blank') if @page.entry?
    content = links.collect {|l| (:p, l) }.join.html_safe

    (:div, content, id: 'edit_in_cms')
  end
end

#google_analytics_tracking_code(*web_property_ids) ⇒ String

Asynchronous Google Analytics Tracking Code. Only added in production when the user is not signed into the CMS.

google_analytics_tracking_code('ABC132')

To track multiple accounts use the following

google_analytics_tracking_code('ABC132', 'DEF546')

Parameters:

  • web_property_ids (Array, String)

    Web property ID(s)

Returns:

  • (String)

    GA embed code



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/helpers/pages_helper.rb', line 90

def google_analytics_tracking_code(*web_property_ids)
  if web_property_ids.first.present? && add_tracking_code?
    analytics_que = "['_setAccount', '#{web_property_ids.shift}'], ['_trackPageview'], ['_trackPageLoadTime']"
    web_property_ids.each_with_index do |web_property_id, index|
       = (index + 98).chr
      analytics_que << ", ['#{}._setAccount', '#{web_property_id}'], ['#{}._trackPageview'], ['#{}._trackPageLoadTime']"
    end

    javascript_tag <<-JAVASCRIPT
var _gaq = _gaq || [];
_gaq.push(#{analytics_que});
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
    JAVASCRIPT
  end
end

#if_t(key, options = {}) ⇒ String

Translates the key and return an empty string if there is no translation.

Parameters:

  • key (String)

    Translation key

  • options (Hash) (defaults to: {})

Returns:

  • (String)

    Translated key



74
75
76
# File 'app/helpers/pages_helper.rb', line 74

def if_t(key, options = {})
  translate(key, options) || ''
end

#markdown(text) ⇒ String

Converts markdown to html.

Parameters:

  • text (String)

Returns:

  • (String)


45
46
47
48
49
50
# File 'app/helpers/pages_helper.rb', line 45

def markdown(text)
  return if text.blank?
  renderer = Redcarpet::Render::HTML.new(hard_wrap: true)
  parser = Redcarpet::Markdown.new(renderer)
  parser.render(text).html_safe
end

#textilize(text) ⇒ String

Returns the text with all the Textile codes turned into HTML tags.

Parameters:

  • text (String)

    Text to be textilized

Returns:

  • (String)

    Textilized string



32
33
34
35
36
37
38
# File 'app/helpers/pages_helper.rb', line 32

def textilize(text)
  unless text.blank?
    red_cloth = RedCloth.new(text)
    red_cloth.no_span_caps = true
    red_cloth.to_html.html_safe
  end
end