Sha256: 7faca0cabb32a356b4f4220b1f456f5d34763af2bfc4fe052c4601c59f3d9149

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Decidim
  module ContentRenderers
    # 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 BaseRenderer Examples of how to use a content renderer
    class PlanRenderer < BaseRenderer
      # Matches a global id representing a Decidim::User
      GLOBAL_ID_REGEX = %r{gid:\/\/([\w-]*\/Decidim::Plans::Plan\/(\d+))}i

      # 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.
      #
      # @return [String] the content ready to display (contains HTML)
      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
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-plans-0.18.2 lib/decidim/content_renderers/plan_renderer.rb
decidim-plans-0.18.1 lib/decidim/content_renderers/plan_renderer.rb
decidim-plans-0.18.0 lib/decidim/content_renderers/plan_renderer.rb
decidim-plans-0.17.0 lib/decidim/content_renderers/plan_renderer.rb
decidim-plans-0.16.9 lib/decidim/content_renderers/plan_renderer.rb
decidim-plans-0.16.8 lib/decidim/content_renderers/plan_renderer.rb
decidim-plans-0.16.7 lib/decidim/content_renderers/plan_renderer.rb