diff --git a/app/controllers/alchemy/admin/languages_controller.rb b/app/controllers/alchemy/admin/languages_controller.rb
index 21e2d5646e..43c5566995 100644
--- a/app/controllers/alchemy/admin/languages_controller.rb
+++ b/app/controllers/alchemy/admin/languages_controller.rb
@@ -13,6 +13,11 @@ def new
page_layout: Config.get(:default_language)['page_layout']
)
end
+
+ def switch
+ set_alchemy_language(params[:language_id])
+ do_redirect_to request.referer || alchemy.admin_dashboard_path
+ end
end
end
end
diff --git a/app/controllers/alchemy/admin/pages_controller.rb b/app/controllers/alchemy/admin/pages_controller.rb
index 5ea228f956..202e02b266 100644
--- a/app/controllers/alchemy/admin/pages_controller.rb
+++ b/app/controllers/alchemy/admin/pages_controller.rb
@@ -26,7 +26,6 @@ class PagesController < Alchemy::Admin::BaseController
def index
authorize! :index, :alchemy_admin_pages
- @languages = Language.on_current_site
if !@page_root
@language = Language.current
@languages_with_page_tree = Language.on_current_site.with_root_page
@@ -219,11 +218,6 @@ def order
do_redirect_to admin_pages_path
end
- def switch_language
- set_alchemy_language(params[:language_id])
- do_redirect_to redirect_path_for_switch_language
- end
-
def flush
Language.current.pages.flushables.update_all(published_at: Time.current)
# We need to ensure, that also all layoutpages get the +published_at+ timestamp set,
@@ -341,14 +335,6 @@ def pages_from_raw_request
end
end
- def redirect_path_for_switch_language
- if request.referer && request.referer.include?('admin/layoutpages')
- admin_layoutpages_path
- else
- admin_pages_path
- end
- end
-
def redirect_path_after_create_page
if @page.redirects_to_external? || !@page.editable_by?(current_alchemy_user)
admin_pages_path
diff --git a/app/views/alchemy/admin/partials/_language_tree_select.html.erb b/app/views/alchemy/admin/partials/_language_tree_select.html.erb
index 13b1ae2b97..83e62f6160 100644
--- a/app/views/alchemy/admin/partials/_language_tree_select.html.erb
+++ b/app/views/alchemy/admin/partials/_language_tree_select.html.erb
@@ -1,9 +1,13 @@
-<% if can?(:switch_language, Alchemy::Page) && @languages.many? %>
+<% languages = Alchemy::Language.on_current_site %>
+<% if can?(:switch_language, Alchemy::Page) && languages.many? %>
- <%= form_tag switch_language_admin_pages_path, method: 'get' do %>
+ <%= form_tag switch_admin_languages_path, method: 'get' do %>
<%= select_tag(
'language_id',
- options_for_select(@languages.map { |l| [l.name, l.id] }, Alchemy::Language.current.id),
+ options_for_select(
+ languages.map { |l| [l.name, l.id] },
+ Alchemy::Language.current.id
+ ),
class: 'alchemy_selectbox short',
data: {'auto-submit' => true}
) %>
diff --git a/config/routes.rb b/config/routes.rb
index 6e8084f925..b4a1479228 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -23,7 +23,6 @@
post :order
post :flush
post :copy_language_tree
- get :switch_language
get :create_language
get :link
get :sort
@@ -88,7 +87,11 @@
end
resources :legacy_page_urls
- resources :languages
+ resources :languages do
+ collection do
+ get :switch
+ end
+ end
resource :clipboard, only: :index, controller: 'clipboard' do
collection do
diff --git a/spec/controllers/alchemy/admin/languages_controller_spec.rb b/spec/controllers/alchemy/admin/languages_controller_spec.rb
index 6169967c29..0b085cfc35 100644
--- a/spec/controllers/alchemy/admin/languages_controller_spec.rb
+++ b/spec/controllers/alchemy/admin/languages_controller_spec.rb
@@ -49,4 +49,35 @@
to eq(Alchemy::Config.get(:default_language)['page_layout'])
end
end
+
+ describe "#switch" do
+ subject(:switch) do
+ get :switch, params: { language_id: language.id }
+ end
+
+ let(:language) { create(:alchemy_language, :klingon) }
+
+ it "should store the current language in session" do
+ switch
+ expect(session[:alchemy_language_id]).to eq(language.id)
+ end
+
+ context "having a referer" do
+ before do
+ expect_any_instance_of(ActionDispatch::Request).to receive(:referer) do
+ '/admin/pages'
+ end
+ end
+
+ it "should redirect to location" do
+ is_expected.to redirect_to(admin_pages_path)
+ end
+ end
+
+ context "having no referer" do
+ it "should redirect to layoutpages" do
+ is_expected.to redirect_to(admin_dashboard_path)
+ end
+ end
+ end
end
diff --git a/spec/requests/alchemy/admin/pages_controller_spec.rb b/spec/requests/alchemy/admin/pages_controller_spec.rb
index 1a405a601a..16a5f99da6 100644
--- a/spec/requests/alchemy/admin/pages_controller_spec.rb
+++ b/spec/requests/alchemy/admin/pages_controller_spec.rb
@@ -74,22 +74,6 @@ module Alchemy
end
end
end
-
- context "with multiple sites" do
- let!(:site_2) do
- create(:alchemy_site, host: 'another-one.com')
- end
-
- let(:language_2) do
- site_2.default_language
- end
-
- it "loads languages from current site only" do
- get admin_pages_path
- expect(assigns(:languages)).to include(language)
- expect(assigns(:languages)).to_not include(language_2)
- end
- end
end
describe '#tree' do
@@ -770,39 +754,6 @@ module Alchemy
end
end
end
-
- describe "#switch_language" do
- subject(:switch_language) do
- get switch_language_admin_pages_path(language_id: language.id)
- end
-
- let(:language) { build_stubbed(:alchemy_language, :klingon) }
-
- before do
- allow(Language).to receive(:find_by).and_return(language)
- end
-
- it "should store the current language in session" do
- switch_language
- expect(session[:alchemy_language_id]).to eq(language.id)
- end
-
- it "should redirect to sitemap" do
- is_expected.to redirect_to(admin_pages_path)
- end
-
- context "coming from layoutpages" do
- before do
- allow_any_instance_of(ActionDispatch::Request).to receive(:referer) do
- 'admin/layoutpages'
- end
- end
-
- it "should redirect to layoutpages" do
- is_expected.to redirect_to(admin_layoutpages_path)
- end
- end
- end
end
end
end