Class: Decidim::ContentRenderers::UserGroupRenderer

Inherits:
BaseRenderer
  • Object
show all
Defined in:
lib/decidim/content_renderers/user_group_renderer.rb

Overview

A renderer that searches Global IDs representing users in content and replaces it with a link to their profile with the nickname.

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

Constant Summary collapse

GLOBAL_ID_REGEX =

Matches a global id representing a Decidim::UserGroup

%r{gid://\S+/Decidim::UserGroup/\d+}.freeze

Instance Attribute Summary

Attributes inherited from BaseRenderer

#content

Instance Method Summary collapse

Methods inherited from BaseRenderer

#initialize

Constructor Details

This class inherits a constructor from Decidim::ContentRenderers::BaseRenderer

Instance Method Details

#render(_options = nil) ⇒ String

Replaces found Global IDs matching an existing user with a link to their profile. The Global IDs representing an invalid Decidim::UserGroup are replaced with an empty string.

Returns:

  • (String)

    the content ready to display (contains HTML)



20
21
22
23
24
25
26
27
28
29
# File 'lib/decidim/content_renderers/user_group_renderer.rb', line 20

def render(_options = nil)
  return content unless content.respond_to?(:gsub)

  content.gsub(GLOBAL_ID_REGEX) do |user_gid|
    user = GlobalID::Locator.locate(user_gid)
    Decidim::UserGroupPresenter.new(user).display_mention
  rescue ActiveRecord::RecordNotFound => _e
    ""
  end
end