mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 17:52:45 +08:00
FIX: allow multiple secondary emails
This commit is contained in:
parent
98478d78d7
commit
c3129444ea
|
@ -11,7 +11,7 @@ class UserEmail < ActiveRecord::Base
|
|||
validates :email, email: true, format: { with: EmailValidator.email_regex },
|
||||
if: :validate_email?
|
||||
|
||||
validates :primary, uniqueness: { scope: [:user_id] }, if: :user_id
|
||||
validates :primary, uniqueness: { scope: [:user_id] }, if: [:user_id, :primary]
|
||||
validate :user_id_not_changed, if: :primary
|
||||
validate :unique_email
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
class ChangeUserEmailsPrimaryIndex < ActiveRecord::Migration[5.1]
|
||||
def up
|
||||
remove_index :user_emails, [:user_id, :primary]
|
||||
add_index :user_emails, [:user_id, :primary], unique: true, where: '"primary"'
|
||||
end
|
||||
|
||||
def down
|
||||
remove_index :user_emails, [:user_id, :primary]
|
||||
add_index :user_emails, [:user_id, :primary], unique: true
|
||||
end
|
||||
end
|
36
spec/models/user_email_spec.rb
Normal file
36
spec/models/user_email_spec.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
require 'rails_helper'
|
||||
require_dependency 'user_email'
|
||||
|
||||
describe UserEmail do
|
||||
context "validation" do
|
||||
it "allows only one primary email" do
|
||||
user = Fabricate(:user_single_email)
|
||||
expect {
|
||||
Fabricate(:alternate_email, user: user, primary: true)
|
||||
}.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
|
||||
it "allows multiple secondary emails" do
|
||||
user = Fabricate(:user_single_email)
|
||||
Fabricate(:alternate_email, user: user, primary: false)
|
||||
Fabricate(:alternate_email, user: user, primary: false)
|
||||
expect(user.user_emails.count).to eq 3
|
||||
end
|
||||
end
|
||||
|
||||
context "indexes" do
|
||||
it "allows only one primary email" do
|
||||
user = Fabricate(:user_single_email)
|
||||
expect {
|
||||
Fabricate.build(:alternate_email, user: user, primary: true).save(validate: false)
|
||||
}.to raise_error(ActiveRecord::RecordNotUnique)
|
||||
end
|
||||
|
||||
it "allows multiple secondary emails" do
|
||||
user = Fabricate(:user_single_email)
|
||||
Fabricate.build(:alternate_email, user: user, primary: false).save(validate: false)
|
||||
Fabricate.build(:alternate_email, user: user, primary: false).save(validate: false)
|
||||
expect(user.user_emails.count).to eq 3
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user