Skip to content
This repository has been archived by the owner on Mar 21, 2019. It is now read-only.

Commit

Permalink
adds rspec-mocks and moves referral code logic into users helper
Browse files Browse the repository at this point in the history
  • Loading branch information
phamdt committed Feb 28, 2016
1 parent 68dd29c commit 0c7eb3f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 82 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ group :development, :test do
gem 'pry'
# To use debugger
# gem 'debugger'
gem 'rspec-rails', '~> 3.1.0'
gem 'rspec-rails', '3.4.1'
gem 'rspec-mocks', '3.4.1'
end

gem 'delayed_job_active_record'
Expand Down
110 changes: 56 additions & 54 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ GEM
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.2.1)
activeadmin (0.6.3)
activeadmin (0.6.6)
arbre (~> 1.0)
bourbon (>= 1.0.0, < 4)
devise (>= 1.5.4, < 4, != 2.2.2, != 2.2.1, != 2.2.0, != 2.1.2, != 2.1.1, != 2.1.0, != 2.0.4, != 2.0.3, != 2.0.2, != 2.0.1, != 2.0.0)
devise (>= 1.5.4, < 4)
formtastic (~> 2.0)
inherited_resources (~> 1.3)
jquery-rails (>= 1.0.0, < 3)
Expand All @@ -39,28 +39,28 @@ GEM
activesupport (3.2.11)
i18n (~> 0.6)
multi_json (~> 1.0)
arbre (1.0.2)
arbre (1.0.3)
activesupport (>= 3.0.0)
arel (3.0.3)
bcrypt (3.1.9)
bourbon (3.2.3)
bcrypt (3.1.10)
bourbon (3.2.4)
sass (~> 3.2)
thor
builder (3.0.4)
coderay (1.1.0)
coderay (1.1.1)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
coffee-script (2.3.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.8.0)
delayed_job (4.0.4)
activesupport (>= 3.0, < 4.2)
delayed_job_active_record (4.0.2)
activerecord (>= 3.0, < 4.2)
delayed_job (>= 3.0, < 4.1)
devise (3.4.1)
coffee-script-source (1.10.0)
delayed_job (4.1.1)
activesupport (>= 3.0, < 5.0)
delayed_job_active_record (4.1.0)
activerecord (>= 3.0, < 5)
delayed_job (>= 3.0, < 5)
devise (3.5.6)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
Expand All @@ -69,28 +69,28 @@ GEM
warden (~> 1.2.3)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.2.2)
execjs (2.6.0)
formtastic (2.3.1)
actionpack (>= 3.0)
has_scope (0.6.0.rc)
has_scope (0.6.0)
actionpack (>= 3.2, < 5)
activesupport (>= 3.2, < 5)
hike (1.2.3)
i18n (0.6.11)
inherited_resources (1.5.1)
actionpack (>= 3.2, < 4.2)
i18n (0.7.0)
inherited_resources (1.6.0)
actionpack (>= 3.2, < 5)
has_scope (~> 0.6.0.rc)
railties (>= 3.2, < 4.2)
responders (~> 1.0)
railties (>= 3.2, < 5)
responders
journey (1.0.4)
jquery-rails (2.3.0)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
kaminari (0.16.1)
json (1.8.3)
kaminari (0.16.3)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
kgio (2.9.2)
kgio (2.10.0)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
Expand All @@ -102,22 +102,22 @@ GEM
polyamorous (~> 0.5.0)
method_source (0.8.2)
mime-types (1.25.1)
multi_json (1.10.1)
multi_json (1.11.2)
orm_adapter (0.5.0)
pg (0.17.1)
pg (0.18.4)
polyamorous (0.5.0)
activerecord (~> 3.0)
polyglot (0.3.5)
pry (0.10.1)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.4.5)
rack-cache (1.2)
rack (1.4.7)
rack-cache (1.6.1)
rack (>= 0.4)
rack-ssl (1.3.4)
rack
rack-test (0.6.2)
rack-test (0.6.3)
rack (>= 1.0)
rails (3.2.11)
actionmailer (= 3.2.11)
Expand All @@ -134,29 +134,30 @@ GEM
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
raindrops (0.13.0)
rake (10.4.0)
raindrops (0.15.0)
rake (10.5.0)
rdoc (3.12.2)
json (~> 1.4)
responders (1.1.2)
railties (>= 3.2, < 4.2)
rspec-core (3.1.7)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
rspec-core (3.4.3)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.1.0)
rspec-mocks (3.1.3)
rspec-support (~> 3.1.0)
rspec-rails (3.1.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
rspec-mocks (~> 3.1.0)
rspec-support (~> 3.1.0)
rspec-support (3.1.2)
sass (3.4.9)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.1)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
sass (3.4.21)
sass-rails (3.2.6)
railties (~> 3.2.0)
sass (>= 3.1.10)
Expand All @@ -168,20 +169,20 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
thor (0.19.1)
thread_safe (0.3.4)
thread_safe (0.3.5)
tilt (1.4.1)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.42)
uglifier (2.5.3)
tzinfo (0.3.46)
uglifier (2.7.2)
execjs (>= 0.3.0)
json (>= 1.8.0)
unicorn (4.8.3)
unicorn (5.0.1)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
warden (1.2.3)
warden (1.2.6)
rack (>= 1.0)

