From b6996879f52c20e50ef874b7ca6dce176f12a6d9 Mon Sep 17 00:00:00 2001 From: Oreoluwa Akinniranye Date: Wed, 3 Feb 2016 17:09:16 +0100 Subject: [PATCH] added validator for email address to only permit Andelans --- app/models/concerns/andela_validator.rb | 4 ++-- app/models/social_provider.rb | 1 + app/models/user.rb | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/models/concerns/andela_validator.rb b/app/models/concerns/andela_validator.rb index 29c1758..957823c 100644 --- a/app/models/concerns/andela_validator.rb +++ b/app/models/concerns/andela_validator.rb @@ -1,9 +1,9 @@ module AndelaValidator extend ActiveSupport::Concern - ANDELA_EMAIL_REGEX = /@andela.co[m]?\z/ + ANDELA_EMAIL_REGEX = /[\w.]+@andela.co[m]?\z/ included do - validates :email, presence: true, format: {with: ANDELA_EMAIL_REGEX} + validates :email, presence: true, format: {with: ANDELA_EMAIL_REGEX, message: "Sign in with an Andela email address"} end end diff --git a/app/models/social_provider.rb b/app/models/social_provider.rb index a074195..bb9c98a 100644 --- a/app/models/social_provider.rb +++ b/app/models/social_provider.rb @@ -1,4 +1,5 @@ class SocialProvider < ActiveRecord::Base + include AndelaValidator belongs_to :user validates :uuid, presence: true validates :provider, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index cccd6f4..4f6aa2c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + include AndelaValidator has_many :comments, as: :comment_on has_many :tags, as: :subscriber has_many :questions @@ -9,9 +10,9 @@ class User < ActiveRecord::Base def self.from_omniauth(auth, user=nil) email_address = auth.info.email - grabbed = EMAIL_FORMAT.match(email_address).try(:[], :email) || email_address - to_check = "#{grabbed}%" - user = where(email: to_check).first_or_create do |u| + grabbed = EMAIL_FORMAT.match(email_address).try(:[], :email) + grabbed = grabbed ? "#{grabbed}%" : email_address + user = where("email LIKE :email", email: grabbed).first_or_create do |u| u.name= auth.info.name u.email= auth.info.email end