Skip to content

Commit

Permalink
validating email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Oreoluwa Akinniranye committed Feb 3, 2016
1 parent 495d898 commit d39b544
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/models/concerns/andela_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module AndelaValidator
extend ActiveSupport::Concern

ANDELA_EMAIL_REGEX = /@andela.co[m]?\z/
included do
validates :email, presence: true, format: {with: ANDELA_EMAIL_REGEX}
end

end
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ class User < ActiveRecord::Base
has_many :answers
has_many :social_providers
has_many :tokens
EMAIL_FORMAT= /(?<email>[.\w]+@andela).co[m]?\z/

def self.from_omniauth(auth, user=nil)
user = where(email: auth.info.email).first_or_create do |u|
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|
u.name= auth.info.name
u.email= auth.info.email
end
Expand Down
7 changes: 2 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@
#
# It's strongly recommended that you check this file into your version control system.


ActiveRecord::Schema.define(version: 20160201170223) do

create_table "answers", force: :cascade do |t|
t.integer "user_id"
t.integer "question_id"
t.string "content"
t.integer "votes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "comments", force: :cascade do |t|
t.integer "user_id"
t.string "content"
t.integer "votes"
t.integer "comment_on_id"
t.string "comment_on_type"
t.datetime "created_at", null: false
Expand All @@ -39,7 +36,6 @@
t.integer "user_id"
t.string "title"
t.string "content"
t.integer "votes"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Expand All @@ -55,7 +51,7 @@
t.string "token"
t.string "profile_picture"
t.string "profile_url"
t.string "profile_email"
t.string "email"
end

add_index "social_providers", ["user_id"], name: "index_social_providers_on_user_id"
Expand Down Expand Up @@ -103,4 +99,5 @@

add_index "votes", ["user_id"], name: "index_votes_on_user_id"
add_index "votes", ["voteable_type", "voteable_id"], name: "index_votes_on_voteable_type_and_voteable_id"

end

0 comments on commit d39b544

Please sign in to comment.