Sha256: 43a1b4167cf9cf7db2025defe1224aefdd39ff4b9d4ba6096021e27c914c34bb

Contents?: true

Size: 1.36 KB

Versions: 39

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Alchemy
  # Handles Legacy page redirects
  #
  # If the page could not be found via its urlname we try to find
  # a legacy page url for requested url to redirect to.
  #
  module LegacyPageRedirects
    extend ActiveSupport::Concern

    included do
      before_action :redirect_to_legacy_url,
        if: :redirect_to_legacy_url?,
        only: [:show]
    end

    private

    def redirect_to_legacy_url
      redirect_permanently_to legacy_page_redirect_url
    end

    def redirect_to_legacy_url?
      (@page.nil? || request.format.nil?) && last_legacy_url
    end

    # Use the bare minimum to redirect to legacy page
    #
    # Don't use query string of legacy urlname.
    # This drops the given query string.
    #
    def legacy_page_redirect_url
      page = last_legacy_url.page
      return unless page

      alchemy.show_page_path(
        locale: prefix_locale? ? page.language_code : nil,
        urlname: page.urlname
      )
    end

    def legacy_urls
      # /slug/tree => slug/tree
      urlname = (request.fullpath[1..-1] if request.fullpath[0] == '/') || request.fullpath
      LegacyPageUrl.joins(:page).where(
        urlname: urlname,
        Page.table_name => {
          language_id: Language.current.id
        }
      )
    end

    def last_legacy_url
      @_last_legacy_url ||= legacy_urls.last
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
alchemy_cms-4.6.7 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.6.6 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.6.5 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.5.1 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.4.5 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.6.4 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.0.6 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.6.3 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.6.2 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.6.1 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.6.0 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.5.0 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.4.4 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.4.3 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.4.2 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.4.1 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.4.0 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.3.2 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.2.4 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-4.3.1 app/controllers/concerns/alchemy/legacy_page_redirects.rb