From a90c1d97f3a990e1e249e18c9314be3dcbd8b5eb Mon Sep 17 00:00:00 2001 From: oleghasjanov Date: Mon, 1 Jul 2024 14:47:10 +0300 Subject: [PATCH] added force localize for sms confrimation code --- app/models/phone_confirmation.rb | 40 +++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/app/models/phone_confirmation.rb b/app/models/phone_confirmation.rb index 1fb4cfdc7..33fb8b5a0 100644 --- a/app/models/phone_confirmation.rb +++ b/app/models/phone_confirmation.rb @@ -13,15 +13,11 @@ def generate_and_send_code return unless user.valid? return if user.mobile_phone_confirmed_sms_send_at.present? && user.mobile_phone_confirmed_sms_send_at > TIME_LIMIT.ago - number = SecureRandom.random_number(10_000) - padded_number = format('%04d', number) + padded_number = generate_padded_number + assign_padded_number_to_user(padded_number) + locale = locale_for_sms(user) - user.update!(mobile_phone_confirmation_code: padded_number, mobile_phone_confirmed_sms_send_at: Time.zone.now) - message_sender = Messente::Omnimessage.new( - user.mobile_phone, - I18n.t('phone_confirmations.instructions', code: padded_number) - ) - message_sender.send_message + send_sms_with_locale(locale, padded_number) end def code @@ -41,4 +37,32 @@ def confirm @user.update!(mobile_phone_confirmed_at: Time.zone.now) end + + private + + def assign_padded_number_to_user(padded_number) + user.update!(mobile_phone_confirmation_code: padded_number, mobile_phone_confirmed_sms_send_at: Time.zone.now) + end + + def send_sms_with_locale(locale, padded_number) + I18n.with_locale(locale) do + message_sender = Messente::Omnimessage.new( + user.mobile_phone, + I18n.t('phone_confirmations.instructions', code: padded_number) + ) + message_sender.send_message + end + end + + def generate_padded_number + number = SecureRandom.random_number(10_000) + format('%04d', number) + end + + def locale_for_sms(user) + force_english_localize = !user.mobile_phone.include?('+372') + default_locale = user.locale.blank? ? I18n.default_locale : user.locale + + force_english_localize ? :en : default_locale + end end