Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear current language when switching sites #2957

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/concerns/alchemy/admin/current_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ module CurrentLanguage
extend ActiveSupport::Concern

included do
# This needs to happen before BaseController#current_alchemy_site sets the session value.
prepend_before_action :clear_current_language_from_session, if: :switching_site?, only: :index
before_action :load_current_language
end

private

def switching_site?
params[:site_id].present? && (params[:site_id] != session[:alchemy_site_id]&.to_s)
end

def clear_current_language_from_session
session.delete(:alchemy_language_id)
end

def load_current_language
@current_language = if session[:alchemy_language_id].present?
set_alchemy_language(session[:alchemy_language_id])
Expand Down
33 changes: 33 additions & 0 deletions lib/alchemy/test_support/current_language_shared_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

RSpec.shared_examples_for "a controller that loads current language" do |args|
context "when session has current language id key" do
let!(:site_1) { create(:alchemy_site) }
let!(:site_1_default_language) { create :alchemy_language, site: site_1, default: true }
let!(:another_site_1_language) { create :alchemy_language, site: site_1, code: :de }
let(:site_2) { create :alchemy_site, host: "another.host", languages: [build(:alchemy_language, code: :en), build(:alchemy_language, code: :de)] }

context "on index action" do
context "when switching the current site" do
before do
session[:alchemy_site_id] = site_1.id
session[:alchemy_language_id] = another_site_1_language.id
end

it "sets @current_language to the new site default language" do
get :index, params: {site_id: site_2.id}
expect(assigns(:current_language)).to eq site_2.default_language
end
end

context "when no language to set" do
it "shows flash warning with redirect" do
Alchemy::Language.destroy_all
get :index, params: {site_id: site_1.id}
expect(flash[:warning]).to eq Alchemy.t("Please create a language first.")
expect(response).to redirect_to admin_languages_path
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Alchemy
authorize_user(:as_admin)
end

it_behaves_like "a controller that loads current language"

describe "#index" do
context "with no language present" do
it "redirects to the languages admin" do
Expand Down
2 changes: 2 additions & 0 deletions spec/controllers/alchemy/admin/nodes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module Alchemy
authorize_user(:as_admin)
end

it_behaves_like "a controller that loads current language"

describe "#index" do
context "if no language is present" do
it "redirects to the language admin" do
Expand Down
2 changes: 2 additions & 0 deletions spec/controllers/alchemy/admin/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
authorize_user(:as_admin)
end

it_behaves_like "a controller that loads current language"

describe "#index" do
let!(:page) { create(:alchemy_page) }

Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require "alchemy/test_support/shared_contexts"
require "alchemy/test_support/shared_link_tab_examples"
require "alchemy/test_support/shared_uploader_examples"
require "alchemy/test_support/current_language_shared_examples"

require_relative "support/calculation_examples"
require_relative "support/hint_examples"
Expand Down