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.

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.rb
index 9ade5def36..fae6f84e97 100644
--- a/core/app/models/spree/price.rb
+++ b/core/app/models/spree/price.rb
@@ -66,8 +66,10 @@ module Spree
Spree::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_value
end
alias_attribute :price, :amount
diff --git a/core/lib/tasks/core.rake b/core/lib/tasks/core.rake
index 4d39956a75..2c15654fa1 100644
--- a/core/lib/tasks/core.rake
+++ b/core/lib/tasks/core.rake
@@ -259,6 +259,19 @@ namespace :core do
Spree::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.rb
index 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 do
end
describe '#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 do
it 'is expected to equal to localized number' do
expect(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
+ end
end
describe '#price' do