Sha256: 3dddfbab86e8f19c31b6ddab560ca0b418a53262767d16b8d4b2ae4715202d15

Contents?: true

Size: 1.23 KB

Versions: 17

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Decidim
  module ContentRenderers
    # A renderer that searches Global IDs representing proposals in content
    # and replaces it with a link to their show page.
    #
    # e.g. gid://<APP_NAME>/Decidim::Proposals::Proposal/1
    #
    # @see BaseRenderer Examples of how to use a content renderer
    class ProposalRenderer < BaseRenderer
      # Matches a global id representing a Decidim::User
      GLOBAL_ID_REGEX = %r{gid://([\w-]*/Decidim::Proposals::Proposal/(\d+))}i.freeze

      # Replaces found Global IDs matching an existing proposal with
      # a link to its show page. The Global IDs representing an
      # invalid Decidim::Proposals::Proposal are replaced with '???' 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 |proposal_gid|
          proposal = GlobalID::Locator.locate(proposal_gid)
          Decidim::Proposals::ProposalPresenter.new(proposal).display_mention
        rescue ActiveRecord::RecordNotFound
          proposal_id = proposal_gid.split("/").last
          "~#{proposal_id}"
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
decidim-proposals-0.26.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.26.0 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.26.0.rc2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.26.0.rc1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.25.2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.25.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.25.0 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.25.0.rc4 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.25.0.rc3 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.25.0.rc2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.25.0.rc1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.24.3 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.24.2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.24.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.24.0 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.24.0.rc2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.24.0.rc1 lib/decidim/content_renderers/proposal_renderer.rb