Skip to content

Commit 0508fdb

Browse files
committed
Fix exception in admin when current language is deleted
This fixes a situation where the session contains a language id for a language that no longer exists because it has been deleted. This commit fixes this in two ways: - when deleting the language, it is removed from the session if the language is identical - when trying to get the language from the session id, the lookup code now falls back to the default language if it can't be found. This is meant to fix the scenario where a different user deletes the language instead of the user with the language id in the session I believe both fixes are useful. I've tried to come up with meaningful tests but I am somewhat unfamiliar with the codebase so feel free to suggest extra things. A backport to Alchemy 7 would be appreciated if possible, as it will probably still take us a while to get to 8. (cherry picked from commit 380d62c)
1 parent 94347ca commit 0508fdb

5 files changed

Lines changed: 29 additions & 1 deletion

File tree

app/controllers/alchemy/admin/languages_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def create
3030

3131
def destroy
3232
if @language.destroy
33+
if session[:alchemy_language_id] == @language.id
34+
session.delete(:alchemy_language_id)
35+
end
3336
flash[:notice] = Alchemy.t("Language successfully removed")
3437
else
3538
flash[:warning] = @language.errors.full_messages.to_sentence

app/models/alchemy/language/code.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def code=(code)
1313

1414
module ClassMethods
1515
def find_by_code(code)
16+
return unless code.respond_to?(:split)
1617
codes = code.split("-")
1718
codes << "" if codes.length == 1
1819
on_current_site.find_by(

lib/alchemy/controller_actions.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def load_alchemy_language_from_params
8080

8181
def load_alchemy_language_from_id_or_code(id_or_code)
8282
Language.find_by(id: id_or_code) ||
83-
Language.find_by_code(id_or_code)
83+
Language.find_by_code(id_or_code) ||
84+
Language.default
8485
end
8586

8687
# Stores language in +Current.language+

spec/controllers/alchemy/admin/languages_controller_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@
118118
expect(response).to redirect_to admin_languages_path
119119
expect(flash[:notice]).to eq("Language successfully removed.")
120120
end
121+
122+
it "removes the language from session if present" do
123+
session[:alchemy_language_id] = language.id
124+
125+
delete :destroy, params: {id: language.id}
126+
expect(response).to redirect_to admin_languages_path
127+
expect(flash[:notice]).to eq("Language successfully removed.")
128+
129+
expect(session[:alchemy_language_id]).to be_nil
130+
end
121131
end
122132
end
123133

spec/controllers/alchemy/admin/pages_controller_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@
4747
end
4848
end
4949
end
50+
51+
context "with wrong or missing language in session" do
52+
let!(:site_1) { create(:alchemy_site) }
53+
let!(:site_1_default_language) { create :alchemy_language, site: site_1, default: true }
54+
55+
it "should fall back to existing language if session language does not exist" do
56+
session[:alchemy_language_id] = 123456789
57+
58+
expect {
59+
get :index
60+
}.to_not raise_error
61+
end
62+
end
5063
end
5164

5265
describe "#destroy" do

0 commit comments

Comments
 (0)