Skip to content

Commit

Permalink
Defer user_model load so it can be a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
danhodge committed Dec 1, 2011
1 parent fc6af70 commit a582eec
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
12 changes: 3 additions & 9 deletions lib/clearance/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
module Clearance
class Configuration
attr_accessor :mailer_sender, :cookie_expiration, :password_strategy, :user_model_name
attr_accessor :mailer_sender, :cookie_expiration, :password_strategy, :user_model

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
@user_model || ::User
end
end

Expand All @@ -35,7 +29,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'
# config.user_model = MyNamespace::MyUser
# end
def self.configure
self.configuration ||= Configuration.new
Expand Down
7 changes: 3 additions & 4 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@

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

MyUser = Class.new
end

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

Expand Down

0 comments on commit a582eec

Please sign in to comment.