Skip to content

Commit

Permalink
Do not constantize Spree.user_class in UserClassHandle
Browse files Browse the repository at this point in the history
`Spree.user_class` will constantize the configured user class. If called
during Rails start-up, this will autoload the configured user class,
`Spree.user_class`. We can do without the autoloading by directly getting the name
of the class from a new method `Spree.user_class_name`.

(cherry picked from commit 355ba7a)
  • Loading branch information
mamhoff committed Dec 4, 2024
1 parent 9b23bf6 commit 16d10dc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def self.user_class
end
end

def self.user_class_name
@@user_class
end

# Load the same version defaults for all available Solidus components
#
# @see Spree::Preferences::Configuration#load_defaults
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/user_class_handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class UserClassHandle
# @return [String] the name of the user class as a string.
# @raise [RuntimeError] if Spree.user_class is nil
def to_s
fail "'Spree.user_class' has not been set yet." unless Spree.user_class
"::#{Spree.user_class}"
fail "'Spree.user_class' has not been set yet." unless Spree.user_class_name
"::#{Spree.user_class_name}"
end
end
end
37 changes: 37 additions & 0 deletions core/spec/lib/spree/user_class_handle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "spec_helper"
require "spree/core"
require "spree/user_class_handle"

RSpec.describe Spree::UserClassHandle do
describe "#to_s" do
around do |example|
@prev_user_class = Spree.user_class_name
example.run
Spree.user_class = @prev_user_class
end

subject { described_class.new.to_s }

context "when Spree.user_class is nil" do
before do
Spree.user_class = nil
end

it "is expected to fail" do
expect { subject }.to raise_error(RuntimeError, "'Spree.user_class' has not been set yet.")
end
end

context "when Spree.user_class is not nil" do
before do
Spree.user_class = "Spree::User"
end

it "is expected to return the user class as a string" do
expect(subject).to eq("::Spree::User")
end
end
end
end

0 comments on commit 16d10dc

Please sign in to comment.