Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.9)
nokogiri (1.15.4-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
Expand Down Expand Up @@ -340,6 +342,7 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.6.4-x86_64-darwin)
sqlite3 (1.6.4-x86_64-linux)
stimulus-rails (1.2.2)
railties (>= 6.0.0)
Expand Down Expand Up @@ -375,6 +378,7 @@ GEM
zeitwerk (2.6.11)

PLATFORMS
x86_64-darwin-22
x86_64-linux

DEPENDENCIES
Expand Down
9 changes: 9 additions & 0 deletions app/assets/images/icons/en.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions app/assets/images/icons/ru.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :set_current_locale
helper_method :current_locale

rescue_from ActionPolicy::Unauthorized do |_e|
redirect_to root_path
end

def set_current_locale
Current.locale = extract_locale_from_user || extract_locale_from_session || I18n.default_locale
end

private

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: %i[name avatar])
devise_parameter_sanitizer.permit(:account_update, keys: %i[name avatar])
end

def current_locale
Current.locale
end

def extract_locale_from_user
current_user&.locale if defined?(current_user) && current_user&.locale
end

def extract_locale_from_session
session[:locale]
end
end
30 changes: 30 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
class PagesController < ApplicationController
def welcome; end

def switch_locale
Current.locale = locale
I18n.locale = Current.locale
store_locale

redirect_back(fallback_location: root_path)
end

private

def params_locale
params[:locale].to_sym
end

def default_locale
@default_locale ||= current_user&.locale || I18n.default_locale
end

def locale
I18n.available_locales.include?(params_locale) ? params_locale : default_locale
end

def store_locale
if user_signed_in?
current_user.update(locale: I18n.locale)
else
session[:locale] = I18n.locale
end
end
end
11 changes: 11 additions & 0 deletions app/helpers/navbar_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module NavbarHelper
def selected_language_icon
Rails.cache.fetch("selected_language_name/#{current_locale}", expires_in: 1.day) do
image_tag "icons/#{current_locale}.svg", size: "23", class: "rounded-2", title: t("navbar.language")
end
end

def next_locale
I18n.locale == :ru ? :en : :ru
end
end
3 changes: 3 additions & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
thread_mattr_accessor :locale
end
19 changes: 19 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,29 @@ class User < ApplicationRecord
foreign_key: :recipient_id, dependent: :destroy, inverse_of: :recipient

enum :role, %i[applicant company moderator admin]
# Нежелательное использование колбека в модели, но это врменное решение до определения логики devise
before_validation :set_name, if: -> { name.blank? }, on: :create

validates :locale, presence: true

def owner?(vacancy)
id == vacancy.user_id
end

def display_name
name || name_from_email
end

private

def set_name
self.name = name_from_email
end

def name_from_email
name_part = email.split("@").first
return name_part if [".", "_", "-"].include?(name_part[1])

name_part.humanize
end
end
12 changes: 10 additions & 2 deletions app/views/layouts/_header.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ nav.navbar.navbar-expand-lg.bg-body-tertiary
- if user_signed_in?
li.nav-item
= link_to "My Vacancies", my_vacancies_vacancies_path, class: "nav-link fs-4 text-black"
ul.navbar-nav.ms-3
ul.navbar-nav.ms-3.gap-1
li.nav-item[data-turbo="false"]
= link_to switch_locale_path(locale: next_locale), class: "btn btn-outline-primary border-0 me-2" do
= selected_language_icon

li.nav-item.dropdown
- if user_signed_in?
= link_to current_user.name? ? current_user.name : current_user.email, "#", class: "dropdown-toggle btn btn-outline-primary", id: "navbarDropdown", role: "button", "data-bs-toggle" => "dropdown", "aria-expanded" => "false"
= link_to "javascript:void(0);",
class: "dropdown-toggle btn btn-outline-primary", id: "navbarDropdown", role: "button",
"data-bs-toggle" => "dropdown", "aria-expanded" => "false" do
span.ms-1.me-2
= current_user.display_name
.dropdown-menu[aria-labelledby="navbarDropdown"]
= link_to "Edit profile", edit_user_registration_path, class: "dropdown-item"
.li.hr.dropdown-divider
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@

en:
hello: "Hello world"
navbar:
language: Change language
3 changes: 3 additions & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,6 @@ ru:
long: "%d %B %Y, %H:%M"
short: "%d %b, %H:%M"
pm: вечера
navbar:
language: Смена языка

2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
resources :vacancies do
get :my_vacancies, on: :collection
end

get "/switch_locale/:locale", to: "pages#switch_locale", as: :switch_locale
end
6 changes: 3 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions spec/requests/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require "rails_helper"

describe PagesController, type: :request do
describe "GET /welcome" do
it "returns http status success" do
get root_url
expect(response).to have_http_status(:success)
end
end

describe "GET #switch_locale" do
let(:user) { create(:user) }

context "when user is signed in" do
before { sign_in user }

it "changes the I18n locale" do
get switch_locale_path(locale: "ru")
expect(I18n.locale).to eq(:ru)
end

it "changes the user locale" do
get switch_locale_path(locale: "ru")
expect(user.reload.locale).to eq("ru")
end

it "clears the session locale" do
get switch_locale_path(locale: "ru")
expect(session[:locale]).to be_nil
end

it "redirects back" do
get switch_locale_path(locale: "ru")
expect(response).to redirect_to(root_path)
end
end

context "when user is not signed in" do
it "changes the I18n locale" do
get switch_locale_path(locale: "ru")
expect(I18n.locale).to eq(:ru)
end

it "changes the session locale" do
get switch_locale_path(locale: "ru")
expect(session[:locale]).to eq(:ru)
end

it "redirects back" do
get switch_locale_path(locale: "ru")
expect(response).to redirect_to(root_path)
end
end

context "when the requested locale is not available" do
it "falls back to the default I18n locale" do
get switch_locale_path(locale: "invalid_locale")
expect(I18n.locale).to eq(:en)
end

it "falls back to the default session locale" do
get switch_locale_path(locale: "invalid_locale")
expect(session[:locale]).to eq(:en)
end

it "redirects back" do
get switch_locale_path(locale: "invalid_locale")
expect(response).to redirect_to(root_path)
end
end
end
end
8 changes: 0 additions & 8 deletions spec/requests/pages_spec.rb

This file was deleted.