Skip to content

Commit

Permalink
Add GitHub handle to user model (#195)
Browse files Browse the repository at this point in the history
* add github handle to user

* add an association between a user and a speaker throught the github handle

* allow nil github handle
  • Loading branch information
adrienpoly committed Sep 15, 2024
1 parent 6347906 commit 4c7c8c3
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/controllers/sessions/omniauth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/models/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240914162252_add_github_handle_to_user.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion db/schema.rb

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

1 change: 1 addition & 0 deletions test/controllers/sessions/omniauth_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/models/speaker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4c7c8c3

Please sign in to comment.