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

Removed email default value, now facebook users should use null, vali… #41

Merged
merged 2 commits into from
Mar 7, 2017

Conversation

felipegarcia92
Copy link

…dations updated

This PR aims to fix the following situation:

User logs in with Facebook but doesn't share their email, has email set to "" (empty string)
Second user logs in with Facebook and doesn't either share their email, so he gets a RecordNotUnique exception when trying to be set the very same default "" as email

Now uniqueness validation runs only for users who do have an e-mail set (this shouldn't mess with users adding their email later, after registering, which should be validated)

@@ -45,6 +46,10 @@ def full_name
"#{first_name} #{last_name}"
end

def uses_email?
provider == 'email' || !email.nil?
Copy link
Contributor

Choose a reason for hiding this comment

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

email.present? does the same job as !email.nil? and it's clearer.

@@ -45,6 +46,10 @@ def full_name
"#{first_name} #{last_name}"
end

def uses_email?
Copy link
Contributor

Choose a reason for hiding this comment

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

Make this private

@@ -34,7 +34,16 @@
describe User do
describe 'validations' do
it { should validate_presence_of(:email) }
it { should validate_uniqueness_of(:email).case_insensitive }
Copy link
Contributor

Choose a reason for hiding this comment

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

Change to it { should validate_uniqueness_of(:uid) }

@felipegarcia92 felipegarcia92 force-pushed the fix/fb-email-uniqueness branch from a819f00 to e66530e Compare March 6, 2017 19:20
@felipegarcia92
Copy link
Author

@santiagovidal @MaicolBen fixed, thanks!

Copy link
Contributor

@santiagovidal santiagovidal left a comment

Choose a reason for hiding this comment

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

Looks good 👍 Nice fix.

Copy link
Contributor

@MaicolBen MaicolBen left a comment

Choose a reason for hiding this comment

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

When @matiasmansilla1989 approves, you can merge

@@ -36,7 +36,8 @@ class User < ApplicationRecord
:recoverable, :rememberable, :trackable, :validatable
include DeviseTokenAuth::Concerns::User

validates :email, uniqueness: true
validates :uid, uniqueness: { scope: :provider }
validates :email, uniqueness: true, if: :uses_email?
Copy link
Contributor

Choose a reason for hiding this comment

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

@MaicolBen @felipegarcia92 Based on the discussion that we had today, when signing up with email (not Facebook). Should not being validated that email is present?

Copy link
Contributor

Choose a reason for hiding this comment

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

But in that case uses_email? will be true

Copy link
Author

Choose a reason for hiding this comment

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

Agree with @MaicolBen

Copy link
Contributor

@matiasmansilla1989 matiasmansilla1989 Mar 7, 2017

Choose a reason for hiding this comment

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

@MaicolBen @felipegarcia92
But, if provider == 'email' is true then uses_email? will return true. Does this assure that the email is present? I think no, but maybe I'm loosing something here.

Copy link
Contributor

Choose a reason for hiding this comment

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

If provider == 'email' you validate uniqueness because you need the email to be present.

@@ -3,5 +3,12 @@
email { Faker::Internet.unique.email }
password { Faker::Internet.password(8) }
username { Faker::Internet.unique.user_name }
uid { Faker::Number.unique.number(10) }
Copy link
Contributor

Choose a reason for hiding this comment

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

align to the right, same as username


trait :with_fb do
password { nil }
email { nil }
Copy link
Contributor

Choose a reason for hiding this comment

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

a block is not necessary when using literals (nil, string, integer)

trait :with_fb do
password { nil }
email { nil }
provider { 'facebook' }
Copy link
Contributor

Choose a reason for hiding this comment

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

align to the right too

Copy link
Contributor

Choose a reason for hiding this comment

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

This is wrong when they aren't blocks, a ruby method should have one space between the method name and the first parameter, that's why rubocop complains about it in the code analysis.

Copy link
Author

Choose a reason for hiding this comment

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

Yep, just agreed to fix it like it is now

@felipegarcia92 felipegarcia92 force-pushed the fix/fb-email-uniqueness branch from e66530e to 18e0c8c Compare March 7, 2017 14:19
@felipegarcia92
Copy link
Author

@matiasmansilla1989 fixed, thanks!

@felipegarcia92 felipegarcia92 force-pushed the fix/fb-email-uniqueness branch from 18e0c8c to 90e7aec Compare March 7, 2017 15:05

context 'when was created with facebook login' do
subject { build :user, :with_fb }
it { should_not validate_uniqueness_of(:email) }
Copy link
Contributor

Choose a reason for hiding this comment

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

Add it { should_not validate_presence_of(:email) }


context 'when was created with regular login' do
subject { build :user }
it { should validate_uniqueness_of(:email).case_insensitive }
Copy link
Contributor

@matiasmansilla1989 matiasmansilla1989 Mar 7, 2017

Choose a reason for hiding this comment

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

@MaicolBen following your last comment, he should validate the presence of the email here, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

@falvariza falvariza force-pushed the fix/fb-email-uniqueness branch from 1bc1380 to b178f5e Compare March 7, 2017 18:42
@matiasmansilla1989
Copy link
Contributor

@falvariza @felipegarcia92 Great Job!

@matiasmansilla1989 matiasmansilla1989 merged commit b7b4e93 into master Mar 7, 2017
@matiasmansilla1989 matiasmansilla1989 deleted the fix/fb-email-uniqueness branch March 7, 2017 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants