diff --git a/app/models/user.rb b/app/models/user.rb index d012a168..7699f2c3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -47,6 +47,8 @@ class User < ApplicationRecord :invitable validates_presence_of :first_name, :last_name + validate :unique_email_address? + has_one :account, foreign_key: "owner_id", inverse_of: :owner belongs_to :organization, class_name: "Account", inverse_of: :users @@ -74,4 +76,8 @@ def color def acronyms first_name[0] + last_name[0] end + + def unique_email_address? + return false if User.where(email: self.email).any? + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 71fcb290..adc2fc71 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -96,4 +96,12 @@ expect(user.acronyms).to eq "AS" end end + + describe "uniqueness of email" do + let!(:existing_user) { create(:user, email: "another@email.com") } + it "validates the uniqueness of email" do + user = build(:user, email: existing_user.email) + expect(user).not_to be_valid + end + end end