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

Instance Method Summary collapse

Instance Attribute Details

#decidim_meta_descriptionObject (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
  @decidim_meta_description
end

#decidim_meta_image_urlObject (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
  @decidim_meta_image_url
end

#decidim_meta_twitter_handlerObject (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
  @decidim_meta_twitter_handler
end

#decidim_meta_urlObject (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
  @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.

Parameters:

  • path (String)
    • A String containing the path (e.g. “/proposals/1”).

Returns:

  • (String)
    • A String with the base URL and path, or the original path if it already includes 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.

Parameters:

  • description (String)
    • The String to be set as description.

Returns:

  • (nil)


86
87
88
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 86

def add_decidim_meta_description(description)
  @decidim_meta_description ||= strip_tags(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.

Parameters:

  • image_url (String)
    • The String to be set as image URL.

Returns:

  • (nil)


125
126
127
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 125

def add_decidim_meta_image_url(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.

Parameters:

  • tags (Hash)
    • A Hash containing the meta tag name as keys and its content as values.

  • resource (Object, nil)
    • The resource object that may contain the image.

Returns:

  • (nil)


14
15
16
17
18
19
20
21
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 14

def add_decidim_meta_tags(tags)
  add_decidim_page_title(tags[:title])
  add_decidim_meta_description(tags[:description])
  add_decidim_meta_url(tags[:url])
  add_decidim_meta_twitter_handler(tags[:twitter_handler])
  image_url = tags[:image_url].presence || resolve_meta_image_url(tags[:resource])
  add_decidim_meta_image_url(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.

Parameters:

  • twitter_handler (String)
    • The String to be set as Twitter handler.

Returns:

  • (nil)


99
100
101
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 99

def add_decidim_meta_twitter_handler(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.

Parameters:

  • url (String)
    • The String to be set as URL.

Returns:

  • (nil)


112
113
114
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 112

def add_decidim_meta_url(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).

Examples:

add_decidim_page_title("My Process")
add_decidim_page_title("My Organization")
decidim_page_title # => "My Process - My Organization"

Parameters:

  • title (String)
    • A String to be added to the title

Returns:

  • (Array<String>)


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_titleString

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 ‘“ - ”`.

Returns:

  • (String)
    • The concatenated title.



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_urlString

Resolves the base URL (example: www.decidim.org) without URL parameters.

Returns:

  • (String)
    • A String of the base URL.



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.

Parameters:

  • resource (Object, nil)
    • The resource object that may contain the image.

Returns:

  • (String, nil)
    • The resolved image URL, or nil if no appropriate image URL is found.



136
137
138
139
# File 'decidim-core/app/helpers/decidim/meta_tags_helper.rb', line 136

def resolve_meta_image_url(resource)
  url = MetaImageUrlResolver.new(resource, current_organization).resolve
  add_base_url_to(url) if url.present?
end