Module: Decidim::MetaTagsHelper
- Included in:
- Admin::ApplicationHelper
- Defined in:
- decidim-core/app/helpers/decidim/meta_tags_helper.rb
Overview
Helper that provides convenient methods to deal with the page meta tags.
Instance Attribute Summary collapse
-
#decidim_meta_description ⇒ Object
readonly
Returns the value of attribute decidim_meta_description.
-
#decidim_meta_image_url ⇒ Object
readonly
Returns the value of attribute decidim_meta_image_url.
-
#decidim_meta_twitter_handler ⇒ Object
readonly
Returns the value of attribute decidim_meta_twitter_handler.
-
#decidim_meta_url ⇒ Object
readonly
Returns the value of attribute decidim_meta_url.
Instance Method Summary collapse
-
#add_base_url_to(path) ⇒ String
Adds base URL to the given path if it does not include a host.
-
#add_decidim_meta_description(description) ⇒ nil
Sets the meta description for the current page.
-
#add_decidim_meta_image_url(image_url) ⇒ nil
Sets the meta image URL for the current page.
-
#add_decidim_meta_tags(tags) ⇒ nil
Sets the given metatags for the page.
-
#add_decidim_meta_twitter_handler(twitter_handler) ⇒ nil
Sets the meta Twitter handler for the current page.
-
#add_decidim_meta_url(url) ⇒ nil
Sets the meta URL for the current page.
-
#add_decidim_page_title(title) ⇒ Array<String>
Accumulates the given
titleso that they can be chained. -
#decidim_page_title ⇒ String
Renders the title for a page.
-
#resolve_base_url ⇒ String
Resolves the base URL (example: www.decidim.org) without URL parameters.
-
#resolve_meta_image_url(resource) ⇒ String?
Resolves the image URL to be used for meta tags.
Instance Attribute Details
#decidim_meta_description ⇒ Object (readonly)
Returns the value of attribute decidim_meta_description.
75 76 77 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 75 def @decidim_meta_description end |
#decidim_meta_image_url ⇒ Object (readonly)
Returns the value of attribute decidim_meta_image_url.
75 76 77 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 75 def @decidim_meta_image_url end |
#decidim_meta_twitter_handler ⇒ Object (readonly)
Returns the value of attribute decidim_meta_twitter_handler.
75 76 77 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 75 def @decidim_meta_twitter_handler end |
#decidim_meta_url ⇒ Object (readonly)
Returns the value of attribute decidim_meta_url.
75 76 77 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 75 def @decidim_meta_url end |
Instance Method Details
#add_base_url_to(path) ⇒ String
Adds base URL to the given path if it does not include a host.
28 29 30 31 32 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 28 def add_base_url_to(path) return path if path.blank? || URI.parse(path).host.present? "#{resolve_base_url}#{path}" end |
#add_decidim_meta_description(description) ⇒ nil
Sets the meta description for the current page. We want to keep the most specific one, so you cannot replace the description if it is set by a view that has already been rendered. Remember that Rails’s views are render inside-out, so the layout is the last one to be rendered. You can put there a basic content and override it in other layers.
86 87 88 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 86 def (description) @decidim_meta_description ||= (description) end |
#add_decidim_meta_image_url(image_url) ⇒ nil
Sets the meta image URL for the current page. We want to keep the most specific one, so you cannot replace the image URL if it is set by a view that has already been rendered. Remember that Rails’s views are render inside-out, so the layout is the last one to be rendered. You can put there a basic content and override it in other layers.
125 126 127 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 125 def (image_url) @decidim_meta_image_url ||= image_url end |
#add_decidim_meta_tags(tags) ⇒ nil
Sets the given metatags for the page. It is a wrapper for the individual methods, so that you can set multiple values with a single call. See the docs for the other methods to see how they work.
14 15 16 17 18 19 20 21 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 14 def () add_decidim_page_title([:title]) ([:description]) ([:url]) ([:twitter_handler]) image_url = [:image_url].presence || ([:resource]) (add_base_url_to(image_url)) if image_url.present? end |
#add_decidim_meta_twitter_handler(twitter_handler) ⇒ nil
Sets the meta Twitter handler for the current page. We want to keep the most specific one, so you cannot replace the Twitter handler if it is set by a view that has already been rendered. Remember that Rails’s views are render inside-out, so the layout is the last one to be rendered. You can put there a basic content and override it in other layers.
99 100 101 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 99 def (twitter_handler) @decidim_meta_twitter_handler ||= twitter_handler end |
#add_decidim_meta_url(url) ⇒ nil
Sets the meta URL for the current page. We want to keep the most specific one, so you cannot replace the URL if it is set by a view that has already been rendered. Remember that Rails’s views are render inside-out, so the layout is the last one to be rendered. You can put there a basic content and override it in other layers.
112 113 114 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 112 def (url) @decidim_meta_url ||= url end |
#add_decidim_page_title(title) ⇒ Array<String>
Accumulates the given title so that they can be chained. Since Rails views are rendered inside-out, title is appended to an array. This way the beginning of the title will be the most specific one. Use the decidim_page_title method to render the title whenever you need to (most surely, in the ‘<title>` tag in the HTML head and in some title metatags).
60 61 62 63 64 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 60 def add_decidim_page_title(title) @decidim_page_title ||= [] @decidim_page_title << title if title.present? @decidim_page_title end |
#decidim_page_title ⇒ String
Renders the title for a page. Use the add_decidim_page_title method to accumulate elements for the title. Basically, it joins the elements of the title array with ‘“ - ”`.
71 72 73 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 71 def decidim_page_title (@decidim_page_title || []).join(" - ") end |
#resolve_base_url ⇒ String
Resolves the base URL (example: www.decidim.org) without URL parameters.
37 38 39 40 41 42 43 44 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 37 def resolve_base_url return request.base_url if respond_to?(:request) && request&.base_url.present? uri = URI.parse(decidim.root_url(host: current_organization.host)) port = uri.port.present? && [80, 443].exclude?(uri.port) ? ":#{uri.port}" : "" "#{uri.scheme}://#{uri.host}#{port}" end |
#resolve_meta_image_url(resource) ⇒ String?
Resolves the image URL to be used for meta tags. This method creates a new instance of MetaImageUrlResolver, which handles the logic for determining the most appropriate image URL.
136 137 138 139 |
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 136 def (resource) url = MetaImageUrlResolver.new(resource, current_organization).resolve add_base_url_to(url) if url.present? end |