Commit 0b7b7a8

mo khan <mo@mokhan.ca>
2014-11-15 15:21:40
add email validation.
1 parent 22fbd2b
Changed files (4)
app/models/user.rb
@@ -1,5 +1,7 @@
 class User < ActiveRecord::Base
   has_many :sessions
   has_many :videos
+  validates :email, email: true
+
   has_secure_password
 end
spec/models/user_spec.rb
@@ -19,4 +19,31 @@ describe User do
       expect(user.authenticate('wrong')).to be_falsey
     end
   end
+
+  context "validations" do
+    subject { build(:user) }
+
+    context "when the email is blank" do
+      it 'produces an error' do
+        subject.email = ''
+        expect(subject).to_not be_valid
+        expect(subject.errors[:email]).to_not be_empty
+      end
+    end
+
+    context "when the email is invalid" do
+      it 'produces an error when' do
+        subject.email = 'blah'
+        expect(subject).to_not be_valid
+        expect(subject.errors[:email]).to_not be_empty
+      end
+    end
+
+    context 'when the email is legit' do
+      it 'is valid' do
+        expect(subject).to be_valid
+        expect(subject.errors[:email]).to be_empty
+      end
+    end
+  end
 end
Gemfile
@@ -41,6 +41,7 @@ gem 'dotenv'
 gem 'i18n-tasks'
 gem 'rack-ssl-enforcer'
 gem 'puma'
+gem 'email_validator'
 
 # Use unicorn as the app server
 # gem 'unicorn'
Gemfile.lock
@@ -76,6 +76,8 @@ GEM
       json
       thread
       thread_safe
+    email_validator (1.4.0)
+      activemodel
     ember-data-source (1.0.0.beta.11)
       ember-source
     ember-source (1.8.1)
@@ -215,6 +217,7 @@ DEPENDENCIES
   dalli
   database_cleaner
   dotenv
+  email_validator
   ember-rails!
   ember-source
   factory_girl_rails