class Decidim::ContentRenderers::UserRenderer

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::User/1

@see BaseRenderer Examples of how to use a content renderer

Constants

GLOBAL_ID_REGEX

Matches a global id representing a Decidim::User

Public Instance Methods

render(_options = nil) click to toggle source

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

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

# File lib/decidim/content_renderers/user_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::UserPresenter.new(user).display_mention
  rescue ActiveRecord::RecordNotFound => _e
    ""
  end
end