diff --git a/lib/alchemy/auth_accessors.rb b/lib/alchemy/auth_accessors.rb index 6cc8bd877e..be5b7d660d 100644 --- a/lib/alchemy/auth_accessors.rb +++ b/lib/alchemy/auth_accessors.rb @@ -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 diff --git a/spec/libraries/auth_accessors_spec.rb b/spec/libraries/auth_accessors_spec.rb index a8f521a07b..16f34492e6 100644 --- a/spec/libraries/auth_accessors_spec.rb +++ b/spec/libraries/auth_accessors_spec.rb @@ -9,7 +9,6 @@ class MyCustomUser describe "AuthAccessors" do describe ".user_class_name" do before do - # prevent memoization Alchemy.user_class_name = "DummyClassName" end @@ -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 @@ -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