Sha256: 778c48b95e8b9ba6980970dd64b3f0f56a3d752f2f4144d19a4ce2b67bce00ba

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Decidim
  module ContentRenderers
    # 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
    class UserRenderer < BaseRenderer
      # Matches a global id representing a Decidim::User
      GLOBAL_ID_REGEX = %r{gid://.*/Decidim::User/\d+}

      # 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)
      def render
        content.gsub(GLOBAL_ID_REGEX) do |user_gid|
          begin
            user = GlobalID::Locator.locate(user_gid)
            Decidim::UserPresenter.new(user).display_mention
          rescue ActiveRecord::RecordNotFound => _ex
            ""
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
decidim-core-0.9.1 lib/decidim/content_renderers/user_renderer.rb
decidim-core-0.9.0 lib/decidim/content_renderers/user_renderer.rb