Class: Decidim::ContentRenderers::ProposalRenderer

Inherits:
BaseRenderer
  • Object
show all
Defined in:
lib/decidim/content_renderers/proposal_renderer.rb

Overview

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 Also:

  • Examples of how to use a content renderer

Constant Summary collapse

GLOBAL_ID_REGEX =

Matches a global id representing a Decidim::User

%r{gid://([\w-]*/Decidim::Proposals::Proposal/(\d+))}i.freeze

Instance Method Summary collapse

Instance Method Details

#render(_options = nil) ⇒ String

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.

Returns:

  • (String)

    the content ready to display (contains HTML)



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/decidim/content_renderers/proposal_renderer.rb', line 20

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