diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index 4ec340cff..580bd0e34 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -1,5 +1,5 @@ class SubscriptionsController < ApplicationController - before_action :set_user + before_action :set_user, :set_user_locale skip_authorization_check def edit @@ -28,4 +28,11 @@ class SubscriptionsController < ApplicationController def allowed_params [:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter] end + + def set_user_locale + if params[:locale].blank? + I18n.locale = I18n.available_locales.find { |locale| locale == @user.locale&.to_sym } + session[:locale] = I18n.locale + end + end end diff --git a/spec/controllers/subscriptions_controller_spec.rb b/spec/controllers/subscriptions_controller_spec.rb index ef1309c46..3c1d39414 100644 --- a/spec/controllers/subscriptions_controller_spec.rb +++ b/spec/controllers/subscriptions_controller_spec.rb @@ -12,5 +12,12 @@ describe SubscriptionsController do expect(response).to redirect_to "/" expect(flash[:alert]).to eq "You do not have permission to access this page." end + + it "shows the 'not allowed' message in the current locale" do + get :edit, params: { token: "", locale: :es } + + expect(response).to redirect_to "/" + expect(flash[:alert]).to eq "No tienes permiso para acceder a esta pÔgina." + end end end diff --git a/spec/system/subscriptions_spec.rb b/spec/system/subscriptions_spec.rb index d480f8a38..39cda642a 100644 --- a/spec/system/subscriptions_spec.rb +++ b/spec/system/subscriptions_spec.rb @@ -3,6 +3,28 @@ require "rails_helper" describe "Subscriptions" do let(:user) { create(:user, subscriptions_token: SecureRandom.base58(32)) } + context "Edit page" do + scenario "Render content in the user's preferred locale" do + user.update!(locale: "es") + visit edit_subscriptions_path(token: user.subscriptions_token) + + expect(page).to have_content "Notificaciones" + expect(page).to have_field "Recibir un email cuando alguien comenta en mis propuestas o debates", + type: :checkbox + expect(page).to have_field "Recibir un email cuando alguien contesta a mis comentarios", type: :checkbox + expect(page).to have_field "Recibir emails con información interesante sobre la web", type: :checkbox + expect(page).to have_field "Recibir resumen de notificaciones sobre propuestas", type: :checkbox + expect(page).to have_field "Recibir emails con mensajes privados", type: :checkbox + expect(page).to have_button "Guardar cambios" + end + + scenario "Use the locale in the parameters when accessing anonymously" do + visit edit_subscriptions_path(token: user.subscriptions_token, locale: :es) + + expect(page).to have_content "Notificaciones" + end + end + context "Update" do scenario "Allow updating the status notification" do user.update!(email_on_comment: false,