Sha256: 1a4060b3064c43c2936a28e824590795b9940e0b8206b0658b3bc5aac03d70bd

Contents?: true

Size: 1.2 KB

Versions: 19

Compression:

Stored size: 1.2 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

      # 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
        content.gsub(GLOBAL_ID_REGEX) do |proposal_gid|
          begin
            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
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-proposals-0.16.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.16.0 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.15.2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.15.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.15.0 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.14.4 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.14.3 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.14.2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.14.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.13.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.12.2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.13.0 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.12.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.13.0.pre1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.12.0 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.11.2 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.12.0.pre lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.11.1 lib/decidim/content_renderers/proposal_renderer.rb
decidim-proposals-0.11.0.pre1 lib/decidim/content_renderers/proposal_renderer.rb