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
Fix compare_at_amount type casting issue (#13137)
Szymon Chorobskib54693f78e37
diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rbindex 9ade5def36..fae6f84e97 100644--- a/core/app/models/spree/price.rb+++ b/core/app/models/spree/price.rb@@ -66,8 +66,10 @@ module SpreeSpree::Money.new(compare_at_amount || 0, currency: currency)end- def compare_at_amount=(compare_at_amount)- self[:compare_at_amount] = Spree::LocalizedNumber.parse(compare_at_amount)+ def compare_at_amount=(value)+ calculated_value = Spree::LocalizedNumber.parse(value) if value.present?++ self[:compare_at_amount] = calculated_valueendalias_attribute :price, :amountdiff --git a/core/lib/tasks/core.rake b/core/lib/tasks/core.rakeindex 4d39956a75..2c15654fa1 100644--- a/core/lib/tasks/core.rake+++ b/core/lib/tasks/core.rake@@ -259,6 +259,19 @@ namespace :core doSpree::Product.where('discontinue_on <= ?', Time.current).where.not(status: 'archived').update_all(status: 'archived', updated_at: Time.current)end+ desc 'Migrate amount spree_prices.compare_at_amount.'+ task migrate_compare_at_amount: :environment do |_t, _args|+ include ActionView::Helpers::TextHelper+ puts '... started'+ total = 0+ Spree::Price.where(compare_at_amount: 0).in_batches do |prices|+ prices.update_all(compare_at_amount: nil)+ total += prices.count+ end+ puts '... done'+ puts "... migrated #{pluralize(total, 'record')}"+ end+desc 'Migrate newsletter subscribers'task migrate_newsletter_subscribers: :environment do |_t, _args|Spree.user_class.where(accepts_email_marketing: true).in_batches(of: 500) do |user_batch|diff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rbindex 7ba96d3616..38ef2a7ec4 100644--- a/core/spec/models/spree/price_spec.rb+++ b/core/spec/models/spree/price_spec.rb@@ -96,7 +96,7 @@ describe Spree::Price, type: :model doenddescribe '#compare_at_amount=' do- let(:price) { build :price }+ let(:price) { build(:price) }let(:compare_at_amount) { '169.99' }before do@@ -106,6 +106,14 @@ describe Spree::Price, type: :model doit 'is expected to equal to localized number' doexpect(price.compare_at_amount).to eq(Spree::LocalizedNumber.parse(compare_at_amount))end++ context 'with empty string being passed as value' do+ let(:compare_at_amount) { '' }++ it 'casts value to nil' do+ expect(price.compare_at_amount).to be_nil+ end+ endenddescribe '#price' do