From 9b8f2bbcbe2ce3a60919e6a0adcca2865f176090 Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 14 Jul 2021 12:43:54 +0100 Subject: [PATCH] Remove code complexity around resetting language preferences This was originally introduced since we saved the user and showed the result on the same action. Now that the preferences controller saves and redirects, the user model and associated language preferences are reloaded between requests, and this code is no longer required. --- app/controllers/application_controller.rb | 7 +++---- app/models/user.rb | 5 ----- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index fc8b75b60..2083627f5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -173,8 +173,7 @@ class ApplicationController < ActionController::Base end end - def preferred_languages(reset: false) - @preferred_languages = nil if reset + def preferred_languages @preferred_languages ||= if params[:locale] Locale.list(params[:locale]) elsif current_user @@ -186,13 +185,13 @@ class ApplicationController < ActionController::Base helper_method :preferred_languages - def set_locale(reset: false) + def set_locale if current_user&.languages&.empty? && !http_accept_language.user_preferred_languages.empty? current_user.languages = http_accept_language.user_preferred_languages current_user.save end - I18n.locale = Locale.available.preferred(preferred_languages(:reset => reset)) + I18n.locale = Locale.available.preferred(preferred_languages) response.headers["Vary"] = "Accept-Language" response.headers["Content-Language"] = I18n.locale.to_s diff --git a/app/models/user.rb b/app/models/user.rb index 964359e9c..4b471f60d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -118,7 +118,6 @@ class User < ApplicationRecord before_save :encrypt_password before_save :update_tile after_save :spam_check - after_save :reset_preferred_languages def to_param display_name @@ -179,10 +178,6 @@ class User < ApplicationRecord @preferred_languages ||= Locale.list(languages) end - def reset_preferred_languages - @preferred_languages = nil - end - def nearby(radius = Settings.nearby_radius, num = Settings.nearby_users) if home_lon && home_lat gc = OSM::GreatCircle.new(home_lat, home_lon) -- 2.39.5