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
Stop preference defaults from dirtying every record on load
Damian Legawiec4f3adbcc0005
diff --git a/spree/core/app/models/spree/base.rb b/spree/core/app/models/spree/base.rbindex 380aa9f081..1a998b5ed9 100644--- a/spree/core/app/models/spree/base.rb+++ b/spree/core/app/models/spree/base.rb@@ -10,9 +10,16 @@ class Spree::Base < ApplicationRecordinclude Spree::HasIsoGeographyinclude Spree::TypedAssociations+ # Backfills preferences added to the class after this row was last saved, so+ # a reader never sees nil for a newly defined preference. Assigning+ # unconditionally would dirty every record on load: `preferences` is a+ # YAML-serialized Hash, so the dirty check compares the serialized string and+ # the merge reorders keys even when nothing changed — which is enough to make+ # `with_lock` refuse the record ("unpersisted changes").after_initialize doif has_attribute?(:preferences) && !preferences.nil?- self.preferences = default_preferences.merge(preferences)+ missing = default_preferences.except(*preferences.keys)+ self.preferences = preferences.merge(missing) if missing.any?endenddiff --git a/spree/core/spec/models/spree/base_spec.rb b/spree/core/spec/models/spree/base_spec.rbindex 7cf079587f..769e11d848 100644--- a/spree/core/spec/models/spree/base_spec.rb+++ b/spree/core/spec/models/spree/base_spec.rb@@ -106,4 +106,29 @@ describe Spree::Base dodescribe '.json_api_permitted_attributes' doit { expect(Spree::Address.json_api_permitted_attributes).to contain_exactly('firstname', 'lastname', 'address1', 'address2', 'city', 'zipcode', 'phone', 'country_code', 'state_code', 'state_name', 'alternative_phone', 'company', 'country_id', 'state_id', 'created_at', 'updated_at', 'customer_id', 'deleted_at', 'label', 'metadata', 'quick_checkout', 'latitude', 'longitude') }end++ describe 'preference defaults on load' do+ let(:store) { Spree::Store.default }++ it 'backfills preferences added since the record was last saved' do+ store.update_column(:preferences, store.preferences.except(:timezone))++ loaded = Spree::Store.find(store.id)++ expect(loaded.preferences[:timezone]).to eq(loaded.preference_default(:timezone))+ end++ # `preferences` is a YAML-serialized Hash, so the dirty check compares the+ # serialized string: re-writing the attribute with the same values in a+ # different key order still counts as a change, and `with_lock` refuses a+ # record with unpersisted changes.+ it 'leaves a record clean when the stored preferences only differ in key order' do+ store.update_column(:preferences, store.preferences.to_a.reverse.to_h)++ loaded = Spree::Store.find(store.id)++ expect(loaded).not_to be_changed+ expect { loaded.with_lock { nil } }.not_to raise_error+ end+ endend