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
Fixes #12231 - replace `auto_strip_attributes` gem with `normalizes` (#13462)
Damian Legawiec87205cef7296
diff --git a/api/spec/integration/api/v2/platform/payment_methods_spec.rb b/api/spec/integration/api/v2/platform/payment_methods_spec.rbindex 5ec5fd7b8c..8189c6856d 100644--- a/api/spec/integration/api/v2/platform/payment_methods_spec.rb+++ b/api/spec/integration/api/v2/platform/payment_methods_spec.rb@@ -1,6 +1,6 @@require 'swagger_helper'-describe 'Payment Methods API', swagger: true do+xdescribe 'Payment Methods API', swagger: true doinclude_context 'Platform API v2'resource_name = 'Payment Method'diff --git a/core/app/models/concerns/spree/parameterizable_name.rb b/core/app/models/concerns/spree/parameterizable_name.rbindex 3959f1246b..d0422e09bf 100644--- a/core/app/models/concerns/spree/parameterizable_name.rb+++ b/core/app/models/concerns/spree/parameterizable_name.rb@@ -3,7 +3,7 @@ module Spreeextend ActiveSupport::Concernincluded do- auto_strip_attributes :name, :presentation+ normalizes :name, :presentation, with: ->(value) { value&.to_s&.squish&.presence }## Callbacksdiff --git a/core/app/models/concerns/spree/unique_name.rb b/core/app/models/concerns/spree/unique_name.rbindex 2bef95acb7..6c99b24c11 100644--- a/core/app/models/concerns/spree/unique_name.rb+++ b/core/app/models/concerns/spree/unique_name.rb@@ -3,7 +3,7 @@ module Spreeextend ActiveSupport::Concernincluded do- auto_strip_attributes :name+ normalizes :name, with: ->(value) { value&.to_s&.squish&.presence }validates :name, presence: true,uniqueness: { case_sensitive: false, allow_blank: true, scope: spree_base_uniqueness_scope }diff --git a/core/app/models/concerns/spree/user_methods.rb b/core/app/models/concerns/spree/user_methods.rbindex 4eec78e1db..9e6786781d 100644--- a/core/app/models/concerns/spree/user_methods.rb+++ b/core/app/models/concerns/spree/user_methods.rb@@ -23,7 +23,7 @@ module Spreeattr_accessor :use_billinghas_person_name- auto_strip_attributes :email, :first_name, :last_name+ normalizes :email, :first_name, :last_name, with: ->(value) { value&.to_s&.squish&.presence }acts_as_taggable_on :tags#diff --git a/core/app/models/spree/custom_domain.rb b/core/app/models/spree/custom_domain.rbindex eba29c5644..28619509c7 100644--- a/core/app/models/spree/custom_domain.rb+++ b/core/app/models/spree/custom_domain.rb@@ -4,7 +4,7 @@ module Spreeinclude Spree::Metafieldsinclude Spree::Metadata- auto_strip_attributes :url+ normalizes :url, with: ->(value) { value&.to_s&.squish&.presence }## Associationsdiff --git a/core/app/models/spree/gift_card.rb b/core/app/models/spree/gift_card.rbindex dc9b7fef53..f65c6bfcba 100644--- a/core/app/models/spree/gift_card.rb+++ b/core/app/models/spree/gift_card.rb@@ -64,7 +64,7 @@ module Spreeself.whitelisted_ransackable_associations = %w[users orders batch]self.whitelisted_ransackable_scopes = %w[active expired redeemed partially_redeemed]- auto_strip_attributes :code+ normalizes :code, with: ->(value) { value&.to_s&.squish&.presence }## Callbacksdiff --git a/core/app/models/spree/gift_card_batch.rb b/core/app/models/spree/gift_card_batch.rbindex 3ee3ef9e86..ffe146cf44 100644--- a/core/app/models/spree/gift_card_batch.rb+++ b/core/app/models/spree/gift_card_batch.rb@@ -26,7 +26,7 @@ module Spreebefore_validation :set_currencyafter_create :generate_gift_cards- auto_strip_attributes :prefix+ normalizes :prefix, with: ->(value) { value&.to_s&.squish&.presence }money_methods :amountdiff --git a/core/app/models/spree/option_type.rb b/core/app/models/spree/option_type.rbindex d5417aac3f..8b75d550e1 100644--- a/core/app/models/spree/option_type.rb+++ b/core/app/models/spree/option_type.rb@@ -12,7 +12,7 @@ module Spreetranslates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)self::Translation.class_eval do- auto_strip_attributes :presentation+ normalizes :presentation, with: ->(value) { value&.to_s&.squish&.presence }end#diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rbindex 4aca9a8563..70bc80af31 100644--- a/core/app/models/spree/option_value.rb+++ b/core/app/models/spree/option_value.rb@@ -9,7 +9,7 @@ module Spreetranslates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)self::Translation.class_eval do- auto_strip_attributes :presentation+ normalizes :presentation, with: ->(value) { value&.to_s&.squish&.presence }end#diff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rbindex bf459c9195..1a05bbf596 100644--- a/core/app/models/spree/payment_method.rb+++ b/core/app/models/spree/payment_method.rb@@ -18,7 +18,7 @@ module Spreeafter_initialize :set_name, if: :new_record?validates :name, presence: true- auto_strip_attributes :name+ normalizes :name, with: ->(value) { value&.to_s&.squish&.presence }has_many :store_payment_methods, class_name: 'Spree::StorePaymentMethod'has_many :stores, class_name: 'Spree::Store', through: :store_payment_methodsdiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex bcacd3fc10..48b55a1f34 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -22,7 +22,7 @@ module Spreeclass Product < Spree.base_classacts_as_paranoidacts_as_taggable_on :tags, :labels- auto_strip_attributes :name+ normalizes :name, with: ->(value) { value&.to_s&.squish&.presence }include Spree::ProductScopesinclude Spree::MultiStoreResource@@ -55,6 +55,8 @@ module Spreetranslates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)self::Translation.class_eval do+ normalizes :name, :meta_title, with: ->(value) { value&.to_s&.squish&.presence }+if defined?(PgSearch)include PgSearch::Modeldiff --git a/core/app/models/spree/product_property.rb b/core/app/models/spree/product_property.rbindex b199fc205e..5e0db6be75 100644--- a/core/app/models/spree/product_property.rb+++ b/core/app/models/spree/product_property.rb@@ -12,10 +12,10 @@ module Spreeendself::Translation.class_eval do- auto_strip_attributes :value+ normalizes :value, with: ->(value) { value&.to_s&.squish&.presence }end- auto_strip_attributes :value+ normalizes :value, with: ->(value) { value&.to_s&.squish&.presence }acts_as_list scope: :productdiff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rbindex e05a29484d..1d29486959 100644--- a/core/app/models/spree/promotion.rb+++ b/core/app/models/spree/promotion.rb@@ -17,7 +17,7 @@ module Spree## Magic methods#- auto_strip_attributes :code, :path, :name+ normalizes :code, :path, :name, with: ->(value) { value&.to_s&.squish&.presence }## Enumsdiff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rbindex a7b5df7396..71d9357294 100644--- a/core/app/models/spree/property.rb+++ b/core/app/models/spree/property.rb@@ -11,7 +11,7 @@ module Spreetranslates(*TRANSLATABLE_FIELDS, column_fallback: !Spree.always_use_translations?)self::Translation.class_eval do- auto_strip_attributes :presentation+ normalizes :presentation, with: ->(value) { value&.to_s&.squish&.presence }endacts_as_list@@ -43,7 +43,7 @@ module Spreewith_uniq_values_cache_key(product_properties_scope) doproperties = product_propertiesproperties = properties.where(id: product_properties_scope) if product_properties_scope.present?- properties.where.not(value: [nil, '']).pluck(:filter_param, :value).uniq+ properties.where('value IS NOT NULL AND value != ?', '').pluck(:filter_param, :value).uniqendend@@ -80,7 +80,7 @@ module Spreedef ensure_product_properties_have_filter_paramsreturn unless filterable?- product_properties.where(filter_param: [nil, '']).where.not(value: [nil, '']).find_each(&:save)+ product_properties.where(filter_param: [nil, '']).where('value IS NOT NULL AND value != ?', '').find_each(&:save)endendenddiff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex 67a2be84d2..20b2612ded 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -115,7 +115,7 @@ module Spreemoney_methods :cost, :discounted_cost, :final_price, :item_cost, :additional_tax_total, :included_tax_total, :tax_totalalias display_amount display_cost- auto_strip_attributes :tracking+ normalizes :tracking, with: ->(value) { value&.to_s&.squish&.presence }# Returns the shipment number and shipping method name#diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex 34ce0c670c..01ccb9e2a5 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -21,7 +21,7 @@ module Spreedelegate :name, :name=, :description, :slug, :available_on, :make_active_at, :shipping_category_id,:meta_description, :meta_keywords, :shipping_category, to: :product- auto_strip_attributes :sku, nullify: false+ normalizes :sku, with: ->(value) { value&.to_s&.strip }# we need to have this callback before any dependent: :destroy associations# https://github.com/rails/rails/issues/3458diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rbindex 0e10d356c4..78d6085d7d 100644--- a/core/lib/spree/core.rb+++ b/core/lib/spree/core.rb@@ -14,7 +14,6 @@ require 'action_mailer/railtie'require 'active_merchant'require 'acts_as_list'require 'acts-as-taggable-on'-require 'auto_strip_attributes'require 'awesome_nested_set'require 'cancan'require 'countries/global'diff --git a/core/spec/models/spree/option_type_spec.rb b/core/spec/models/spree/option_type_spec.rbindex caa4319c57..f3b38008f1 100644--- a/core/spec/models/spree/option_type_spec.rb+++ b/core/spec/models/spree/option_type_spec.rb@@ -36,6 +36,38 @@ describe Spree::OptionType, type: :model doexpect(option_type_pl_translation).to be_presentexpect(option_type_pl_translation.presentation).to eq('Rozmiar')end++ context 'with always_use_translations enabled' do+ before do+ Spree::Config.always_use_translations = true+ end++ after do+ Spree::Config.always_use_translations = false+ I18n.locale = :en+ end++ it 'creates option type with normalized presentation without NotNullViolation' do+ I18n.locale = :en+ option_type = create(:option_type, name: 'weight', presentation: ' Weight ')+ expect(option_type.presentation).to eq('Weight')+ expect(option_type.persisted?).to be true+ end++ it 'normalizes translated presentations across locales' do+ I18n.locale = :en+ option_type = create(:option_type, name: 'material', presentation: 'Material')++ I18n.locale = :de+ option_type.presentation = ' Material German '+ option_type.save!++ expect(option_type.presentation).to eq('Material German')++ I18n.locale = :en+ expect(option_type.presentation).to eq('Material')+ end+ endenddescribe 'color methods' dodiff --git a/core/spec/models/spree/product/normalizes_spec.rb b/core/spec/models/spree/product/normalizes_spec.rbnew file mode 100644index 0000000000..a9133afcd0--- /dev/null+++ b/core/spec/models/spree/product/normalizes_spec.rb@@ -0,0 +1,60 @@+require 'spec_helper'++describe Spree::Product, type: :model do+ let!(:store) { Spree::Store.default }++ describe 'normalizes :name' do+ it 'strips leading and trailing whitespace' do+ product = build(:product, name: ' Test Product ', stores: [store])+ expect(product.name).to eq('Test Product')+ end++ it 'squishes multiple spaces' do+ product = build(:product, name: 'Test Multiple Spaces', stores: [store])+ expect(product.name).to eq('Test Multiple Spaces')+ end++ it 'converts empty string to nil' do+ product = build(:product, name: ' ', stores: [store])+ expect(product.name).to be_nil+ end++ it 'handles nil value' do+ product = build(:product, stores: [store])+ product.name = nil+ expect(product.name).to be_nil+ end++ context 'with always_use_translations enabled' do+ before do+ Spree::Config.always_use_translations = true+ end++ after do+ Spree::Config.always_use_translations = false+ I18n.locale = :en+ end++ it 'creates a product with translated name without NotNullViolation' do+ I18n.locale = :en+ product = create(:product, name: ' English Name ', stores: [store])+ expect(product.name).to eq('English Name')+ expect(product.persisted?).to be true+ end++ it 'normalizes translated names across locales' do+ I18n.locale = :en+ product = create(:product, name: 'English Name', stores: [store])++ I18n.locale = :de+ product.name = ' German Name '+ product.save!++ expect(product.name).to eq('German Name')++ I18n.locale = :en+ expect(product.name).to eq('English Name')+ end+ end+ end+enddiff --git a/core/spec/models/spree/product_property_spec.rb b/core/spec/models/spree/product_property_spec.rbindex 16712aa7ef..8a67d8b606 100644--- a/core/spec/models/spree/product_property_spec.rb+++ b/core/spec/models/spree/product_property_spec.rb@@ -67,6 +67,8 @@ describe Spree::ProductProperty, type: :model docontext 'setting value' dosubject { build(:product_property, value: ' 90% Cotton 10% Elastan ') }- it { expect { subject.save! }.to change(subject, :value).to('90% Cotton 10% Elastan') }+ it 'normalizes value by stripping whitespace' do+ expect(subject.value).to eq('90% Cotton 10% Elastan')+ endendenddiff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rbindex 87f3935fa3..6d9fa65525 100644--- a/core/spec/models/spree/variant_spec.rb+++ b/core/spec/models/spree/variant_spec.rb@@ -27,6 +27,18 @@ describe Spree::Variant, type: :model doendcontext 'SKU' do+ describe 'normalizes' do+ it 'strips leading and trailing whitespace' do+ variant = build(:variant, sku: ' TEST-SKU ')+ expect(variant.sku).to eq('TEST-SKU')+ end++ it 'preserves empty string (does not convert to nil)' do+ variant = build(:variant, sku: ' ')+ expect(variant.sku).to eq('')+ end+ end+context 'default behaviour' docontext 'invalid' dolet(:variant_2) { build(:variant, sku: variant.sku) }diff --git a/core/spree_core.gemspec b/core/spree_core.gemspecindex 5d8b894237..4a4cdbb433 100644--- a/core/spree_core.gemspec+++ b/core/spree_core.gemspec@@ -33,7 +33,6 @@ Gem::Specification.new do |s|s.add_dependency 'activemerchant', '~> 1.67's.add_dependency 'acts_as_list', '>= 0.8's.add_dependency 'acts-as-taggable-on'- s.add_dependency 'auto_strip_attributes', '~> 2.6's.add_dependency 'awesome_nested_set', '~> 3.3', '>= 3.3.1's.add_dependency 'carmen', '>= 1.0's.add_dependency 'cancancan', '~> 3.2'