Render the page in the user's preferred locale
We add new method set_user_locale to render the page with the user's preferred locale. Note that we add a condition 'if params[:locale].blank?' to recover the user's preferred locale. This is necessary because it may be the case that the user does not have an associated locale, and when execute '@user.locale' when this value is 'nil', by default returns the default locale. As we do not want this to happen and we want the locale we receive as parameter to prevail in this case.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
class SubscriptionsController < ApplicationController
|
class SubscriptionsController < ApplicationController
|
||||||
before_action :set_user
|
before_action :set_user, :set_user_locale
|
||||||
skip_authorization_check
|
skip_authorization_check
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
@@ -28,4 +28,11 @@ class SubscriptionsController < ApplicationController
|
|||||||
def allowed_params
|
def allowed_params
|
||||||
[:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter]
|
[:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter]
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -12,5 +12,12 @@ describe SubscriptionsController do
|
|||||||
expect(response).to redirect_to "/"
|
expect(response).to redirect_to "/"
|
||||||
expect(flash[:alert]).to eq "You do not have permission to access this page."
|
expect(flash[:alert]).to eq "You do not have permission to access this page."
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,6 +3,28 @@ require "rails_helper"
|
|||||||
describe "Subscriptions" do
|
describe "Subscriptions" do
|
||||||
let(:user) { create(:user, subscriptions_token: SecureRandom.base58(32)) }
|
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
|
context "Update" do
|
||||||
scenario "Allow updating the status notification" do
|
scenario "Allow updating the status notification" do
|
||||||
user.update!(email_on_comment: false,
|
user.update!(email_on_comment: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user