Skip to content

Commit

Permalink
Refactor with Concern
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-huang committed Feb 27, 2014
1 parent 7a9d389 commit c22d470
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
9 changes: 2 additions & 7 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
class Invitation < ActiveRecord::Base
include Tokenable

belongs_to :inviter, class_name: "User"

validates_presence_of :recipient_name, :recipient_email, :message

before_create :generate_token

def generate_token
self.token = SecureRandom.urlsafe_base64
end
end
8 changes: 2 additions & 6 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
include Tokenable

validates_presence_of :email, :password, :full_name
validates_uniqueness_of :email
has_many :queue_items, -> { order :position }
Expand All @@ -8,8 +10,6 @@ class User < ActiveRecord::Base

has_secure_password validations: false

before_create :generate_token

def normalize_queue_item_positions
queue_items.each_with_index do |queue_item, index|
queue_item.update_attributes(position: index+1)
Expand All @@ -31,8 +31,4 @@ def follow(another_user)
def can_follow?(another_user)
!(self.follows?(another_user) || self == another_user)
end

def generate_token
self.token = SecureRandom.urlsafe_base64
end
end
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ class Application < Rails::Application
g.orm :active_record
g.template_engine :haml
end
config.autoload_paths << "#{Rails.root}/lib"
end
end
11 changes: 11 additions & 0 deletions lib/tokenable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Tokenable
extend ActiveSupport::Concern

included do
before_create :generate_token

def generate_token
self.token = SecureRandom.urlsafe_base64
end
end
end
4 changes: 4 additions & 0 deletions spec/models/invitation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
it { should validate_presence_of(:recipient_name) }
it { should validate_presence_of(:recipient_email) }
it { should validate_presence_of(:message) }

it_behaves_like "tokenable" do
let(:object) { Fabricate(:invitation) }
end
end
5 changes: 2 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
it { should have_many(:queue_items).order(:position)}
it { should have_many(:reviews).order("created_at DESC")}

it "generates a random token when the user is created" do
alice = Fabricate(:user)
expect(alice.token).to be_present
it_behaves_like "tokenable" do
let(:object) { Fabricate(:user) }
end

describe "#queued_video?" do
Expand Down
6 changes: 6 additions & 0 deletions spec/support/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
expect(response).to redirect_to sign_in_path
end
end

shared_examples "tokenable" do
it "generates a random token when the user is created" do
expect(object.token).to be_present
end
end

0 comments on commit c22d470

Please sign in to comment.