Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not abort if user class cannot be found #1950

Merged
merged 4 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/alchemy/auth_accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +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 or, if you don't want
to create your own class, add the `alchemy-devise` gem to your Gemfile.
Please add a user class and tell Alchemy about it:

bundle add alchemy-devise
# config/initializers/alchemy.rb
Alchemy.user_class_name = 'MyUser'

Or add the `alchemy-devise` gem to your Gemfile:

bundle add alchemy-devise

MSG
nil
else
raise e
end
Expand Down
9 changes: 7 additions & 2 deletions lib/alchemy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ class Engine < Rails::Engine
end
end

config.after_initialize do
require_relative "./userstamp"
initializer "alchemy.userstamp" do
if Alchemy.user_class
ActiveSupport.on_load(:active_record) do
Alchemy.user_class.model_stamper
Alchemy.user_class.stampable(stamper_class_name: Alchemy.user_class_name)
end
end
end
end
end
12 changes: 0 additions & 12 deletions lib/alchemy/userstamp.rb

This file was deleted.

66 changes: 63 additions & 3 deletions spec/libraries/auth_accessors_spec.rb
Original file line number Diff line number Diff line change
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