Skip to content

Commit

Permalink
Allow User model to be reloaded in development
Browse files Browse the repository at this point in the history
When a `user_model` is configured in a Clearance initializer, a
reference to that class is immediately saved off. If that class is
changed, Clearance will not know to automatically reload the class as
Rails does automatically for classes in development.

This change introduces a `to_prepare` block to the Engine that is
responsible for forcing the configured user class to be reloaded.
`to_prepare` runs once per request in development and only at startup in
other environments.
  • Loading branch information
derekprior committed Mar 27, 2015
1 parent 49e45c3 commit 90dbeb9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/clearance/configuration.rb
Expand Up @@ -133,6 +133,17 @@ def user_id_parameter
def routes_enabled?
@routes
end

# Reloads the clearance user model class.
# This is called from the Clearance engine to reload the configured
# user class during each request while in development mode, but only once
# in production.
# @private
def reload_user_model
if @user_model.present?
@user_model = @user_model.to_s.constantize
end
end
end

# @return [Clearance::Configuration] Clearance's current configuration
Expand Down
4 changes: 4 additions & 0 deletions lib/clearance/engine.rb
Expand Up @@ -11,5 +11,9 @@ class Engine < Rails::Engine
ActionDispatch::ParamsParser,
Clearance::RackSession
)

config.to_prepare do
Clearance.configuration.reload_user_model
end
end
end
13 changes: 13 additions & 0 deletions spec/configuration_spec.rb
Expand Up @@ -162,4 +162,17 @@
expect(Clearance.configuration.routes_enabled?).to be false
end
end

describe "#reload_user_model" do
it "returns the user model class if one has already been configured" do
ConfiguredUser = Class.new
Clearance.configure { |config| config.user_model = ConfiguredUser }

expect(Clearance.configuration.reload_user_model).to eq ConfiguredUser
end

it "returns nil if the user_model has not been configured" do
expect(Clearance.configuration.reload_user_model).to be_nil
end
end
end

0 comments on commit 90dbeb9

Please sign in to comment.