Sha256: 52bef256666b0d5c7203a1e0c67651f08ee5f6aa8b5f0e955326ebd77804a78c

Contents?: true

Size: 1.35 KB

Versions: 31

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Decidim
  # A custom implementation for HighVoltage so we can correctly find which page
  # to render. We need this because we allow rendering pages with content from
  # the database (with Decidim::Page) but also fallback to a template if it
  # exists.
  class PageFinder < HighVoltage::PageFinder
    DECIDIM_PAGE_TEMPLATE = "decidim_page"

    # Initializes the finder.
    #
    # page_id - A String with the id or slug of the page to render.
    # organization - A Decidim::Organization to look for pages in.
    def initialize(page_id, organization)
      @page_id = page_id
      @organization = organization
    end

    # Finds a Decidim::Page by slug.
    #
    # Returns a Decidim::Page or nil.
    def page
      @page ||= organization.static_pages.where(slug: page_id).first
    end

    private

    attr_reader :organization

    # Overwrite HighVoltage::PageFinder method to return a specific template
    # when we need to render a Decidim::Page.
    #
    # Returns a String.
    def clean_path
      return super unless page.present?

      DECIDIM_PAGE_TEMPLATE
    end

    # Overwrite HighVoltage::PageFinder method to not allow rendering the
    # template used to render Decidim::Pages without a page.
    #
    # Returns a Boolean.
    def invalid_page_id?(id)
      super || id == DECIDIM_PAGE_TEMPLATE
    end
  end
end

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
decidim-core-0.6.8 lib/decidim/page_finder.rb
decidim-0.6.8 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.6.7 lib/decidim/page_finder.rb
decidim-0.6.7 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.6.6 lib/decidim/page_finder.rb
decidim-0.6.6 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.6.5 lib/decidim/page_finder.rb
decidim-0.6.5 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.6.4 lib/decidim/page_finder.rb
decidim-0.6.4 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.6.3 lib/decidim/page_finder.rb
decidim-0.6.3 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.6.2 lib/decidim/page_finder.rb
decidim-0.6.2 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.6.1 lib/decidim/page_finder.rb
decidim-0.6.1 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.6.0 lib/decidim/page_finder.rb
decidim-0.6.0 decidim-core/lib/decidim/page_finder.rb
decidim-core-0.5.1 lib/decidim/page_finder.rb
decidim-0.5.1 decidim-core/lib/decidim/page_finder.rb