Class: Decidim::ContentRenderers::ParagraphRenderer
- Inherits:
-
BaseRenderer
- Object
- BaseRenderer
- Decidim::ContentRenderers::ParagraphRenderer
- Defined in:
- lib/decidim/content_renderers/paragraph_renderer.rb
Overview
A renderer that searches Global IDs representing paragraphs in content and replaces it with a link to their show page.
e.g. gid://<APP_NAME>/Decidim::EnhancedTextwork::Paragraph/1
Constant Summary collapse
- GLOBAL_ID_REGEX =
Matches a global id representing a Decidim::User
%r{gid://([\w-]*/Decidim::EnhancedTextwork::Paragraph/(\d+))}i.freeze
Instance Method Summary collapse
-
#render(_options = nil) ⇒ String
Replaces found Global IDs matching an existing paragraph with a link to its show page.
Instance Method Details
#render(_options = nil) ⇒ String
Replaces found Global IDs matching an existing paragraph with a link to its show page. The Global IDs representing an invalid Decidim::EnhancedTextwork::Paragraph are replaced with '???' string.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/decidim/content_renderers/paragraph_renderer.rb', line 20 def render( = nil) return content unless content.respond_to?(:gsub) content.gsub(GLOBAL_ID_REGEX) do |paragraph_gid| paragraph = GlobalID::Locator.locate(paragraph_gid) Decidim::EnhancedTextwork::ParagraphPresenter.new(paragraph).display_mention rescue ActiveRecord::RecordNotFound paragraph_id = paragraph_gid.split("/").last "~#{paragraph_id}" end end |