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

validates uniqueness of email #590

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/RedundantSelf: Redundant self detected.

end
end
8 changes: 8 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@
expect(user.acronyms).to eq "AS"
end
end

describe "uniqueness of email" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

let!(:existing_user) { create(:user, email: "[email protected]") }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

it "validates the uniqueness of email" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

user = build(:user, email: existing_user.email)
expect(user).not_to be_valid
end
end
end