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

Fix Alchemy.user_class_name constant conflict #1724

Merged
merged 5 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 18 additions & 12 deletions lib/alchemy/auth_accessors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
# Alchemy.register_ability MyCustom::Ability
#
module Alchemy
mattr_accessor :user_class_name,
:user_class_primary_key,
mattr_accessor :user_class_primary_key,
:current_user_method,
:signup_path,
:login_path,
Expand Down Expand Up @@ -73,17 +72,24 @@ module Alchemy
# # config/initializers/alchemy.rb
# Alchemy.user_class_name = 'Admin'
#
def self.user_class
@@user_class ||= begin
if @@user_class_name.is_a?(String)
@@user_class_name.constantize
else
raise TypeError, 'Alchemy.user_class_name must be a String, not a Class.'
end

# Prefix with :: when getting to avoid constant name conflicts
def self.user_class_name
if !@@user_class_name.is_a?(String)
raise TypeError, 'Alchemy.user_class_name must be a String, not a Class.'
end
rescue NameError => e
if e.message =~ /#{Regexp.escape(@@user_class_name)}/
abort <<-MSG.strip_heredoc
"::#{@@user_class_name}".freeze
mickenorlen marked this conversation as resolved.
Show resolved Hide resolved
end

def self.user_class_name=(user_class_name)
@@user_class_name = user_class_name
end

def self.user_class
@@user_class ||= @@user_class_name.constantize
rescue NameError => e
mickenorlen marked this conversation as resolved.
Show resolved Hide resolved
if e.message =~ /#{Regexp.escape(@@user_class_name)}/
abort <<-MSG.strip_heredoc

AlchemyCMS cannot find any user class!

Expand Down
14 changes: 9 additions & 5 deletions spec/libraries/auth_accessors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ class MyCustomUser
end

describe 'AuthAccessors' do
describe '.user_class' do
describe '.user_class_name' do
before do
# prevent memoization
Alchemy.class_variable_set('@@user_class', nil)
Alchemy.user_class_name = 'DummyClassName'
end

it "raises error if user_class_name is not a String" do
Alchemy.user_class_name = MyCustomUser
Alchemy.user_class_name = DummyUser
expect {
Alchemy.user_class
Alchemy.user_class_name
}.to raise_error(TypeError)
end

it "returns user_class_name with :: prefix" do
expect(Alchemy.user_class_name).to eq("::DummyClassName")
end

after do
Alchemy.user_class_name = 'DummyUser'
Alchemy.user_class_name = 'DummyClassName'
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/libraries/userstamp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
describe '.user_class' do
it "injects userstamp class methods" do
expect(Alchemy.user_class).to respond_to(:stamper_class_name)
expect(Alchemy.user_class.stamper_class_name).to eq(:DummyUser)
expect(Alchemy.user_class.stamper_class_name).to eq(:"::DummyUser")
end
end
end