Skip to content

Commit

Permalink
added validator for email address to only permit Andelans
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreoluwa Akinniranye committed Feb 3, 2016
1 parent d39b544 commit b699687
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/models/concerns/andela_validator.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/models/social_provider.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class SocialProvider < ActiveRecord::Base
include AndelaValidator
belongs_to :user
validates :uuid, presence: true
validates :provider, presence: true
Expand Down
7 changes: 4 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class User < ActiveRecord::Base
include AndelaValidator
has_many :comments, as: :comment_on
has_many :tags, as: :subscriber
has_many :questions
Expand All @@ -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
Expand Down

0 comments on commit b699687

Please sign in to comment.