Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Simplify and reduce footprint of `Variant#options_text` (#13449)

Damian Legawiecd04cb61d03a0
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index ff4bd88d34..34ce0c670c 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -255,7 +255,11 @@ module Spree
# Returns the options text of the variant.
# @return [String] the options text of the variant
def options_text
- @options_text ||= Spree::Variants::OptionsPresenter.new(self).to_sentence
+ @options_text ||= if option_values.loaded?
+ option_values.sort_by { |ov| ov.option_type.position }.map { |ov| "#{ov.option_type.presentation}: #{ov.presentation}" }.to_sentence(words_connector: ', ', two_words_connector: ', ')
+ else
+ option_values.includes(:option_type).joins(:option_type).order("#{Spree::OptionType.table_name}.position").map { |ov| "#{ov.option_type.presentation}: #{ov.presentation}" }.to_sentence(words_connector: ', ', two_words_connector: ', ')
+ end
end
# Returns the exchange name of the variant.
diff --git a/core/app/presenters/spree/variants/options_presenter.rb b/core/app/presenters/spree/variants/options_presenter.rb
index 55f47441d9..beede2f5da 100644
--- a/core/app/presenters/spree/variants/options_presenter.rb
+++ b/core/app/presenters/spree/variants/options_presenter.rb
@@ -8,6 +8,7 @@ module Spree
delegate :option_values, to: :variant
def initialize(variant)
+ Spree::Deprecation.warn('Spree::Variants::OptionsPresenter is deprecated and will be removed in Spree 5.5. Please use Spree::Variant#options_text instead.')
@variant = variant
end
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index 80d872a8ce..87f3935fa3 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -761,17 +761,20 @@ describe Spree::Variant, type: :model do
describe '#options_text' do
subject(:options_text) { variant.options_text }
- let(:variant) { build :variant }
- let(:fake_presenter) { double :fake_presenter }
+ context 'when the variant has no option values' do
+ let(:variant) { build(:variant, option_values: []) }
- before do
- allow(Spree::Variants::OptionsPresenter).to receive(:new).with(variant).and_return(fake_presenter)
+ it 'returns an empty string' do
+ expect(options_text).to eql ''
+ end
end
- it 'calls Spree::Variants::OptionsPresenter' do
- expect(fake_presenter).to receive(:to_sentence)
+ context 'when the variant has option values' do
+ let(:variant) { build(:variant, option_values: [create(:option_value, name: 'Foo', presentation: 'Foo', option_type: create(:option_type, position: 2, name: 'Foo Type', presentation: 'Foo Type'))]) }
- options_text
+ it 'returns the options text of the variant' do
+ expect(options_text).to eql 'Foo Type: Foo'
+ end
end
end