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')