From c24a6af99f5110ecbf20186950c29f1c202da818 Mon Sep 17 00:00:00 2001 From: Cesium Date: Wed, 4 Dec 2024 13:57:07 -0800 Subject: [PATCH 1/3] Default language is no longer editable --- app/controllers/languages_controller.rb | 10 +++++++--- config/locales/controllers/en.yml | 4 ++++ spec/controllers/languages_controller_spec.rb | 19 +++++++++++++------ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/controllers/languages_controller.rb b/app/controllers/languages_controller.rb index 96892070d76..8475fd6a54b 100644 --- a/app/controllers/languages_controller.rb +++ b/app/controllers/languages_controller.rb @@ -1,5 +1,4 @@ class LanguagesController < ApplicationController - def index @languages = Language.default_order @works_counts = Rails.cache.fetch("/v1/languages/work_counts/#{current_user.present?}", expires_in: 1.day) do @@ -16,7 +15,7 @@ def create @language = Language.new(language_params) authorize @language if @language.save - flash[:notice] = t('successfully_added', default: 'Language was successfully added.') + flash[:notice] = t("languages.successfully_added") redirect_to languages_path else render action: "new" @@ -26,13 +25,17 @@ def create def edit @language = Language.find_by(short: params[:id]) authorize @language + return unless @language == Language.default + + flash[:error] = t("languages.cannot_edit_default") + redirect_to languages_path end def update @language = Language.find_by(short: params[:id]) authorize @language if @language.update(language_params) - flash[:notice] = t('successfully_updated', default: 'Language was successfully updated.') + flash[:notice] = t("languages.successfully_updated") redirect_to languages_path else render action: "new" @@ -40,6 +43,7 @@ def update end private + def language_params params.require(:language).permit( :name, :short, :support_available, :abuse_support_available, :sortable_name diff --git a/config/locales/controllers/en.yml b/config/locales/controllers/en.yml index ea56888e5be..cd354033d27 100644 --- a/config/locales/controllers/en.yml +++ b/config/locales/controllers/en.yml @@ -104,6 +104,10 @@ en: kudos: create: success: Thank you for leaving kudos! + languages: + cannot_edit_default: Sorry, you can’t edit the default language. + successfully_added: Language was successfully added. + successfully_updated: Language was successfully updated. muted: users: create: diff --git a/spec/controllers/languages_controller_spec.rb b/spec/controllers/languages_controller_spec.rb index 96065f7f6b3..74b53bbe103 100644 --- a/spec/controllers/languages_controller_spec.rb +++ b/spec/controllers/languages_controller_spec.rb @@ -59,7 +59,7 @@ end describe "POST create" do - let(:language_params) { + let(:language_params) do { language: { name: "Suomi", @@ -69,7 +69,7 @@ sortable_name: "su" } } - } + end context "when not logged in" do it "redirects with error" do @@ -140,21 +140,28 @@ end %w[translation superadmin].each do |role| + Language.create(name: "Suomi", short: "fi") context "when logged in as an admin with #{role} role" do let(:admin) { create(:admin, roles: [role]) } - it "renders the edit template" do + it "renders the edit template for a non-default language" do fake_login_admin(admin) - get :edit, params: { id: "en" } + get :edit, params: { id: "fi" } expect(response).to render_template("edit") end + + it "redirects for the default language" do + fake_login_admin(admin) + get :edit, params: { id: "en" } + it_redirects_to_with_error(languages_path, "Sorry, you can’t edit the default language.") + end end end end describe "PUT update" do let(:finnish) { Language.create(name: "Suomi", short: "fi") } - let(:language_params) { + let(:language_params) do { id: finnish.short, language: { @@ -165,7 +172,7 @@ sortable_name: "su" } } - } + end context "when not logged in" do it "redirects with error" do From 25c922774de70997aacb08d7a88fee1d648a6f30 Mon Sep 17 00:00:00 2001 From: Cesium Date: Wed, 4 Dec 2024 14:21:01 -0800 Subject: [PATCH 2/3] fix UT --- spec/controllers/languages_controller_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/controllers/languages_controller_spec.rb b/spec/controllers/languages_controller_spec.rb index 74b53bbe103..6b6f11c44c5 100644 --- a/spec/controllers/languages_controller_spec.rb +++ b/spec/controllers/languages_controller_spec.rb @@ -140,13 +140,13 @@ end %w[translation superadmin].each do |role| - Language.create(name: "Suomi", short: "fi") + Language.create(name: "Deutsch", short: "de") context "when logged in as an admin with #{role} role" do let(:admin) { create(:admin, roles: [role]) } it "renders the edit template for a non-default language" do fake_login_admin(admin) - get :edit, params: { id: "fi" } + get :edit, params: { id: "de" } expect(response).to render_template("edit") end From 4978fdd743f745086a7a07346756b8a1e33a469c Mon Sep 17 00:00:00 2001 From: Cesium-Ice Date: Sat, 7 Dec 2024 03:42:01 -0800 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Bilka --- config/locales/controllers/en.yml | 2 +- spec/controllers/languages_controller_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/controllers/en.yml b/config/locales/controllers/en.yml index cd354033d27..5a4ce889026 100644 --- a/config/locales/controllers/en.yml +++ b/config/locales/controllers/en.yml @@ -105,7 +105,7 @@ en: create: success: Thank you for leaving kudos! languages: - cannot_edit_default: Sorry, you can’t edit the default language. + cannot_edit_default: Sorry, you can't edit the default language. successfully_added: Language was successfully added. successfully_updated: Language was successfully updated. muted: diff --git a/spec/controllers/languages_controller_spec.rb b/spec/controllers/languages_controller_spec.rb index 6b6f11c44c5..3723aaa32c7 100644 --- a/spec/controllers/languages_controller_spec.rb +++ b/spec/controllers/languages_controller_spec.rb @@ -153,7 +153,7 @@ it "redirects for the default language" do fake_login_admin(admin) get :edit, params: { id: "en" } - it_redirects_to_with_error(languages_path, "Sorry, you can’t edit the default language.") + it_redirects_to_with_error(languages_path, "Sorry, you can't edit the default language.") end end end