Class: Decidim::ContentRenderers::ResourceRenderer

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

Instance Attribute Summary

Attributes inherited from BaseRenderer

#content

Instance Method Summary collapse

Methods inherited from BaseRenderer

#initialize

Constructor Details

This class inherits a constructor from Decidim::ContentRenderers::BaseRenderer

Instance Method Details

#regexObject



25
26
27
# File 'lib/decidim/content_renderers/resource_renderer.rb', line 25

def regex
  raise "Not implemented"
end

#render(_options = nil) ⇒ String

Replaces found Global IDs matching an existing resource with a link to its show page. The Global IDs representing an invalid Resource are replaced with ‘???’ string.

Returns:

  • (String)

    the content ready to display (contains HTML)



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/decidim/content_renderers/resource_renderer.rb', line 13

def render(_options = nil)
  return content unless content.respond_to?(:gsub)

  content.gsub(regex) do |resource_gid|
    resource = GlobalID::Locator.locate(resource_gid)
    resource.presenter.display_mention
  rescue ActiveRecord::RecordNotFound
    resource_id = resource_gid.split("/").last
    "~#{resource_id}"
  end
end