Skip to content

Commit faed08b

Browse files
committed
Use ::User for the user_class in the auth module
This removes the ability to set custom user class in kracken's config. So you will no longer be able to do this: ``` Kracken.config.user_class = Account ``` Instead it just assumes a AR model called `::User`. This was required because auto-reloading was broken in Rails 5. Changing the user model or logging in would cause the following exception to be raised: ``` A copy of User has been removed from the module tree but is still active! ``` Or even more helpful: ``` Cannot assign property of type User to object of type User ``` The module would store a reference the `User` class and that wouldn't be reinitalized between requests. To work around this, we just use the constant. Luckily all our Rails apps follow that convention.
1 parent fae3e75 commit faed08b

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

lib/kracken/config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Kracken
22
class Config
3-
attr_accessor :app_id, :app_secret
4-
attr_writer :provider_url, :user_class
3+
attr_accessor :app_id, :app_secret, :user_class
4+
attr_writer :provider_url
55

66
def initialize
77
@user_class = nil
@@ -12,7 +12,7 @@ def provider_url
1212
end
1313

1414
def user_class
15-
@user_class || ::User
15+
::User
1616
end
1717
end
1818
end

spec/kracken/controllers/token_authenticatable_spec.rb

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,13 @@ def authenticate_or_request_with_http_token(realm = nil)
125125
end
126126

127127
it "lazy loads the current user" do
128-
begin
129-
# Ensure we cannot lookup a user - doing so would raise an error
130-
org_user_class = Kracken.config.user_class
131-
user_class = double("AnyUserClass")
132-
Kracken.config.user_class = user_class
133-
134-
# Action under test
135-
a_controller.authenticate_user_with_token!
128+
# Action under test
129+
a_controller.authenticate_user_with_token!
136130

137-
# Make sure we perform the lookup as expected now
138-
expect(user_class).to receive(:find).with(:any_id).and_return(:user)
131+
# Make sure we perform the lookup as expected now
132+
expect(::User).to receive(:find).with(:any_id).and_return(:user)
139133

140-
expect(a_controller.current_user).to be :user
141-
ensure
142-
Kracken.config.user_class = org_user_class
143-
end
134+
expect(a_controller.current_user).to be :user
144135
end
145136
end
146137

0 commit comments

Comments
 (0)