Skip to content

Commit

Permalink
Log a warning instead of raise if user class cannot be found
Browse files Browse the repository at this point in the history
If a user class has not been set yet we must not raise, so we do not interupt booting the application. Instead log a helpful warning.
  • Loading branch information
tvdeyen committed Oct 21, 2020
1 parent 27fe9d0 commit 385d120
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
18 changes: 10 additions & 8 deletions lib/alchemy/auth_accessors.rb
Expand Up @@ -91,21 +91,23 @@ def self.user_class
@@user_class_name.constantize
rescue NameError => e
if e.message =~ /#{Regexp.escape(@@user_class_name)}/
abort <<-MSG.strip_heredoc
Rails.logger.warn <<~MSG
#{e.message}
#{e.backtrace.join("\n")}
AlchemyCMS cannot find any user class!
AlchemyCMS cannot find any user class!
Please add a user class and tell Alchemy about it:
Please add a user class and tell Alchemy about it:
# config/initializers/alchemy.rb
Alchemy.user_class_name = 'MyUser'
# config/initializers/alchemy.rb
Alchemy.user_class_name = 'MyUser'
Or add the `alchemy-devise` gem to your Gemfile:
Or add the `alchemy-devise` gem to your Gemfile:
bundle add alchemy-devise
bundle add alchemy-devise
Original error message: #{e.message}
MSG
nil
else
raise e
end
Expand Down
66 changes: 63 additions & 3 deletions spec/libraries/auth_accessors_spec.rb
Expand Up @@ -9,7 +9,6 @@ class MyCustomUser
describe "AuthAccessors" do
describe ".user_class_name" do
before do
# prevent memoization
Alchemy.user_class_name = "DummyClassName"
end

Expand All @@ -23,9 +22,65 @@ class MyCustomUser
it "returns user_class_name with :: prefix" do
expect(Alchemy.user_class_name).to eq("::DummyClassName")
end
end

after do
Alchemy.user_class_name = "DummyClassName"
describe ".user_class" do
context "with no custom user_class_name set" do
before do
Alchemy.user_class_name = "User"
end

context "and the default user class exists" do
class ::User; end

it "returns the default user class" do
expect(Alchemy.user_class).to be(::User)
end
end

context "and the default user class does not exist" do
before do
if Object.constants.include?(:User)
Object.send(:remove_const, :User)
end
end

it "returns nil" do
expect(Alchemy.user_class).to be_nil
end

it "logs warning" do
expect(Rails.logger).to receive(:warn).with(a_string_matching("AlchemyCMS cannot find any user class"))
Alchemy.user_class
end
end
end

context "with custom user_class_name set" do
before do
Alchemy.user_class_name = "DummyUser"
end

context "and the custom User class exists" do
it "returns the custom user class" do
expect(Alchemy.user_class).to be(::DummyUser)
end
end

context "and the custom user class does not exist" do
before do
Alchemy.user_class_name = "NoUser"
end

it "returns nil" do
expect(Alchemy.user_class).to be_nil
end

it "logs warning" do
expect(Rails.logger).to receive(:warn).with(a_string_matching("AlchemyCMS cannot find any user class"))
Alchemy.user_class
end
end
end
end

Expand All @@ -50,5 +105,10 @@ class MyCustomUser
expect(Alchemy.logout_method).to eq("delete")
end
end

after do
Alchemy.user_class_name = "DummyUser"
Alchemy.class_variable_set("@@user_class", nil)
end
end
end

0 comments on commit 385d120

Please sign in to comment.