Sha256: 3535ccf89f756d1ac9332a58792d5385d183fa994b2a57e168c6d50d52e8858a

Contents?: true

Size: 1.36 KB

Versions: 12

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..] if request.fullpath[0] == "/") || request.fullpath
      LegacyPageUrl.joins(:page).where(
        :urlname => urlname,
        Page.table_name => {
          language_id: Current.language.id
        }
      )
    end

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
alchemy_cms-7.3.4 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.3.3 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.3.2 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.2.7 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.3.1 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.3.0 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.2.6 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.2.5 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.2.4 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.2.3 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.2.2 app/controllers/concerns/alchemy/legacy_page_redirects.rb
alchemy_cms-7.2.1 app/controllers/concerns/alchemy/legacy_page_redirects.rb