Skip to content

Commit

Permalink
Moves switch_language method into languages_controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Nov 1, 2019
1 parent e69d74e commit 291168d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 68 deletions.
5 changes: 5 additions & 0 deletions app/controllers/alchemy/admin/languages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 0 additions & 14 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions app/views/alchemy/admin/partials/_language_tree_select.html.erb
Original file line number Diff line number Diff line change
@@ -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? %>
<div class="button_with_label">
<%= 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}
) %>
Expand Down
7 changes: 5 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
post :order
post :flush
post :copy_language_tree
get :switch_language
get :create_language
get :link
get :sort
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions spec/controllers/alchemy/admin/languages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 0 additions & 49 deletions spec/requests/alchemy/admin/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 291168d

Please sign in to comment.