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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 49 additions & 14 deletions app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ 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]

# Legacy page redirects need to run after the page was loaded and before we render 404.
include LegacyPageRedirects

# From here on, we need a +@page+ to work with!
before_action :page_not_found!, if: -> { @page.blank? }, only: [:index, :show]
# From here on, we need a published +@page+ to work with!
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 +@page+ set.
include PageRedirects
# Page redirects need to run after the page was loaded and we're sure to have a public +@page+ set.
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,13 +85,25 @@ 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.
#
# If the root page is not public it redirects to the first published child.
# This can be configured via +redirect_to_public_child+ [default: true]
#
# If no index page and no admin users are present we show the "Welcome to Alchemy" page.
#
def load_index_page
Expand All @@ -116,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.

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

This file was deleted.

6 changes: 0 additions & 6 deletions config/alchemy/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
#
auto_logout_time: 30

# === Redirect Options
#
# redirect_to_public_child [Boolean] # Alchemy redirects to the first public child page found, if a page is not public.
#
redirect_to_public_child: true

# === Page caching
#
# Enable/Disable page caching globally.
Expand Down
4 changes: 1 addition & 3 deletions lib/alchemy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def show
# a value of nil means there is no new default
# any not nil value is the new default
def deprecated_configs
{
redirect_to_public_child: nil,
}
{}
end

private
Expand Down
76 changes: 12 additions & 64 deletions spec/controllers/alchemy/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,76 +62,24 @@ module Alchemy
default_language_root.update!(public_on: nil)
end

context "and redirect_to_public_child is set to false" do
before do
stub_alchemy_config(:redirect_to_public_child, false)
end

it "raises routing error (404)" do
expect {
get :index
}.to raise_error(ActionController::RoutingError)
end

context "when a page layout callback is set" do
before do
ApplicationController.extend Alchemy::OnPageLayout
ApplicationController.class_eval do
on_page_layout("index") { "do something" }
end
end

it 'raises routing error (404) and no "undefined method for nil" error' do
expect {
get :index
}.to raise_error(ActionController::RoutingError)
end
end
it "raises routing error (404)" do
expect {
get :index
}.to raise_error(ActionController::RoutingError)
end

context "and redirect_to_public_child is set to true" do
context "when a page layout callback is set" do
before do
stub_alchemy_config(:redirect_to_public_child, true)
end

context "that has a public child" do
let!(:public_child) do
create(:alchemy_page, :public, parent: default_language_root)
end

it "loads this page" do
get :index
expect(assigns(:page)).to eq(public_child)
ApplicationController.extend Alchemy::OnPageLayout
ApplicationController.class_eval do
on_page_layout("index") { "do something" }
end
end

context "that has a non public child" do
let!(:non_public_child) do
create(:alchemy_page, parent: default_language_root)
end

context "that has a public child" do
let!(:public_child) do
create(:alchemy_page, :public, parent: non_public_child)
end

it "loads this page" do
get :index
expect(assigns(:page)).to eq(public_child)
end
end

context "that has a non public child" do
before do
create(:alchemy_page, parent: non_public_child)
end

it "raises routing error (404)" do
expect {
get :index
}.to raise_error(ActionController::RoutingError)
end
end
it 'raises routing error (404) and no "undefined method for nil" error' do
expect {
get :index
}.to raise_error(ActionController::RoutingError)
end
end
end
Expand Down
Loading