Skip to content

Commit

Permalink
Optional config param 'user_model_name' added
Browse files Browse the repository at this point in the history
  • Loading branch information
danhodge committed Dec 1, 2011
1 parent e18fee0 commit fc6af70
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 16 deletions.
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ task :all => ["appraisal:install"] do |t|
exec('rake appraisal spec cucumber')
end

RSpec::Core::RakeTask.new do |t|
t.pattern = 'spec/**/*_spec.rb'
end
RSpec::Core::RakeTask.new(:spec)

Cucumber::Rake::Task.new(:cucumber) do |t|
t.fork = true
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/clearance/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def new
end

def create
if user = ::User.find_by_email(params[:password][:email])
if user = Clearance.configuration.user_model.find_by_email(
params[:password][:email])
user.forgot_password!
::ClearanceMailer.change_password(user).deliver
render :template => 'passwords/create'
Expand All @@ -21,13 +22,13 @@ def create
end

def edit
@user = ::User.find_by_id_and_confirmation_token(
@user = Clearance.configuration.user_model.find_by_id_and_confirmation_token(
params[:user_id], params[:token])
render :template => 'passwords/edit'
end

def update
@user = ::User.find_by_id_and_confirmation_token(
@user = Clearance.configuration.user_model.find_by_id_and_confirmation_token(
params[:user_id], params[:token])

if @user.update_password(params[:user][:password])
Expand All @@ -49,7 +50,7 @@ def forbid_missing_token
end

def forbid_non_existent_user
unless ::User.find_by_id_and_confirmation_token(
unless Clearance.configuration.user_model.find_by_id_and_confirmation_token(
params[:user_id], params[:token])
flash_failure_when_forbidden
render :template => 'passwords/new'
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/clearance/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ class Clearance::UsersController < ApplicationController
before_filter :redirect_to_root, :only => [:new, :create], :if => :signed_in?

def new
@user = ::User.new(params[:user])
@user = Clearance.configuration.user_model.new(params[:user])
render :template => 'users/new'
end

def create
@user = ::User.new(params[:user])
@user = Clearance.configuration.user_model.new(params[:user])
if @user.save
sign_in(@user)
redirect_back_or(url_after_create)
Expand Down
4 changes: 2 additions & 2 deletions lib/clearance/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def sign_out
# @example
# @user = authenticate(params)
def authenticate(params)
::User.authenticate(params[:session][:email],
params[:session][:password])
Clearance.configuration.user_model.authenticate(params[:session][:email],
params[:session][:password])
end

# Deny the user access if they are signed out.
Expand Down
13 changes: 12 additions & 1 deletion lib/clearance/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
module Clearance
class Configuration
attr_accessor :mailer_sender, :cookie_expiration, :password_strategy
attr_accessor :mailer_sender, :cookie_expiration, :password_strategy, :user_model_name

def initialize
@mailer_sender = 'donotreply@example.com'
@cookie_expiration = lambda { 1.year.from_now.utc }
@user_model_name = '::User'
end

def user_model_name=(model_name)
@user_model_name = model_name
@user_model = nil
end

def user_model
@user_model ||= @user_model_name.constantize
end
end

Expand All @@ -25,6 +35,7 @@ class << self
# config.mailer_sender = 'me@example.com'
# config.cookie_expiration = lambda { 2.weeks.from_now.utc }
# config.password_strategy = MyPasswordStrategy
# config.user_model_name = 'MyNamespace::MyUser'
# end
def self.configure
self.configuration ||= Configuration.new
Expand Down
34 changes: 34 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe Clearance::Configuration do
describe "when no user_model_name is specified" do
before do
Clearance.configure do |config|
end
end

it "defaults to User" do
Clearance.configuration.user_model.should == ::User
end
end

describe "when a custom user_model_name is specified" do
before do
Clearance.configure do |config|
config.user_model_name = 'MyUser'
end

MyUser = Class.new
end

after do
Clearance.configure do |config|
config.user_model_name = '::User'
end
end

it "is used instead of User" do
Clearance.configuration.user_model.should == ::MyUser
end
end
end
2 changes: 1 addition & 1 deletion spec/controllers/passwords_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
describe "with incorrect email address" do
before do
email = "user1@example.com"
(::User.exists?(['email = ?', email])).should_not be
(Clearance.configuration.user_model.exists?(['email = ?', email])).should_not be
ActionMailer::Base.deliveries.clear
@user.reload.confirmation_token.should == @user.confirmation_token

Expand Down
6 changes: 3 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@
end

it "is authenticated with correct email and password" do
(::User.authenticate(@user.email, @password)).should be
(Clearance.configuration.user_model.authenticate(@user.email, @password)).should be
@user.should be_authenticated(@password)
end

it "is authenticated with correct uppercased email and correct password" do
(::User.authenticate(@user.email.upcase, @password)).should be
(Clearance.configuration.user_model.authenticate(@user.email.upcase, @password)).should be
@user.should be_authenticated(@password)
end

it "is authenticated with incorrect credentials" do
(::User.authenticate(@user.email, 'bad_password')).should_not be
(Clearance.configuration.user_model.authenticate(@user.email, 'bad_password')).should_not be
@user.should_not be_authenticated('bad password')
end
end
Expand Down

0 comments on commit fc6af70

Please sign in to comment.