Module: ViewHelpers

Defined in:
app/helpers/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#erb_sanitized(value) ⇒ Object



19
20
21
22
23
24
25
# File 'app/helpers/view_helpers.rb', line 19

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

Parameters:

  • the (SocialLinker::Subject)

    SocialLinker::Subject initialized as complete as possible

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

    with site-defaults for ‘:site_title_postfix`, (e.g. article title - title postfix here), `:domain` (the main url). These options are overridden by the subject if set by the subject.

Returns:

  • String of tags (possibly marked as sanitized when available)



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/view_helpers.rb', line 32

def header_meta_tags subject, options={}
  options = options.merge(subject.options) if subject

  site_title_postfix = options[:site_title_postfix]
  site_name = options[:site_name] || site_title_postfix
  site_title_postfix = nil if options[:render_site_title_postfix] == false
  language = options[:language]
  domain = options[:domain]

  header_html = []

  header_html << meta_tag("twitter:site", options[:twitter_username])
  header_html << meta_tag("twitter:creator", options[:twitter_username])
  header_html << meta_tag("twitter:domain", domain)
  header_html << meta_tag("Content-Language", language)
  header_html << meta_tag("dc.language", language)
  header_html << meta_tag("og:locale", language)
  header_html << meta_tag("fb:app_id", options[:facebook_app_id])

  if subject
    header_html << meta_tag("twitter:card", subject.media ? :summary_large_image : :summary)

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

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

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


    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>#{erb_sanitized(site_title)}</title>"
  header_html << meta_tag("twitter:title", title)
  header_html << meta_tag("og:title", title)
  header_html << meta_tag("og:site_name", site_name)

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

  # we trust the html because all user input is sanitized by erb_sanitized
  header_html = header_html.html_safe if header_html.methods.include?(:html_safe)
  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)



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

def meta_tag(name, content)
  content = erb_sanitized(content)
  name = erb_sanitized(name)
  name_or_property_attribute = if (name.start_with?("og:") or name.start_with?("fb:"))
      "property"
    elsif ['Content-Language'].include? name
      "http-equiv"
    else
      "name"
  end
  "<meta #{name_or_property_attribute}=\"#{name}\" content=\"#{content}\" />" if content and content.to_s.strip != ""
end

Generates the <a href> code for the subject and network By default it will use the #social_link_to_image - function, refer to that function if you don’t see the icons rendered.

Options: params [Hash] options:

* :social_icons_image_path (defaults to the default SocialLinker iconset)
* :title (the title attribute, defaults to the network's name capitalized)
* :target_blank (boolean whether it should open in a new window)
* :class (array or string of classes to add to the a-href element)

Parameters:

  • the (SocialLinker::Subject)

    SocialLinker::Subject initialized as complete as possible

  • network (Symbol)

    key (e.g. twitter, facebook, see README and/or SocialLinker::Subject::SHARE_TEMPLATES )

Returns:

  • String of html (possibly marked as sanitized when available)

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/view_helpers.rb', line 121

def social_link_to subject, network, options = {}
  raise ArgumentError, "subject can't be nil" unless subject
  raise ArgumentError, "network can't be nil" unless network
  options_with_defaults = {
    social_icons_asset_path: 'social_linker/icons.svg',
    title: network.to_s.capitalize,
    target_blank: true,
  }.merge(options)

  link_content = network

  if block_given?
    link_content = capture{ yield }
  else
    social_icons_asset_path = options_with_defaults[:social_icons_asset_path]
    social_icons_asset_path = asset_path(social_icons_asset_path) if self.methods.include?(:image_path)

    link_content = social_link_to_image(network, social_icons_asset_path)
  end

  title = options_with_defaults[:title]
  html_class = [options_with_defaults[:class], network].flatten.compact.join(" ")
  targetblank = options_with_defaults[:target_blank] ? " target=\"_blank\"" : ""
  html = "<a href=\"#{erb_sanitized(subject.share_link(network))}\"#{targetblank} class=\"#{html_class}\" title=\"#{title}\">#{link_content}</a>"
  html = html.html_safe if html.methods.include?(:html_safe)
  html
end

Generates the <SVG> code for the image It references the parent image path with ‘xhref`. Make sure your browser supports this, or use something like `svg4everyone` to fix your client’s browsers Options:

  • social_icons_image_path (defaults to the default SocialLinker iconset)

  • title (the title attribute, defaults to the network’s name capitalized)



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

def social_link_to_image(network, image_path=nil)
  if image_path == nil
    image_path = asset_path("social_linker/icons.svg") if self.methods.include?(:image_path)
  end

  if network and image_path
    html = "<svg class=\"icon icon-#{network} icon-default-style\"><title>#{network.capitalize}</title><use xlink:href=\"#{image_path}#icon-#{network}\"></use></svg>"
    # html = html.html_safe if html.methods.include?(:html_safe)
    html
  end
end