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 constantize Spree.user_class in UserClassHandle #5999

Merged
merged 1 commit into from
Dec 4, 2024
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
4 changes: 4 additions & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder of a present check makes sense?

Suggested change
fail "'Spree.user_class' has not been set yet." unless Spree.user_class_name
fail "'Spree.user_class' has not been set yet." unless Spree.user_class_name.present?

"::#{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
Loading