class Decidim::ContentRenderers::HashtagRenderer

A renderer that searches Global IDs representing hashtags in content and replaces it with a link to their detail page with the name.

e.g. gid://<APP_NAME>/Decidim::Hashtag/1

@see BaseRenderer Examples of how to use a content renderer

Constants

GLOBAL_ID_REGEX

Matches a global id representing a Decidim::Hashtag

Public Instance Methods

extra_hashtags() click to toggle source

Returns all the extra hashtags found in the content

# File lib/decidim/content_renderers/hashtag_renderer.rb, line 43
def extra_hashtags
  @extra_hashtags ||= existing_hashtags.select { |hashtag| content_extra_hashtags_ids.member?(hashtag.id) }
end
render(links: true, extras: true) click to toggle source

Replaces found Global IDs matching an existing hashtag with a link to their detail page. The Global IDs representing an invalid Decidim::Hashtag are replaced with an empty string.

links - should render hashtags as links? extras - should include extra hashtags?

@return [String] the content ready to display (contains HTML)

# File lib/decidim/content_renderers/hashtag_renderer.rb, line 23
def render(links: true, extras: true)
  return content unless content.respond_to?(:gsub)

  content.gsub(GLOBAL_ID_REGEX) do |hashtag_gid|
    id, extra, cased_name = hashtag_gid.scan(GLOBAL_ID_REGEX).flatten
    hashtag = hashtags[id.to_i]

    next "" if hashtag.nil? || (!extras && extra.present?)

    presenter = Decidim::HashtagPresenter.new(hashtag, cased_name: cased_name)

    if links
      presenter.display_hashtag
    else
      presenter.display_hashtag_name
    end
  end
end

Private Instance Methods

content_extra_hashtags_ids() click to toggle source
# File lib/decidim/content_renderers/hashtag_renderer.rb, line 62
def content_extra_hashtags_ids
  @content_extra_hashtags_ids ||= ids_from_matches(content_matches.select { |match| match[1].present? })
end
content_hashtags_ids() click to toggle source
# File lib/decidim/content_renderers/hashtag_renderer.rb, line 58
def content_hashtags_ids
  @content_hashtags_ids ||= ids_from_matches(content_matches)
end
content_matches() click to toggle source
# File lib/decidim/content_renderers/hashtag_renderer.rb, line 66
def content_matches
  @content_matches ||= content.scan(GLOBAL_ID_REGEX)
end
existing_hashtags() click to toggle source
# File lib/decidim/content_renderers/hashtag_renderer.rb, line 54
def existing_hashtags
  @existing_hashtags ||= Decidim::Hashtag.where(id: content_hashtags_ids)
end
hashtags() click to toggle source
# File lib/decidim/content_renderers/hashtag_renderer.rb, line 49
def hashtags
  @hashtags ||=
    existing_hashtags.index_by(&:id)
end
ids_from_matches(matches) click to toggle source
# File lib/decidim/content_renderers/hashtag_renderer.rb, line 70
def ids_from_matches(matches)
  matches.map(&:first).map(&:to_i).uniq
end