From 259a89e891354eeab68ddeb969ad9f469024893d Mon Sep 17 00:00:00 2001 From: Felix Eckhofer <felix@eckhofer.com> Date: Sun, 22 Dec 2024 23:33:33 +0100 Subject: [PATCH] Allow users to specify their language proficiency Required by the more languages team. --- app/controllers/application_controller.rb | 2 +- app/models/user.rb | 6 ++++++ app/views/assignments/_user_avatar.html.erb | 7 +++++++ app/views/candidates/_user_avatar.html.erb | 7 +++++++ app/views/devise/registrations/edit.html.erb | 16 ++++++++++++++++ config/locales/en.yml | 7 +++++++ .../20241222215716_add_languages_to_users.rb | 6 ++++++ db/schema.rb | 4 +++- 8 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20241222215716_add_languages_to_users.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 53d1cf0..c964fde 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,7 +6,7 @@ class ApplicationController < ActionController::Base def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:invitation_token]) devise_parameter_sanitizer.permit(:account_update) do |u| - u.permit(:name, :email, :password, :password_confirmation, :avatar_color, :telegram_username, :current_password) + u.permit(:name, :email, :password, :password_confirmation, :avatar_color, :languages_from, :languages_to, :telegram_username, :current_password) end end diff --git a/app/models/user.rb b/app/models/user.rb index 867e3ad..29a0fb2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,12 @@ class User < ApplicationRecord validates :name, uniqueness: { case_sensitive: false, message: "already in use" }, allow_nil: false validates :email, uniqueness: { case_sensitive: false, message: "already in use" }, allow_nil: true, allow_blank: true + validates :languages_from, format: { with: /\A([a-z][a-z])(,[a-z][a-z])*\z/, message: "please use comma-separated two-letter codes"}, allow_blank: true + validates :languages_from, length: { maximum: 14 } + + validates :languages_to, format: { with: /\A([a-z][a-z])(,[a-z][a-z])*\z/, message: "please use comma-separated two-letter codes"}, allow_blank: true + validates :languages_to, length: { maximum: 14 } + validates :invitation_token, presence: true, on: :create validate :valid_invitation_token, on: :create diff --git a/app/views/assignments/_user_avatar.html.erb b/app/views/assignments/_user_avatar.html.erb index a22856e..1ed5259 100644 --- a/app/views/assignments/_user_avatar.html.erb +++ b/app/views/assignments/_user_avatar.html.erb @@ -11,3 +11,10 @@ <% end %> </button> </span> +<% if user.languages_from || user.languages_to %> +<small class="text-2xs uppercase font-light bg-black/10 rounded-sm p-1 mr-1" aria-label="Languages <%= user.name %> can translate" title="Languages <%= user.name %> can translate"> + <%= user.languages_from %> + <span aria-label="to">→</span> + <%= user.languages_to %> +</small> +<% end %> diff --git a/app/views/candidates/_user_avatar.html.erb b/app/views/candidates/_user_avatar.html.erb index 382cef8..0197fff 100644 --- a/app/views/candidates/_user_avatar.html.erb +++ b/app/views/candidates/_user_avatar.html.erb @@ -12,6 +12,13 @@ <% end %> </button> </span> +<% if user.languages_from || user.languages_to %> +<small class="text-2xs uppercase font-light bg-black/10 rounded-sm p-1 ml-1"> + <%= user.languages_from %> + <% if user.languages_from && user.languages_to %>→<% end %> + <%= user.languages_to %> +</small> +<% end %> <% if candidate.note %> <span class="relative ml-1.5"><span class="max-w-60 max-h-14 overflow-scroll bg-gray-600 text-white text-xs font-medium px-2 py-1 rounded-md relative inline-block"><%= candidate.note %></span><span class="w-0 h-0 border-t-[6px] border-t-transparent border-b-[6px] border-b-transparent border-r-[6px] border-r-gray-600 absolute left-[-5px] top-[20%] transform -translate-y-1/2"></span> </span> <% end %> diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 17dc3c4..b517dde 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -43,6 +43,22 @@ <%= f.text_field :telegram_username %> </div> +<fieldset class="border border-gray-300 p-4 rounded-md"> + <legend class="text-lg font-semibold">More Languages Team Only</legend> + <i class="block">Please use comma-separated two-letter codes.</i> + <i class="block">Leave empty unless you are with the more languages team.</i> + + <div class="field"> + <%= f.label :languages_from %> + <%= f.text_field :languages_from, placeholder: "de,en" %> + </div> + + <div class="field"> + <%= f.label :languages_to %> + <%= f.text_field :languages_to, placeholder: "jp,es" %> + </div> + </fieldset> + <div class="field"> <%= f.label :current_password %> <i class="block">(we need your current password to confirm your changes)</i> diff --git a/config/locales/en.yml b/config/locales/en.yml index 6c349ae..23e7f5b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -29,3 +29,10 @@ en: hello: "Hello world" + +en: + activerecord: + attributes: + user: + languages_from: "Languages you can translate from" + languages_to: "Languages you can translate to" diff --git a/db/migrate/20241222215716_add_languages_to_users.rb b/db/migrate/20241222215716_add_languages_to_users.rb new file mode 100644 index 0000000..1f07c8a --- /dev/null +++ b/db/migrate/20241222215716_add_languages_to_users.rb @@ -0,0 +1,6 @@ +class AddLanguagesToUsers < ActiveRecord::Migration[7.1] + def change + add_column :users, :languages_from, :string + add_column :users, :languages_to, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 8a993e7..d3627a4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_12_20_212328) do +ActiveRecord::Schema[7.1].define(version: 2024_12_22_215716) do create_table "assignments", force: :cascade do |t| t.integer "user_id", null: false t.integer "session_id", null: false @@ -279,6 +279,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_12_20_212328) do t.datetime "remember_created_at" t.boolean "shiftcoordinator", default: false, null: false t.string "invitation_token" + t.string "languages_from" + t.string "languages_to" end add_foreign_key "assignments", "sessions" -- GitLab