Module: ViewHelpers
- Defined in:
- app/helpers/view_helpers.rb
Instance Method Summary collapse
- #erb_sanitized(value) ⇒ Object
-
#header_meta_tags(subject, options = {}) ⇒ Object
header_meta_tags renders the most important metatags based on the SocialLinker::Subject.
-
#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).
-
#social_link_to(subject, network, options = {}) ⇒ Object
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.
-
#social_link_to_image(network, image_path = nil) ⇒ Object
Generates the <SVG> code for the image It references the parent image path with ‘xhref`.
-
#tag_if(tagname, key_value_pairs, if_key = :content) ⇒ Object
renders a tag conditionally (if value is said) param [String, Symbol] tagname of the tag param [Hash] key value pairs (the attributes and their corresponding values param [String, Symbol] if_key is the key to be checked for containing a value, otherwise nil is returned, defaults to :content.
Instance Method Details
#erb_sanitized(value) ⇒ Object
39 40 41 42 43 44 45 |
# File 'app/helpers/view_helpers.rb', line 39 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
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/helpers/view_helpers.rb', line 52 def subject, ={} = .merge(subject.) if subject site_title_postfix = [:site_title_postfix] site_name = [:site_name] || site_title_postfix site_title_postfix = nil if [:render_site_title_postfix] == false language = [:language] domain = [:domain] header_html = [] # <link href="https://plus.google.com/+YourPage" rel="publisher"> # <meta itemprop="name" content="Content Title"> # <meta itemprop="description" content="Content description less than 200 characters"> # <meta itemprop="image" content="http://example.com/image.jpg"> # = header_html << ("twitter:site", [:twitter_username]) header_html << ("twitter:creator", [:twitter_username]) header_html << tag_if(:link, {href: "https://plus.google.com/+#{options[:google_plus_name]}", rel: "publisher"}, :href) if [:google_plus_name] header_html << ("twitter:domain", domain) header_html << ("Content-Language", language) header_html << ("dc.language", language) header_html << ("og:locale", language) header_html << ("fb:app_id", [:facebook_app_id]) if subject header_html << ("twitter:card", subject.media ? :summary_large_image : :summary) if subject.url header_html << ("og:url", subject.canonical_url) header_html << "<link rel=\"canonical\" href=\"#{erb_sanitized(subject.canonical_url)}\" />" end header_html << ("keywords", subject..join(" ")) header_html << ("description", subject.summary(false)) header_html << ("twitter:description", subject.summary(true)) header_html << ("og:description", subject.summary(false)) header_html << tag_if(:meta, {itemprop: :description, content: subject.summary(false)}) if subject.media header_html << ("twitter:image:src", subject.media) header_html << ("og:image", subject.media) header_html << ("og:image:width", subject.media_width) header_html << ("og:image:height", subject.media_height) header_html << ("og:image:type", subject.image_type) header_html << tag_if(:meta, {itemprop: :image, content: subject.media}) 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 << ("twitter:title", title) header_html << ("og:title", title) header_html << ("og:site_name", site_name) header_html << tag_if(:meta, {itemprop: :name, content: site_title}, :content) 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)
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/helpers/view_helpers.rb', line 7 def (name, content) key_value_pairs = {} 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 key_value_pairs[name_or_property_attribute] = name key_value_pairs[:content] = content tag_if(:meta, key_value_pairs, :content) end |
#social_link_to(subject, network, options = {}) ⇒ Object
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)
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/helpers/view_helpers.rb', line 153 def subject, network, = {} raise ArgumentError, "subject can't be nil" unless subject raise ArgumentError, "network can't be nil" unless network = { social_icons_asset_path: 'social_linker/icons.svg', title: network.to_s.capitalize, target_blank: true, }.merge() link_content = network if block_given? link_content = capture{ yield } else = [:social_icons_asset_path] = asset_path() if self.methods.include?(:image_path) link_content = (network, ) end title = [:title] html_class = [[:class], network].flatten.compact.join(" ") targetblank = [: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 |
#social_link_to_image(network, image_path = nil) ⇒ Object
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)
127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/helpers/view_helpers.rb', line 127 def (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 |
#tag_if(tagname, key_value_pairs, if_key = :content) ⇒ Object
renders a tag conditionally (if value is said) param [String, Symbol] tagname of the tag param [Hash] key value pairs (the attributes and their corresponding values param [String, Symbol] if_key is the key to be checked for containing a value, otherwise nil is returned, defaults to :content
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/helpers/view_helpers.rb', line 26 def tag_if(tagname, key_value_pairs, if_key=:content) tag = tagname.to_sym critical_value = key_value_pairs[if_key] if critical_value and critical_value.to_s.strip != "" attribs = key_value_pairs.collect do |k,v| key = erb_sanitized(k) value = erb_sanitized(v) rv = "#{key}=\"#{value}\"" end.join(" ") "<#{tag} #{attribs} />" end end |