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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Improve performance of `Variant#price_in`, add `Variant#on_sale?`
Damian Legawiec9e8814d31dd6
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 3c60c75519..5ff8f2d785 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -7,7 +7,7 @@ module Spreeinclude Spree::Metadatainclude Spree::Variant::Webhooks- MEMOIZED_METHODS = %w(purchasable in_stock backorderable tax_category options_text compare_at_price)+ MEMOIZED_METHODS = %w(purchasable in_stock on_sale backorderable tax_category options_text compare_at_price)belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product', inverse_of: :variantsbelongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true@@ -66,7 +66,7 @@ module Spreeafter_create :create_stock_itemsafter_create :set_master_out_of_stock, unless: :is_master?-+ after_commit :clear_line_items_cache, on: :updateafter_touch :clear_in_stock_cachescope :in_stock, -> { joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand > ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }@@ -283,19 +283,26 @@ module Spreedef price_in(currency)currency = currency&.upcase- find_or_build_price = lambda do- if prices.loaded?- prices.detect { |price| price.currency == currency } || prices.build(currency: currency)- else- prices.find_or_initialize_by(currency: currency)- end- end- Rails.cache.fetch("spree/prices/#{cache_key_with_version}/price_in/#{currency}") do- find_or_build_price.call+ price = if prices.loaded? && prices.any?+ prices.detect { |p| p.currency == currency }+ else+ prices.find_by(currency: currency)+ end++ if price.nil?+ return Spree::Price.new(+ currency: currency,+ variant_id: id+ )end++ pricerescue TypeError- find_or_build_price.call+ Spree::Price.new(+ currency: currency,+ variant_id: id+ )enddef amount_in(currency)@@ -345,13 +352,13 @@ module Spreeenddef in_stock?- # Issue 10280- # Check if model responds to cache version and fall back to updated_at for older rails versions- # This makes sure a version is supplied when recyclable cache keys are disabled.- version = respond_to?(:cache_version) ? cache_version : updated_at.to_i- @in_stock ||= Rails.cache.fetch(in_stock_cache_key, version: version) do- total_on_hand > 0- end+ @in_stock ||= if association(:stock_items).loaded? && association(:stock_locations).loaded?+ total_on_hand.positive?+ else+ Rails.cache.fetch(in_stock_cache_key, version: cache_version) do+ total_on_hand.positive?+ end+ endenddef backorderable?@@ -360,6 +367,10 @@ module Spreeendend+ def on_sale?(currency)+ @on_sale ||= price_in(currency)&.discounted?+ end+delegate :total_on_hand, :can_supply?, to: :quantifieralias is_backorderable? backorderable?@@ -457,5 +468,9 @@ module Spreedef disable_sku_validation?Spree::Config[:disable_sku_validation]end++ def clear_line_items_cache+ line_items.update_all(updated_at: Time.current)+ endendenddiff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex a3d1a2d45e..f0e32fad12 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -411,11 +411,11 @@ describe Spree::Variant, type: :model doendend- describe '.price_in' do+ describe '#price_in' dosubject { variant.price_in(currency).display_amount }before do- variant.prices << create(:price, variant: variant, currency: 'EUR', amount: 33.33)+ create(:price, variant: variant, currency: 'EUR', amount: 33.33)endcontext 'when currency is not specified' do@@ -441,6 +441,70 @@ describe Spree::Variant, type: :model doexpect(subject.to_s).to eql '$19.99'endend++ context 'when there is no price with present amount in given currency' do+ let(:currency) { 'GBP' }++ before do+ variant.prices.create(currency: 'GBP', amount: nil)+ end++ it 'returns 0' do+ expect(subject.to_s).to eql '£0.00'+ end+ end+ end++ describe '#on_sale?' do+ subject { variant.on_sale?(currency) }++ let!(:eur_price) { create(:price, variant: variant, currency: 'EUR', amount: 100.00, compare_at_amount: eur_compare_at_amount) }+ let!(:gbp_price) { create(:price, variant: variant, currency: 'GBP', amount: 100.00, compare_at_amount: gbp_compare_at_amount) }+ let(:usd_compare_at_amount) { nil }+ let(:eur_compare_at_amount) { nil }+ let(:gbp_compare_at_amount) { nil }++ before do+ variant.prices.where(currency: 'USD').take.update(amount: 100.00, compare_at_amount: usd_compare_at_amount)+ end++ context 'when existing currency is passed' do+ let(:currency) { 'GBP' }+ let(:gbp_compare_at_amount) { 200.00 }++ it 'checks if variant is discounted in that currency' do+ expect(subject).to be true+ end++ context 'when variant is discounted' do+ let(:currency) { 'EUR' }+ let(:eur_compare_at_amount) { 200.00 }+ let(:gbp_compare_at_amount) { nil }++ it 'returns true' do+ expect(subject).to be true+ end+ end++ context 'when variant is not discounted' do+ let(:currency) { 'EUR' }++ it 'returns false' do+ expect(subject).to be false+ end+ end+ end++ context 'when passed currency does not exist' do+ let(:currency) { 'NON_EXISTING_CURRENCY' }+ let(:usd_compare_at_amount) { 200.00 }+ let(:eur_compare_at_amount) { 200.00 }+ let(:gbp_compare_at_amount) { 200.00 }++ it 'returns false' do+ expect(subject).to be false+ end+ endenddescribe '.amount_in' do