From 4c7c8c36322531ce03d5f32c7e251c0d998a5785 Mon Sep 17 00:00:00 2001 From: Adrien Poly Date: Sun, 15 Sep 2024 08:41:24 +0200 Subject: [PATCH] Add GitHub handle to user model (#195) * add github handle to user * add an association between a user and a speaker throught the github handle * allow nil github handle --- app/controllers/sessions/omniauth_controller.rb | 1 + app/models/speaker.rb | 4 +++- app/models/user.rb | 2 ++ db/migrate/20240914162252_add_github_handle_to_user.rb | 10 ++++++++++ db/schema.rb | 4 +++- test/controllers/sessions/omniauth_controller_test.rb | 1 + test/models/speaker_test.rb | 9 +++++++++ 7 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20240914162252_add_github_handle_to_user.rb diff --git a/app/controllers/sessions/omniauth_controller.rb b/app/controllers/sessions/omniauth_controller.rb index 6e4ab35b..ede3f8ab 100644 --- a/app/controllers/sessions/omniauth_controller.rb +++ b/app/controllers/sessions/omniauth_controller.rb @@ -9,6 +9,7 @@ def create @user = User.find_or_create_by(email: github_email) do |user| user.password = SecureRandom.base58 user.verified = true + user.github_handle = omniauth.info&.try(:nickname) end connected_account.user = @user connected_account.access_token = token diff --git a/app/models/speaker.rb b/app/models/speaker.rb index d070c886..678bcafa 100644 --- a/app/models/speaker.rb +++ b/app/models/speaker.rb @@ -25,9 +25,11 @@ class Speaker < ApplicationRecord # associations has_many :speaker_talks, dependent: :destroy, inverse_of: :speaker, foreign_key: :speaker_id has_many :talks, through: :speaker_talks, inverse_of: :speakers - belongs_to :canonical, class_name: "Speaker", optional: true has_many :aliases, class_name: "Speaker", foreign_key: "canonical_id" + belongs_to :canonical, class_name: "Speaker", optional: true + belongs_to :user, primary_key: :github_handle, foreign_key: :github, optional: true + # validations validates :canonical, exclusion: {in: ->(speaker) { [speaker] }, message: "can't be itself"} diff --git a/app/models/user.rb b/app/models/user.rb index 5f56bdfa..7e3bcff6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,9 +21,11 @@ class User < ApplicationRecord has_many :sessions, dependent: :destroy, inverse_of: :user has_many :connected_accounts, dependent: :destroy has_many :watch_lists, dependent: :destroy + has_one :speaker, primary_key: :github_handle, foreign_key: :github validates :email, presence: true, uniqueness: true, format: {with: URI::MailTo::EMAIL_REGEXP} validates :password, allow_nil: true, length: {minimum: 6} + validates :github_handle, presence: true, uniqueness: true, allow_nil: true encrypts :email, deterministic: true encrypts :name diff --git a/db/migrate/20240914162252_add_github_handle_to_user.rb b/db/migrate/20240914162252_add_github_handle_to_user.rb new file mode 100644 index 00000000..16fee954 --- /dev/null +++ b/db/migrate/20240914162252_add_github_handle_to_user.rb @@ -0,0 +1,10 @@ +class AddGithubHandleToUser < ActiveRecord::Migration[7.2] + def change + add_column :users, :github_handle, :string + + add_index :users, :github_handle, unique: true, where: "github_handle IS NOT NULL" + ConnectedAccount.all.each do |connected_account| + connected_account.user.update(github_handle: connected_account.username) + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 934484ee..82021ed3 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.2].define(version: 2024_09_12_164120) do +ActiveRecord::Schema[7.2].define(version: 2024_09_14_162252) do create_table "ahoy_events", force: :cascade do |t| t.integer "visit_id" t.integer "user_id" @@ -215,7 +215,9 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "name" + t.string "github_handle" t.index ["email"], name: "index_users_on_email", unique: true + t.index ["github_handle"], name: "index_users_on_github_handle", unique: true, where: "github_handle IS NOT NULL" end create_table "watch_list_talks", force: :cascade do |t| diff --git a/test/controllers/sessions/omniauth_controller_test.rb b/test/controllers/sessions/omniauth_controller_test.rb index 4e2ac14b..c08a8e0b 100644 --- a/test/controllers/sessions/omniauth_controller_test.rb +++ b/test/controllers/sessions/omniauth_controller_test.rb @@ -31,6 +31,7 @@ def setup end assert_equal "twitter", User.last.connected_accounts.last.username + assert_equal "twitter", User.last.github_handle end test "finds existing user if already exists (developer)" do diff --git a/test/models/speaker_test.rb b/test/models/speaker_test.rb index d033ce40..3e751ca7 100644 --- a/test/models/speaker_test.rb +++ b/test/models/speaker_test.rb @@ -38,4 +38,13 @@ class SpeakerTest < ActiveSupport::TestCase speaker = Speaker.new(website: "") assert_equal "#", speaker.valid_website_url end + + test "speaker user association" do + speaker = speakers(:one) + user = users(:one) + user.update(github_handle: speaker.github) + assert_equal user.github_handle, speaker.github + assert_equal user, speaker.user + assert_equal speaker, user.speaker + end end