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
Extract Market/Locale/Currency/Channel code to Purchase concerns
Damian Legawiec993697dc0cd0
diff --git a/spree/core/app/models/concerns/spree/purchase/channel.rb b/spree/core/app/models/concerns/spree/purchase/channel.rbnew file mode 100644index 0000000000..b036349947--- /dev/null+++ b/spree/core/app/models/concerns/spree/purchase/channel.rb@@ -0,0 +1,21 @@+module Spree+ module Purchase+ module Channel+ extend ActiveSupport::Concern++ included do+ belongs_to :channel, class_name: 'Spree::Channel'++ validates :channel, presence: true++ before_validation :ensure_channel_presence+ end++ def ensure_channel_presence+ return if channel_id.present?++ self.channel = store&.default_channel+ end+ end+ end+enddiff --git a/spree/core/app/models/concerns/spree/purchase/currency.rb b/spree/core/app/models/concerns/spree/purchase/currency.rbnew file mode 100644index 0000000000..d3b0528dd2--- /dev/null+++ b/spree/core/app/models/concerns/spree/purchase/currency.rb@@ -0,0 +1,29 @@+module Spree+ module Purchase+ module Currency+ extend ActiveSupport::Concern++ included do+ validates :currency, presence: true+ validate :currency_must_be_supported_by_store++ before_validation :ensure_currency+ end++ def currency_must_be_supported_by_store+ return if currency.blank? || store.blank?++ supported_codes = store.supported_currencies_list.map(&:iso_code)+ unless supported_codes.include?(currency)+ errors.add(:currency, Spree.t(:currency_not_supported_by_store))+ end+ end++ def ensure_currency+ return if currency.present?++ self.currency = market&.currency+ end+ end+ end+enddiff --git a/spree/core/app/models/concerns/spree/purchase/locale.rb b/spree/core/app/models/concerns/spree/purchase/locale.rbnew file mode 100644index 0000000000..c579a250d2--- /dev/null+++ b/spree/core/app/models/concerns/spree/purchase/locale.rb@@ -0,0 +1,30 @@+module Spree+ module Purchase+ module Locale+ extend ActiveSupport::Concern++ included do+ validates :locale, presence: true+ validate :locale_must_be_supported_by_store++ before_validation :ensure_locale+ end++ def ensure_locale+ return if locale.present?++ self.locale = Spree::Current.locale.presence || market&.default_locale+ end++ # Validates that the order's locale is within the store's supported locales.+ # Mirrors currency_must_be_supported_by_store.+ def locale_must_be_supported_by_store+ return if locale.blank? || store.blank?++ unless store.supported_locales_list.include?(locale)+ errors.add(:locale, Spree.t(:locale_not_supported_by_store))+ end+ end+ end+ end+enddiff --git a/spree/core/app/models/concerns/spree/purchase/market.rb b/spree/core/app/models/concerns/spree/purchase/market.rbnew file mode 100644index 0000000000..f3ed3271bf--- /dev/null+++ b/spree/core/app/models/concerns/spree/purchase/market.rb@@ -0,0 +1,28 @@+module Spree+ module Purchase+ module Market+ extend ActiveSupport::Concern++ included do+ belongs_to :market, class_name: 'Spree::Market'++ validates :market, presence: true++ before_validation :ensure_market_presence+ end++ def ensure_market_presence+ self.market ||= Spree::Current.market || store&.default_market+ end++ # When currency changes, auto-resolve the matching market (mirrors Order).+ def resolve_market_from_currency+ return unless store&.markets&.exists?+ return if market&.currency == currency++ resolved = store.markets.find_by(currency: currency)+ self.market = resolved if resolved+ end+ end+ end+enddiff --git a/spree/core/app/models/spree/cart.rb b/spree/core/app/models/spree/cart.rbindex b8f34c2042..491e776a97 100644--- a/spree/core/app/models/spree/cart.rb+++ b/spree/core/app/models/spree/cart.rb@@ -18,6 +18,21 @@ module Spreehas_secure_token+ extend Spree::DisplayMoney++ include Spree::SingleStoreResource+ include Spree::Purchase::Channel+ include Spree::Purchase::Market+ include Spree::Purchase::Currency+ include Spree::Purchase::Locale+ include Spree::Purchase::CheckoutSteps+ include Spree::Purchase::DigitalItems+ include Spree::Purchase::Taxation+ include Spree::Purchase::StoreCredits+ include Spree::Purchase::GiftCards+ include Spree::Purchase::LineItemCurrencies+ include Spree::Purchase::PaymentProcessing+# Concurrency is manual (the API's OrderLock semantics — compare# client-sent version, 409 on mismatch); Rails auto-locking must not# raise on internal saves.@@ -32,25 +47,14 @@ module Spreealias_attribute :item_count, :total_quantityalias_attribute :promo_total, :discount_total- extend Spree::DisplayMoneymoney_methods :item_total, :adjustment_total, :included_tax_total, :additional_tax_total,:discount_total, :fee_total, :delivery_total, :total, :payment_total, :outstanding_balance,:tax_total, :pre_tax_item_amount, :pre_tax_total, :amount_due- include Spree::Purchase::CheckoutSteps- include Spree::Purchase::DigitalItems- include Spree::Purchase::Taxation- include Spree::Purchase::StoreCredits- include Spree::Purchase::GiftCards- include Spree::Purchase::LineItemCurrencies- include Spree::Purchase::PaymentProcessingalias display_promo_total display_discount_totalalias display_ship_total display_delivery_totalalias_attribute :ship_total, :delivery_total- belongs_to :store, class_name: 'Spree::Store'- belongs_to :market, class_name: 'Spree::Market'- belongs_to :channel, class_name: 'Spree::Channel'belongs_to :customer, class_name: "::#{Spree.user_class}", optional: truebelongs_to :ship_address, class_name: 'Spree::Address', optional: true, dependent: :destroybelongs_to :bill_address, class_name: 'Spree::Address', optional: true, dependent: :destroy@@ -130,12 +134,7 @@ module Spreealias_method :shipping_address_attributes=, :ship_address_attributes=alias_method :billing_address_attributes=, :bill_address_attributes=- before_validation :ensure_market_presence- before_validation :ensure_channel_presence-validates :store, :currency, presence: true- validate :currency_must_be_supported_by_store-scope :complete, -> { where.not(completed_at: nil) }scope :incomplete, -> { where(completed_at: nil) }@@ -197,10 +196,6 @@ module SpreeSpree.cart_recalculate_totals_workflow.call(cart: self)end- def currency- self[:currency] || store&.default_currency- end-def shipping_eq_billing_address?bill_address == ship_addressend@@ -392,33 +387,5 @@ module Spreeself.bill_address = ship_address if ship_addresstrueend-- def currency_must_be_supported_by_store- return if currency.blank? || store.blank?-- supported_codes = store.supported_currencies_list.map(&:iso_code)- unless supported_codes.include?(currency)- errors.add(:currency, Spree.t(:currency_not_supported_by_store))- end- end-- def ensure_market_presence- self.market ||= Spree::Current.market || store&.default_market- end-- # When currency changes, auto-resolve the matching market (mirrors Order).- def resolve_market_from_currency- return unless store&.markets&.exists?- return if market&.currency == currency-- resolved = store.markets.find_by(currency: currency)- self.market = resolved if resolved- end-- def ensure_channel_presence- return if channel_id.present?-- self.channel = store&.default_channel- endendenddiff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex 1dbad9cf1b..c8fc5a173b 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -18,6 +18,11 @@ module Spreeextend Spree::DisplayMoney+ include Spree::SingleStoreResource+ include Spree::Purchase::Channel+ include Spree::Purchase::Market+ include Spree::Purchase::Currency+ include Spree::Purchase::Localeinclude Spree::Purchase::CheckoutStepsinclude Spree::Purchase::DigitalItemsinclude Spree::Purchase::Taxation@@ -29,7 +34,6 @@ module Spreeinclude Spree::Core::NumberGenerator.new(prefix: 'R')include Spree::NumberIdentifier- include Spree::SingleStoreResourcepublishes_lifecycle_eventsinclude Spree::MemoizedData@@ -162,9 +166,6 @@ module Spreealias_method :shipping_address=, :ship_address=alias_attribute :shipping_address_id, :ship_address_id- belongs_to :store, class_name: 'Spree::Store'- belongs_to :market, class_name: 'Spree::Market'- belongs_to :channel, class_name: 'Spree::Channel'belongs_to :preferred_stock_location, class_name: 'Spree::StockLocation', optional: truewith_options dependent: :destroy do@@ -229,10 +230,6 @@ module Spreealias shipments_attributes= fulfillments_attributes=# Needs to happen before save_permalink is called- before_validation :ensure_market_presence- before_validation :ensure_channel_presence- before_validation :ensure_currency_presence- before_validation :ensure_locale_presencebefore_validation :resolve_market_from_currency, if: -> { persisted? && currency_changed? && !skip_market_resolution }before_validation :clone_billing_address, if: :use_billing?@@ -247,9 +244,6 @@ module Spree# which is very costly and slow on large set of recordsvalidates :email, length: { maximum: 254, allow_blank: true }, email: { allow_blank: true }, if: :require_emailvalidates :total_quantity, numericality: { greater_than_or_equal_to: 0, less_than: 2**31, only_integer: true, allow_blank: true }- validates :store- validates :currency- validates :localeendvalidates :payment_status, inclusion: { in: PAYMENT_STATES, allow_blank: true }validates :fulfillment_status, inclusion: { in: FULFILLMENT_STATUSES, allow_blank: true }@@ -261,8 +255,6 @@ module Spreevalidates :delivery_total, MONEY_VALIDATIONvalidates :discount_total, NEGATIVE_MONEY_VALIDATIONvalidates :total, MONEY_VALIDATION- validate :currency_must_be_supported_by_store- validate :locale_must_be_supported_by_storedelegate :update_totals, :persist_totals, to: :updaterdelegate :merge!, to: :merger@@ -524,16 +516,6 @@ module Spreeensure_storeend- def ensure_market_presence- self.market ||= Spree::Current.market || store&.default_market- end-- def ensure_channel_presence- return if channel_id.present?-- self.channel = store&.default_channel- end-# @return [Boolean] true when this order has no registered user and its# channel forbids guest checkout (see Spree::Channel::Gating). Enforced by# the checkout completion service and the v3 Store API so every completion@@ -1139,35 +1121,6 @@ module Spreeuse_shipping.in?([true, 'true', '1'])end- def ensure_currency_presence- self.currency ||= store&.default_currency- end-- # Sets the locale from Spree::Current.locale when not already set.- # Called as a before_validation callback, mirroring ensure_currency_presence.- def ensure_locale_presence- self.locale ||= Spree::Current.locale- end-- def currency_must_be_supported_by_store- return if currency.blank? || store.blank?-- supported_codes = store.supported_currencies_list.map(&:iso_code)- unless supported_codes.include?(currency)- errors.add(:currency, Spree.t(:currency_not_supported_by_store))- end- end-- # Validates that the order's locale is within the store's supported locales.- # Mirrors currency_must_be_supported_by_store.- def locale_must_be_supported_by_store- return if locale.blank? || store.blank?-- unless store.supported_locales_list.include?(locale)- errors.add(:locale, Spree.t(:locale_not_supported_by_store))- end- end-# When currency changes, auto-resolve the matching market.# Only applies when the store has markets configured.def resolve_market_from_currency