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
Introduce NumberIdentifier concern (#11346)
Damian Legawiec66a8b37f8aae
diff --git a/core/app/models/concerns/spree/number_identifier.rb b/core/app/models/concerns/spree/number_identifier.rbnew file mode 100644index 0000000000..0dc66c10eb--- /dev/null+++ b/core/app/models/concerns/spree/number_identifier.rb@@ -0,0 +1,17 @@+module Spree+ module NumberIdentifier+ extend ActiveSupport::Concern++ included do+ before_validation :uppercase_number++ validates :number, presence: true, length: { maximum: 32, allow_blank: true }, uniqueness: { allow_blank: true, case_sensitive: true }+ end++ protected++ def uppercase_number+ number&.to_s&.upcase!+ end+ end+enddiff --git a/core/app/models/spree/customer_return.rb b/core/app/models/spree/customer_return.rbindex 512aae71ca..c106c9edea 100644--- a/core/app/models/spree/customer_return.rb+++ b/core/app/models/spree/customer_return.rb@@ -1,6 +1,8 @@module Spreeclass CustomerReturn < Spree::Baseinclude Spree::Core::NumberGenerator.new(prefix: 'CR', length: 9)+ include NumberIdentifier+belongs_to :stock_locationbelongs_to :store, class_name: 'Spree::Store', inverse_of: :customer_returns@@ -11,7 +13,6 @@ module Spreeafter_create :process_return!validates :store, presence: true- validates :number, uniqueness: { case_sensitive: true }validates :return_items, :stock_location, presence: truevalidate :must_have_return_authorization, on: :createvalidate :return_items_belong_to_same_orderdiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex b76b543a12..eb363198f3 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -18,6 +18,7 @@ module Spreeinclude Spree::Core::NumberGenerator.new(prefix: 'R')include Spree::Core::TokenGenerator+ include NumberIdentifierinclude NumberAsParaminclude SingleStoreResourceinclude MemoizedData@@ -128,7 +129,6 @@ module Spree# Needs to happen before save_permalink is calledbefore_validation :ensure_store_presencebefore_validation :ensure_currency_presence- before_validation :uppercase_numberbefore_validation :clone_billing_address, if: :use_billing?attr_accessor :use_billing@@ -140,7 +140,6 @@ module Spreewith_options presence: true do# we want to have this case_sentive: true as changing it to false causes all SQL to use LOWER(slug)# which is very costly and slow on large set of records- validates :number, length: { maximum: 32, allow_blank: true }, uniqueness: { allow_blank: true, case_sensitive: true }validates :email, length: { maximum: 254, allow_blank: true }, email: { allow_blank: true }, if: :require_emailvalidates :item_count, numericality: { greater_than_or_equal_to: 0, less_than: 2**31, only_integer: true, allow_blank: true }validates :store@@ -730,9 +729,5 @@ module Spreedef credit_card_nil_payment?(attributes)payments.store_credits.present? && attributes[:amount].to_f.zero?end-- def uppercase_number- number&.upcase!- endendenddiff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex 19a21441c0..be52a2231a 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -3,11 +3,11 @@ require_dependency 'spree/payment/processing'module Spreeclass Payment < Spree::Baseinclude Spree::Core::NumberGenerator.new(prefix: 'P', letters: true, length: 7)+ include NumberIdentifier+ include NumberAsParaminclude Spree::Payment::Processing- include NumberAsParam-NON_RISKY_AVS_CODES = ['B', 'D', 'H', 'J', 'M', 'Q', 'T', 'V', 'X', 'Y'].freezeRISKY_AVS_CODES = ['A', 'C', 'E', 'F', 'G', 'I', 'K', 'L', 'N', 'O', 'P', 'R', 'S', 'U', 'W', 'Z'].freezeINVALID_STATES = %w(failed invalid).freeze@@ -25,7 +25,6 @@ module Spreehas_many :refunds, inverse_of: :paymentvalidates :payment_method, presence: true- validates :number, uniqueness: { case_sensitive: true }validates :source, presence: true, if: -> { payment_method&.source_required? }before_validation :validate_sourcediff --git a/core/app/models/spree/reimbursement.rb b/core/app/models/spree/reimbursement.rbindex f1e2aa7c98..9318e1f638 100644--- a/core/app/models/spree/reimbursement.rb+++ b/core/app/models/spree/reimbursement.rb@@ -1,6 +1,7 @@module Spreeclass Reimbursement < Spree::Baseinclude Spree::Core::NumberGenerator.new(prefix: 'RI', length: 9)+ include NumberIdentifierclass IncompleteReimbursementError < StandardError; end@@ -15,7 +16,6 @@ module Spreehas_many :return_itemsend- validates :number, uniqueness: truevalidates :order, presence: truevalidate :validate_return_items_belong_to_same_orderdiff --git a/core/app/models/spree/return_authorization.rb b/core/app/models/spree/return_authorization.rbindex 5f88d02a36..a2e1dc4baa 100644--- a/core/app/models/spree/return_authorization.rb+++ b/core/app/models/spree/return_authorization.rb@@ -1,6 +1,7 @@module Spreeclass ReturnAuthorization < Spree::Baseinclude Spree::Core::NumberGenerator.new(prefix: 'RA', length: 9)+ include NumberIdentifierbelongs_to :order, class_name: 'Spree::Order', inverse_of: :return_authorizations@@ -17,7 +18,6 @@ module Spreeaccepts_nested_attributes_for :return_items, allow_destroy: true- validates :number, uniqueness: truevalidates :order, :reason, :stock_location, presence: truevalidate :must_have_shipped_units, on: :creatediff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rbindex 9976045137..97cd0312f6 100644--- a/core/app/models/spree/shipment.rb+++ b/core/app/models/spree/shipment.rb@@ -3,7 +3,7 @@ require 'ostruct'module Spreeclass Shipment < Spree::Baseinclude Spree::Core::NumberGenerator.new(prefix: 'H', length: 11)-+ include NumberIdentifierinclude NumberAsParamwith_options inverse_of: :shipments do@@ -26,7 +26,6 @@ module Spreebefore_validation :set_cost_zero_when_nilvalidates :stock_location, presence: true- validates :number, uniqueness: { case_sensitive: true }attr_accessor :special_instructionsdiff --git a/core/app/models/spree/stock_transfer.rb b/core/app/models/spree/stock_transfer.rbindex 0d679a0e90..393ff093d7 100644--- a/core/app/models/spree/stock_transfer.rb+++ b/core/app/models/spree/stock_transfer.rb@@ -1,20 +1,16 @@module Spreeclass StockTransfer < Spree::Baseinclude Spree::Core::NumberGenerator.new(prefix: 'T')+ include NumberIdentifier+ include NumberAsParamhas_many :stock_movements, as: :originatorbelongs_to :source_location, class_name: 'StockLocation', optional: truebelongs_to :destination_location, class_name: 'StockLocation'- validates :number, uniqueness: true-self.whitelisted_ransackable_attributes = %w[reference source_location_id destination_location_id number]- def to_param- number- end-def source_movementsfind_stock_location_with_location_id(source_location_id)enddiff --git a/core/lib/spree/core/number_generator.rb b/core/lib/spree/core/number_generator.rbindex 17d64ec071..709a04b0b2 100644--- a/core/lib/spree/core/number_generator.rb+++ b/core/lib/spree/core/number_generator.rb@@ -16,8 +16,6 @@ module Spreegenerator_instance = selfhost.class_eval do- validates(:number, presence: true, uniqueness: { allow_blank: true })-before_validation do |instance|instance.number ||= generate_permalink(host)end