Module: PagesCore::HeadTagsHelper

Included in:
ApplicationHelper, Frontend::PagesController
Defined in:
app/helpers/pages_core/head_tags_helper.rb

Instance Method Summary collapse

Instance Method Details

#document_title(*args) ⇒ Object Also known as: page_title

Sets a document title.

document_title "Dashboard"

7
8
9
10
11
12
13
14
15
16
# File 'app/helpers/pages_core/head_tags_helper.rb', line 7

def document_title(*args)
  if args.any?
    @document_title = args.first
  else
    safe_join(
      [@document_title, PagesCore.config(:site_name)].compact.uniq,
      " - "
    )
  end
end

#document_title?Boolean

Returns true if document title has been set.

Returns:

  • (Boolean)

20
21
22
# File 'app/helpers/pages_core/head_tags_helper.rb', line 20

def document_title?
  @document_title ? true : false
end

#feed_tags(options = {}) ⇒ Object

Generates links for all RSS feeds. Specify :include_hidden to also include hidden pages.

feed_tags
feed_tags include_hidden: true

30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/pages_core/head_tags_helper.rb', line 30

def feed_tags(options = {})
  feeds = Page.enabled_feeds(@locale, options)
  return unless feeds.any?
  feed_tags = [
    rss_link_tag(PagesCore.config(:site_name),
                 pages_url(@locale, format: :rss))
  ] + feeds.map do |page|
    rss_link_tag("#{PagesCore.config(:site_name)}: #{page.name}",
                 page_url(@locale, page, format: :rss))
  end
  safe_join(feed_tags, "\n")
end

#google_analytics_tags(account_id) ⇒ Object

Outputs Google Analytics tracking code.

google_analytics_tags "UA-12345678-1"

47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/pages_core/head_tags_helper.rb', line 47

def google_analytics_tags()
  javascript_tag(
    "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||" \
      "function(){\n (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new " \
      "Date();a=s.createElement(o),\nm=s.getElementsByTagName(o)[0];" \
      "a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n})(window," \
      "document,'script','//www.google-analytics.com/analytics.js','ga');" \
      "\n\nga('create', '#{}', 'auto');\nga('send', 'pageview');"
  )
end

#head_tag(&block) ⇒ Object

Outputs a HTML5 doctype and head tags, with document title and relevant meta tags. Takes a block which will be placed inside <head>.

<%= head_tag do %>
  <%= stylesheet_link_tag "application" %>
  <%= javascript_include_tag "application" %>
  <%= feed_tags %>
<% end %>

68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/pages_core/head_tags_helper.rb', line 68

def head_tag(&block)
  # The block output must be captured first
  block_output = block_given? ? capture(&block) : nil

  safe_join(
    [
      "<!doctype html>\n<html lang=\"#{I18n.locale}\">".html_safe,
      (:head) do
        safe_join(head_tag_contents(block_output), "\n")
      end
    ]
  )
end

Generates a link to an RSS feed.

rss_link_tag "My feed", "feed.rss"

86
87
88
89
90
91
92
93
94
# File 'app/helpers/pages_core/head_tags_helper.rb', line 86

def rss_link_tag(title, href)
  tag(
    :link,
    rel: "alternate",
    type: "application/rss+xml",
    title: title,
    href: href
  )
end

#typekit_tags(kit_id) ⇒ Object

Outputs Typekit tags.

typekit_tags "aadgrag"

100
101
102
103
104
105
# File 'app/helpers/pages_core/head_tags_helper.rb', line 100

def typekit_tags(kit_id)
  safe_join([
              javascript_include_tag("http://use.typekit.com/#{kit_id}.js"),
              javascript_tag("try{Typekit.load();}catch(e){}")
            ], "\n")
end