Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redirect_to_public_child flag and feature #1910

Merged
merged 2 commits into from Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 46 additions & 8 deletions app/controllers/alchemy/pages_controller.rb
Expand Up @@ -13,7 +13,10 @@ class PagesController < Alchemy::BaseController

# Redirecting concerns. Order is important here!
include SiteRedirects
include LocaleRedirects

before_action :enforce_no_locale,
if: :locale_prefix_not_allowed?,
only: [:index, :show]

before_action :load_index_page, only: [:index]
before_action :load_page, only: [:show]
Expand All @@ -25,7 +28,9 @@ class PagesController < Alchemy::BaseController
before_action :page_not_found!, unless: -> { @page&.public? }, only: [:index, :show]

# Page redirects need to run after the page was loaded and we're sure to have a public +@page+ set.
include PageRedirects
before_action :enforce_locale,
if: :locale_prefix_missing?,
only: [:index, :show]

# We only need to set the +@root_page+ if we are sure that no more redirects happen.
before_action :set_root_page, only: [:index, :show]
Expand Down Expand Up @@ -66,12 +71,8 @@ def index
# descendant it finds. If no public page can be found it renders a 404 error.
#
def show
if redirect_url.present?
redirect_permanently_to redirect_url
else
authorize! :show, @page
render_page if render_fresh_page?
end
authorize! :show, @page
render_page if render_fresh_page?
end

# Renders a search engine compatible xml sitemap.
Expand All @@ -84,6 +85,21 @@ def sitemap

private

# Redirects to requested action without locale prefixed
def enforce_no_locale
redirect_permanently_to additional_params.merge(locale: nil)
end

# Is the requested locale allowed?
#
# If Alchemy is not in multi language mode or the requested locale is the default locale,
# then we want to redirect to a non prefixed url.
#
def locale_prefix_not_allowed?
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
params[:locale].present? && !multi_language? ||
params[:locale].presence == ::I18n.default_locale.to_s
end

# == Loads index page
#
# Loads the current public language root page.
Expand Down Expand Up @@ -113,6 +129,28 @@ def load_page
)
end

def enforce_locale
redirect_permanently_to page_locale_redirect_url(locale: Language.current.code)
end

def locale_prefix_missing?
multi_language? && params[:locale].blank? && !default_locale?
end

def default_locale?
Language.current.code.to_sym == ::I18n.default_locale.to_sym
end

# Page url with or without locale while keeping all additional params
def page_locale_redirect_url(options = {})
options = {
locale: prefix_locale? ? @page.language_code : nil,
urlname: @page.urlname,
}.merge(options)

alchemy.show_page_path additional_params.merge(options)
end

# Redirects to given url with 301 status
def redirect_permanently_to(url)
redirect_to url, status: :moved_permanently
Expand Down
40 changes: 0 additions & 40 deletions app/controllers/concerns/alchemy/locale_redirects.rb

This file was deleted.

53 changes: 0 additions & 53 deletions app/controllers/concerns/alchemy/page_redirects.rb

This file was deleted.