From d6987ae09f6619b12722758deec057c9a43a821a Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Tue, 31 Mar 2020 09:11:29 +0200 Subject: [PATCH] Fix bug in language from session w/o site If there is no Site yet the language can not be loaded from the users session. We must fall back to the default language. --- lib/alchemy/controller_actions.rb | 2 +- spec/libraries/controller_actions_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/alchemy/controller_actions.rb b/lib/alchemy/controller_actions.rb index ce7330d442..6ac7db7fc7 100644 --- a/lib/alchemy/controller_actions.rb +++ b/lib/alchemy/controller_actions.rb @@ -82,7 +82,7 @@ def load_alchemy_language_from_params # Load language from session if it's present on current site. # Otherwise return nil so we can load the default language from current site. def load_alchemy_language_from_session - if session[:alchemy_language_id].present? + if session[:alchemy_language_id].present? && Site.current Site.current.languages.find_by(id: session[:alchemy_language_id]) end end diff --git a/spec/libraries/controller_actions_spec.rb b/spec/libraries/controller_actions_spec.rb index 2512d63357..db5d435638 100644 --- a/spec/libraries/controller_actions_spec.rb +++ b/spec/libraries/controller_actions_spec.rb @@ -109,6 +109,19 @@ expect(Alchemy::Language.current).to eq(klingon) end + context "if no current site exists" do + before do + expect(Alchemy::Site).to receive(:current).exactly(:twice) { nil } + end + + it "should set the default language" do + controller.send :set_alchemy_language + + expect(assigns(:language)).to eq(default_language) + expect(Alchemy::Language.current).to eq(default_language) + end + end + context "if the language is not on the current site" do let(:french_site) do create(:alchemy_site, host: 'french.fr')