PLATFORMS
Expand All @@ -194,7 +195,8 @@ DEPENDENCIES
pg
pry
rails (= 3.2.11)
rspec-rails (~> 3.1.0)
rspec-mocks (= 3.4.1)
rspec-rails (= 3.4.1)
sass-rails (~> 3.2.3)
uglifier (>= 1.0.3)
unicorn
7 changes: 5 additions & 2 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module UsersHelper
def self.replace_if_collision(collision, referral_code)
while !collision.nil?
def self.unused_referral_code
referral_code = SecureRandom.hex(5)
collision = User.find_by_referral_code(referral_code)

until collision.nil?
referral_code = SecureRandom.hex(5)
collision = User.find_by_referral_code(referral_code)
end
Expand Down
24 changes: 13 additions & 11 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
require 'users_helper'

class User < ActiveRecord::Base

belongs_to :referrer, class_name: 'User', foreign_key: 'referrer_id'
has_many :referrals, class_name: 'User', foreign_key: 'referrer_id'

attr_accessible :email

validates :email, presence: true, uniqueness: true, format: { with: /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i, message: 'Invalid email format.' }
validates :email, presence: true, uniqueness: true, format: {
with: /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i,
message: 'Invalid email format.'
}
validates :referral_code, uniqueness: true

before_create :create_referral_code
Expand All @@ -18,36 +20,36 @@ class User < ActiveRecord::Base
'count' => 5,
'html' => 'Shave<br>Cream',
'class' => 'two',
'image' => ActionController::Base.helpers.asset_path('refer/[email protected]')
'image' => ActionController::Base.helpers.asset_path(
'refer/[email protected]')
},
{
'count' => 10,
'html' => 'Truman Handle<br>w/ Blade',
'class' => 'three',
'image' => ActionController::Base.helpers.asset_path('refer/[email protected]')
'image' => ActionController::Base.helpers.asset_path(
'refer/[email protected]')
},
{
'count' => 25,
'html' => 'Winston<br>Shave Set',
'class' => 'four',
'image' => ActionController::Base.helpers.asset_path('refer/[email protected]')
'image' => ActionController::Base.helpers.asset_path(
'refer/[email protected]')
},
{
'count' => 50,
'html' => 'One Year<br>Free Blades',
'class' => 'five',
'image' => ActionController::Base.helpers.asset_path('refer/[email protected]')
'image' => ActionController::Base.helpers.asset_path(
'refer/[email protected]')
}
]

private

def create_referral_code
referral_code = SecureRandom.hex(5)
@collision = User.find_by_referral_code(referral_code)

self.referral_code = UsersHelper.replace_if_collision(
@collision, referral_code)
self.referral_code = UsersHelper.unused_referral_code
end

def send_welcome_email
Expand Down
24 changes: 10 additions & 14 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

RSpec.describe User, type: :model do
it 'should error when saving without email' do
expect {
expect do
user = User.new
user.save!
}.to raise_error(ActiveRecord::RecordInvalid)
end.to raise_error(ActiveRecord::RecordInvalid)
end

it 'should error when saving with malformed email' do
expect {
user = User.create!(email: 'notanemail')
}.to raise_error(ActiveRecord::RecordInvalid)
expect do
User.create!(email: 'notanemail')
end.to raise_error(ActiveRecord::RecordInvalid)
end

it 'should generate a referral code on creation' do
Expand All @@ -32,19 +32,15 @@
end

RSpec.describe UsersHelper do
describe 'replace_if_collision' do
describe 'unused_referral_code' do
it 'should return the same referral code if there is no collision' do
referral_code = SecureRandom.hex(5)
@collision = nil
candidate = UsersHelper.replace_if_collision(@collision, referral_code)
expect(referral_code).to eq(candidate)
expect(User).to receive(:find_by_referral_code).and_return(nil)
UsersHelper.unused_referral_code
end

it 'should return a new referral code if there is a collision' do
referral_code = SecureRandom.hex(5)
@collision = referral_code
candidate = UsersHelper.replace_if_collision(@collision, referral_code)
expect(referral_code).to_not eq(candidate)
expect(User).to receive(:find_by_referral_code).exactly(2).times.and_return('collision', nil)
UsersHelper.unused_referral_code
end
end
end

0 comments on commit 0c7eb3f

Please sign in to comment.