Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dl/username fix #157

Merged
merged 2 commits into from
Sep 16, 2024
Merged
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 app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class User < ApplicationRecord
has_many :link_clicks, dependent: :destroy
has_many :achievement_views, dependent: :destroy

VALID_USERNAME_REGEX = /\A[a-zA-Z0-9_]+\z/

validates :username, presence: true, uniqueness: true, format: { with: VALID_USERNAME_REGEX, message: 'can only contain letters, numbers, and underscores' }

validates :username, uniqueness: true, allow_blank: true
validates :full_name, presence: true
validate :ensure_username_presence
Expand Down
5 changes: 2 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# spec/models/user_spec.rb
require 'rails_helper'

RSpec.describe User, type: :model do
describe 'validations' do
it { should validate_presence_of(:email) }
it { should validate_uniqueness_of(:username).allow_blank }
it { should validate_uniqueness_of(:username) } # Removed .allow_blank
it { should validate_presence_of(:full_name) }
end

Expand Down Expand Up @@ -67,4 +66,4 @@
expect(user.parsed_tags).to eq(['ruby', 'rails'])
end
end
end
end
Loading