Sha256: cc1151da39e2b3fabb6d565d067245c54cb35acf79b357b42fa35c1fa19adc46

Contents?: true

Size: 1.13 KB

Versions: 21

Compression:

Stored size: 1.13 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::UserGroup/1
    #
    # @see BaseRenderer Examples of how to use a content renderer
    class UserGroupRenderer < BaseRenderer
      # Matches a global id representing a Decidim::UserGroup
      GLOBAL_ID_REGEX = %r{gid://\S+/Decidim::UserGroup/\d+}.freeze

      # 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.
      #
      # @return [String] the content ready to display (contains HTML)
      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
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
decidim-core-0.25.2 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.25.1 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.25.0 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.25.0.rc4 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.25.0.rc3 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.25.0.rc2 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.25.0.rc1 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.24.3 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.23.6 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.24.2 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.23.5 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.24.1 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.24.0 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.24.0.rc2 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.23.4 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.24.0.rc1 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.23.3 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.23.2 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.23.1 lib/decidim/content_renderers/user_group_renderer.rb
decidim-core-0.23.1.rc1 lib/decidim/content_renderers/user_group_renderer.rb