Skip to content

Commit

Permalink
Merge pull request #6002 from solidusio/backport/v4.2/pr-5999
Browse files Browse the repository at this point in the history
[v4.2] Do not constantize Spree.user_class in UserClassHandle
  • Loading branch information
tvdeyen authored Dec 4, 2024
2 parents ad1418e + 1ff9a7d commit 2b5ddb9
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 @@ -42,6 +42,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 2b5ddb9

Please sign in to comment.