Skip to content

Commit

Permalink
Remove redirect_to_public_child flag and feature
Browse files Browse the repository at this point in the history
This has been deprecated in 5.0 and is not supported anymore.
  • Loading branch information
tvdeyen committed Jul 26, 2020
1 parent f9b70e0 commit d01c892
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 184 deletions.
5 changes: 2 additions & 3 deletions app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def index
# descendant it finds. If no public page can be found it renders a 404 error.
#
def show
page_not_found! unless @page.public?

if redirect_url.present?
redirect_permanently_to redirect_url
else
Expand All @@ -88,9 +90,6 @@ def sitemap
#
# 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 Down
21 changes: 3 additions & 18 deletions app/controllers/concerns/alchemy/page_redirects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ module PageRedirects
#
# == Lookup:
#
# 1. If the page is not published and we have a published child,
# we return the url top that page. (Configurable through +redirect_to_public_child+).
# 2. If the page layout of the page found has a controller and action configured,
# we return the url to that route. (Configure controller and action in `page_layouts.yml`).
# 3. If the current page URL has no locale prefixed, but we should have one,
# 1. If the current page URL has no locale prefixed, but we should have one,
# we return the prefixed URL.
# 4. If no redirection is needed returns nil.
# 2. If no redirection is needed returns nil.
#
# @return String
# @return NilClass
#
def redirect_url
@_redirect_url ||= public_child_redirect_url || locale_prefixed_url || nil
@_redirect_url ||= locale_prefixed_url || nil
end

def locale_prefixed_url
Expand All @@ -36,17 +32,6 @@ def locale_prefixed_url
page_redirect_url(locale: Language.current.code)
end

def public_child_redirect_url
return if @page.public?

if configuration(:redirect_to_public_child)
@page = @page.descendants.published.not_restricted.first
@page ? page_redirect_url : page_not_found!
else
page_not_found!
end
end

# Page url with or without locale while keeping all additional params
def page_redirect_url(options = {})
options = {
Expand Down
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
93 changes: 3 additions & 90 deletions spec/features/page_redirects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
create(:alchemy_page, :public, name: "Page 1")
end

let(:public_child) do
create(:alchemy_page, :public, name: "Public Child", parent_id: public_page.id)
end

context "in multi language mode" do
let(:second_page) { create(:alchemy_page, :public, name: "Second Page") }

Expand Down Expand Up @@ -92,74 +88,12 @@
name: "Not Public",
urlname: "",
)
public_child
end

it "redirects to public child" do
visit "/not-public"
expect(page.current_path).to eq("/not-public/public-child")
end

context "with only unpublished pages in page tree" do
before do
public_child.update(public_on: nil)
end

it "should raise not found error" do
expect {
visit "/not-public"
}.to raise_error(ActionController::RoutingError)
end
end

context "if page locale is the default locale" do
it "redirects to public child with prefixed locale" do
allow(::I18n).to receive(:default_locale).and_return(:de)
it "should raise not found error" do
expect {
visit "/not-public"
expect(page.current_path).to eq("/en/not-public/public-child")
end
end
end

context "if requested url is the index url" do
context "and redirect_to_public_child is enabled" do
before do
allow(Alchemy::Config).to receive(:get) do |arg|
arg == :redirect_to_public_child ? true : Alchemy::Config.parameter(arg)
end
end

context "if index page is unpublished" do
let!(:public_child) do
create(:alchemy_page, :public, name: "Public Child", parent_id: default_language_root.id)
end

before do
default_language_root.update(
public_on: nil,
name: "Not Public",
urlname: "",
)
end

context "and index page locale is default locale" do
it "redirects to public child without prefixed locale" do
visit "/"
expect(page.current_path).to eq("/public-child")
end
end

context "and page locale is not default locale" do
before do
allow(::I18n).to receive(:default_locale).and_return(:de)
end

it "redirects to public child with prefixed locale" do
visit "/"
expect(page.current_path).to eq("/en/public-child")
end
end
end
}.to raise_error(ActionController::RoutingError)
end
end

Expand Down Expand Up @@ -240,27 +174,6 @@
expect(page.current_path).to eq("/#{public_page.urlname}")
end

context "redirects to public child" do
before do
public_page.update(
public_on: nil,
name: "Not Public",
urlname: "",
)
public_child
end

it "if requested page is unpublished" do
visit "/not-public"
expect(page.current_path).to eq("/not-public/public-child")
end

it "with normal url, if requested url has nested language code and is not public" do
visit "/en/not-public"
expect(page.current_path).to eq("/not-public/public-child")
end
end

context "if requested url is index url" do
context "when locale is prefixed" do
it "redirects to normal url" do
Expand Down

0 comments on commit d01c892

Please sign in to comment.