From 16d10dcacce7b79edf210fca7c841df42a755d6b Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Wed, 4 Dec 2024 09:50:31 +0100 Subject: [PATCH] Do not constantize Spree.user_class in UserClassHandle `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 355ba7a54c549ecf6501d25d1953920967caf8f7) --- core/lib/spree/core.rb | 4 ++ core/lib/spree/user_class_handle.rb | 4 +- core/spec/lib/spree/user_class_handle_spec.rb | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 core/spec/lib/spree/user_class_handle_spec.rb diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb index d1909197a3d..da95496482b 100644 --- a/core/lib/spree/core.rb +++ b/core/lib/spree/core.rb @@ -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 diff --git a/core/lib/spree/user_class_handle.rb b/core/lib/spree/user_class_handle.rb index 9df1da303a2..8e01cd83446 100644 --- a/core/lib/spree/user_class_handle.rb +++ b/core/lib/spree/user_class_handle.rb @@ -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 diff --git a/core/spec/lib/spree/user_class_handle_spec.rb b/core/spec/lib/spree/user_class_handle_spec.rb new file mode 100644 index 00000000000..b9055b9062d --- /dev/null +++ b/core/spec/lib/spree/user_class_handle_spec.rb @@ -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