Skip to content

Instantly share code, notes, and snippets.

View pmatsinopoulos's full-sized avatar
💻
Programming

Panos Matsinopoulos pmatsinopoulos

💻
Programming
View GitHub Profile
@pmatsinopoulos
pmatsinopoulos / user_mailer_locale.rb
Created November 7, 2026 10:54
Localizing outgoing ActionMailer emails using recipient language preference
class UserMailer < PublicMailer
def daily_report
daily_report_id = params[:daily_report_id]
@daily_report = Rayo::DailyReport.find(daily_report_id)
@brand = @daily_report.user.brand
with_recipient_locale(recipient_user: @daily_report.user, to_email: @daily_report.user.contact_email) do
mail from: @brand.contact_from_email_address,
to: @daily_report.user.contact_email,
@pmatsinopoulos
pmatsinopoulos / preferences_controller.rb
Created November 7, 2026 10:54
PreferencesController for updating user language preference in Ruby on Rails
# frozen_string_literal: true
module Rayo
class PreferencesController < ApplicationController
skip_before_action :authorize_user
# GET /preferences
def show
load_supported_languages
@language_preference = current_user.language_preference.presence || I18n.locale.to_s
@pmatsinopoulos
pmatsinopoulos / sessions_controller_locale.rb
Created November 7, 2026 10:54
Synchronizing and preserving locale across sign-in and sign-out in SessionsController
module Rayo
class SessionsController < ApplicationController
# POST /sessions
def create
@login = Rayo::Forms::Login.new(login_params)
if @login.save(session, current_brand)
sync_user_locale_on_sign_in(@login.user)
# ...
else
locale = session[:locale]
@pmatsinopoulos
pmatsinopoulos / sign_up_and_confirm_locale.rb
Created November 7, 2026 10:54
Capturing and promoting user locale during sign-up and email confirmation
# In Rayo::Forms::SignUp#save:
resolved_locale = locale.presence || I18n.locale.to_s
sign_up_request = Rayo::SignUpRequest.create!(
brand: brand,
country_id: country_id,
email: email,
locale: resolved_locale,
password: password,
password_confirmation: password_confirmation
@pmatsinopoulos
pmatsinopoulos / user_with_application_settings.rb
Created November 7, 2026 10:54
Persisting user language preference in user application settings
module UserWithApplicationSettings
extend ActiveSupport::Concern
included do
has_one :application_settings, class_name: 'Rayo::UserApplicationSettings', inverse_of: :user, dependent: :destroy
end
def language_preference
application_setting('language_preference')
end
@pmatsinopoulos
pmatsinopoulos / rayo_set_locale.rb
Created November 7, 2026 10:54
Rayo::SetLocale concern with authenticated user language preference
# frozen_string_literal: true
module Rayo
module SetLocale
extend ActiveSupport::Concern
TLD_TO_LOCALE = {
'gr' => :el,
'nl' => :nl
}.freeze
@pmatsinopoulos
pmatsinopoulos / application_helper.rb
Created November 7, 2026 10:54
Passing active locale across marketing site and web application URLs
module ApplicationHelper
def sign_up_url
"#{application_url}/sign_ups/new?locale=#{I18n.locale}"
end
def sign_in_url
"#{application_url}/sessions/new?locale=#{I18n.locale}"
end
def contact_us_url
@pmatsinopoulos
pmatsinopoulos / locale_controller.rb
Created November 7, 2026 10:54
LocaleController for explicit language switching in Ruby on Rails
# frozen_string_literal: true
class LocaleController < ApplicationController
skip_before_action :set_locale
# GET /set_locale?locale=XX
def set_locale
super
redirect_back_or_to root_path, allow_other_host: false
@pmatsinopoulos
pmatsinopoulos / set_locale.rb
Created November 7, 2026 10:54
SetLocale controller concern for resolving visitor language preference in Ruby on Rails
# frozen_string_literal: true
module SetLocale
extend ActiveSupport::Concern
TLD_TO_LOCALE = {
'gr' => :el,
'nl' => :nl
}.freeze
# frozen_string_literal: true
module EnqueueableJob
extend ActiveSupport::Concern
included do
def process_enqueued_job(queueable:)
enqueued_job = nil
ActiveRecord::Base.transaction do