Module: SocialLinker::ViewHelpers

Defined in:
lib/social_linker/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#erb_sanitized(value) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/social_linker/view_helpers.rb', line 12

def erb_sanitized(value)
  if defined? Rails
    h(value)
  else
    ERB::Util.h(value)
  end
end

#header_meta_tags(subject, options = {}) ⇒ Object

header_meta_tags renders the most important metatags based on the SocialLinker::Subject param [SocialLinker::Subject] the SocialLinker::Subject initialized as complete as possible param [Hash] options with site-defaults for ‘:site_title_postfix`, (e.g. article title - title postfix here), `:domain` (the main url), `



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/social_linker/view_helpers.rb', line 23

def header_meta_tags subject, options={}
  site_title_postfix = options[:site_title_postfix]
  header_html = []
  if subject
    domain = options[:domain] || subject.options[:domain]

    header_html << meta_tag("twitter:card", subject.media ? :summary_large_image : :summary)
    header_html << meta_tag("twitter:site", subject.options[:twitter_username])
    header_html << meta_tag("twitter:creator", subject.options[:twitter_username])
    header_html << meta_tag("twitter:domain", domain)

    if subject.url
      header_html << meta_tag("og:url", subject.canonical_url)
      header_html << "<link rel=\"canonical\" content=\"#{erb_sanitized(subject.canonical_url)}\" />"
    end

    header_html << meta_tag("keywords", subject.tags.join(" "))
    header_html << meta_tag("description", subject.summary)

    header_html << meta_tag("twitter:description", subject.summary)
    header_html << meta_tag("og:description", subject.summary)

    if subject.media
      header_html << meta_tag("twitter:image:src", subject.media)
      header_html << meta_tag("og:image", subject.media)
      header_html << meta_tag("og:image:type", subject.options[:image_type])
    end
  end

  title = @title
  title = subject.title if subject
  site_title = [title, site_title_postfix].uniq.compact.join(" - ")
  header_html << "<title>#{site_title}</title>"
  header_html << meta_tag("twitter:title", title)
  header_html << meta_tag("og:title", title)

  header_html.compact!
  header_html.join("\n") if header_html

end

#meta_tag(name, content) ⇒ Object

renders a metatag param [String, Symbol] name (or property) (defaults to name, values starting with ‘og:’ (opengraph) will be using the property attribute) param [String, Symbol] content (the value for the name or the property)



7
8
9
10
# File 'lib/social_linker/view_helpers.rb', line 7

def meta_tag(name, content)
  name_or_property_section = name.start_with?("og:") ? "property=\"#{erb_sanitized(name)}\"" : "name=\"#{erb_sanitized(name)}\""
  "<meta #{name_or_property_section} content=\"#{erb_sanitized(content)}\" />" if content and content != ""
end