Class: Decidim::ContentRenderers::PlanRenderer

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

Overview

A renderer that searches Global IDs representing plans in content and replaces it with a link to their show page.

e.g. gid://<APP_NAME>/Decidim::Plans::Plan/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::Plans::Plan\/(\d+))}i

Instance Method Summary collapse

Instance Method Details

#renderString

Replaces found Global IDs matching an existing plan with a link to its show page. The Global IDs representing an invalid Decidim::Plans::Plan 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/plan_renderer.rb', line 20

def render
  content.gsub(GLOBAL_ID_REGEX) do |plan_gid|
    begin
      plan = GlobalID::Locator.locate(plan_gid)
      Decidim::Plans::PlanPresenter.new(plan).display_mention
    rescue ActiveRecord::RecordNotFound
      plan_gid = plan_gid.split("/").last
      "##{plan_gid}"
    end
  end
end