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
Added `Spree::Purchase::Addresses` instead of AddressBook and methods in models
Damian Legawiec2fec553b2808
diff --git a/spree/core/app/models/concerns/spree/purchase/address_book.rb b/spree/core/app/models/concerns/spree/purchase/addresses.rbsimilarity index 66%rename from spree/core/app/models/concerns/spree/purchase/address_book.rbrename to spree/core/app/models/concerns/spree/purchase/addresses.rbindex 0be51a7c0a..22cf38ca5a 100644--- a/spree/core/app/models/concerns/spree/purchase/address_book.rb+++ b/spree/core/app/models/concerns/spree/purchase/addresses.rb@@ -1,10 +1,63 @@module Spreemodule Purchase- # Address-book behavior shared by Spree::Cart and Spree::Order: fills- # defaults from the customer's saved addresses, deduplicates address- # rows on write, guards address_id assignment to the owning customer,- # and promotes checkout addresses to the customer's defaults.- module AddressBook+ # The full address surface shared by Spree::Cart and Spree::Order:+ # the bill/ship associations and their public aliases, default filling+ # from the customer's saved addresses, address-row deduplication on+ # write, ownership-guarded address_id assignment, promotion of checkout+ # addresses to the customer's defaults, and the ship→bill cloning+ # predicate. The shipping address is canonical: use_shipping copies it+ # onto the billing address; the reverse use_billing direction is a+ # deprecated 6.1-removal bridge. Clone callbacks are wired per model.+ module Addresses+ extend ActiveSupport::Concern++ included do+ belongs_to :bill_address, class_name: 'Spree::Address', optional: true, dependent: :destroy+ belongs_to :ship_address, class_name: 'Spree::Address', optional: true, dependent: :destroy++ accepts_nested_attributes_for :bill_address+ accepts_nested_attributes_for :ship_address++ alias_method :billing_address, :bill_address+ alias_method :billing_address=, :bill_address=+ alias_attribute :billing_address_id, :bill_address_id+ alias_method :shipping_address, :ship_address+ alias_method :shipping_address=, :ship_address=+ alias_attribute :shipping_address_id, :ship_address_id++ alias_method :shipping_address_attributes=, :ship_address_attributes=+ alias_method :billing_address_attributes=, :bill_address_attributes=++ attr_accessor :temporary_address, :use_shipping+ attr_reader :use_billing++ before_validation :clone_billing_address, if: :use_billing?+ before_validation :clone_shipping_address, if: :use_shipping?+ end++ # @return [Boolean]+ def shipping_eq_billing_address?+ bill_address == ship_address+ end++ # @deprecated The shipping address is canonical — use +use_shipping+ to+ # copy it onto the billing address. Removed in 6.1.+ def use_billing=(value)+ Spree::Deprecation.warn('use_billing is deprecated and will be removed in Spree 6.1. The shipping address comes first — use use_shipping to copy it onto the billing address.')+ @use_billing = value+ end++ # @deprecated See {#use_billing=}; removed in 6.1.+ # @return [Boolean]+ def use_billing?+ use_billing.in?([true, 'true', '1'])+ end++ # @return [Boolean]+ def use_shipping?+ use_shipping.in?([true, 'true', '1'])+ end+# Fills any blank bill/ship address from the customer's valid saved# defaults. The ship address is skipped when no physical delivery is# required, so shipping-address validations never fire on digital-only@@ -33,6 +86,8 @@ module Spree# Copies the bill address onto the ship address and promotes it to the# customer's default ship address.#+ # @deprecated The shipping address is canonical (see {#use_billing=});+ # removed in 6.1 together with the use_billing bridge.# @return [true]def clone_billing_addressself.ship_address = bill_address if bill_addressdiff --git a/spree/core/app/models/spree/cart.rb b/spree/core/app/models/spree/cart.rbindex 37d1d2f0da..81d1eb9abd 100644--- a/spree/core/app/models/spree/cart.rb+++ b/spree/core/app/models/spree/cart.rb@@ -27,7 +27,7 @@ module Spreeinclude Spree::Purchase::GiftCardsinclude Spree::Purchase::LineItemCurrenciesinclude Spree::Purchase::PaymentProcessing- include Spree::Purchase::AddressBook+ include Spree::Purchase::Addresses# Concurrency is manual (the API's OrderLock semantics — compare# client-sent version, 409 on mismatch); Rails auto-locking must not@@ -56,8 +56,6 @@ module Spreealias_method :user, :customeralias_method :user=, :customer=alias_attribute :user_id, :customer_id- belongs_to :ship_address, class_name: 'Spree::Address', optional: true, dependent: :destroy- belongs_to :bill_address, class_name: 'Spree::Address', optional: true, dependent: :destroy# Normalizes like the legacy Order writer so lookups stay case-insensitive.def coupon_code=(code)normalized = begin@@ -95,13 +93,6 @@ module SpreeSpree::StockLocation.find_by(id: preferred_stock_location_id)end- alias_method :shipping_address, :ship_address- alias_method :shipping_address=, :ship_address=- alias_attribute :shipping_address_id, :ship_address_id- alias_method :billing_address, :bill_address- alias_method :billing_address=, :bill_address=- alias_attribute :billing_address_id, :bill_address_id-has_many :line_items, -> { order(:created_at) }, class_name: 'Spree::LineItem', inverse_of: :cart, dependent: :destroyhas_many :variants, through: :line_itemshas_many :products, through: :variants@@ -125,17 +116,13 @@ module Spreeaccepts_nested_attributes_for :line_itemsaccepts_nested_attributes_for :payments- alias_method :shipping_address_attributes=, :ship_address_attributes=- alias_method :billing_address_attributes=, :bill_address_attributes=scope :complete, -> { where.not(completed_at: nil) }scope :incomplete, -> { where(completed_at: nil) }self.whitelisted_ransackable_attributes = %w[email completed_at token updated_at]- attr_accessor :temporary_address, :use_shipping, :skip_market_resolution-- before_validation :clone_shipping_address, if: :use_shipping?+ attr_accessor :skip_market_resolutionbefore_validation :resolve_market_from_currency, if: -> { persisted? && currency_changed? && !skip_market_resolution }@@ -190,14 +177,6 @@ module SpreeSpree.cart_recalculate_totals_workflow.call(cart: self)end- def shipping_eq_billing_address?- bill_address == ship_address- end-- def use_shipping?- use_shipping.in?([true, 'true', '1'])- end-def quantityline_items.sum(:quantity)enddiff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex f7138151c2..a74d0978f1 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -29,7 +29,7 @@ module Spreeinclude Spree::Purchase::GiftCardsinclude Spree::Purchase::LineItemCurrenciesinclude Spree::Purchase::PaymentProcessing- include Spree::Purchase::AddressBook+ include Spree::Purchase::Addressesinclude Spree::Core::NumberGenerator.new(prefix: 'R')include Spree::NumberIdentifier@@ -121,8 +121,6 @@ module Spree]self.whitelisted_ransackable_scopes = %w[complete incomplete refunded partially_refunded search multi_search]- attr_accessor :temporary_address-# Set to false on admin-initiated flows to suppress customer-facing emails.attr_accessor :notify_customer@@ -158,18 +156,6 @@ module Spreebelongs_to :approver, class_name: "::#{Spree.admin_user_class}", optional: truebelongs_to :canceler, class_name: "::#{Spree.admin_user_class}", optional: true- belongs_to :bill_address, foreign_key: :bill_address_id, class_name: 'Spree::Address',- optional: true, dependent: :destroy- alias_method :billing_address, :bill_address- alias_method :billing_address=, :bill_address=- alias_attribute :billing_address_id, :bill_address_id-- belongs_to :ship_address, foreign_key: :ship_address_id, class_name: 'Spree::Address',- optional: true, dependent: :destroy- alias_method :shipping_address, :ship_address- alias_method :shipping_address=, :ship_address=- alias_attribute :shipping_address_id, :ship_address_id-belongs_to :preferred_stock_location, class_name: 'Spree::StockLocation', optional: truewith_options dependent: :destroy do@@ -224,10 +210,6 @@ module Spreedelegate :has_markets?, to: :store, prefix: trueaccepts_nested_attributes_for :line_items- accepts_nested_attributes_for :bill_address- accepts_nested_attributes_for :ship_address- alias shipping_address_attributes= ship_address_attributes=- alias billing_address_attributes= bill_address_attributes=accepts_nested_attributes_for :payments, reject_if: :credit_card_nil_payment?accepts_nested_attributes_for :fulfillments# @deprecated legacy writer — removed in 6.1@@ -236,9 +218,7 @@ module Spree# Needs to happen before save_permalink is calledbefore_validation :resolve_market_from_currency, if: -> { persisted? && currency_changed? && !skip_market_resolution }- before_validation :clone_billing_address, if: :use_billing?- before_validation :clone_shipping_address, if: :use_shipping?- attr_accessor :use_billing, :use_shipping, :skip_market_resolution+ attr_accessor :skip_market_resolutionbefore_create :link_by_emailbefore_update :ensure_updated_fulfillments, :homogenize_line_item_currencies, if: :currency_changed?@@ -888,11 +868,6 @@ module Spreefulfillments.map { |s| s.refresh_rates(shipping_method_filter) }end- def shipping_eq_billing_address?- bill_address == ship_address- end--def set_shipments_costSpree::Deprecation.warn('Spree::Order#set_shipments_cost is deprecated and will be removed in Spree 6.1l. Please use set_fulfillments_cost instead')set_fulfillments_cost@@ -1130,14 +1105,6 @@ module Spreeendend- def use_billing?- use_billing.in?([true, 'true', '1'])- end-- def use_shipping?- use_shipping.in?([true, 'true', '1'])- end-# When currency changes, auto-resolve the matching market.# Only applies when the store has markets configured.def resolve_market_from_currencydiff --git a/spree/core/spec/models/concerns/spree/purchase/address_book_spec.rb b/spree/core/spec/models/concerns/spree/purchase/addresses_spec.rbsimilarity index 51%rename from spree/core/spec/models/concerns/spree/purchase/address_book_spec.rbrename to spree/core/spec/models/concerns/spree/purchase/addresses_spec.rbindex 241067d954..b498ecac00 100644--- a/spree/core/spec/models/concerns/spree/purchase/address_book_spec.rb+++ b/spree/core/spec/models/concerns/spree/purchase/addresses_spec.rb@@ -1,10 +1,10 @@require 'spec_helper'-# Shared battery for Spree::Purchase::AddressBook, run against BOTH hosts.+# Shared battery for Spree::Purchase::Addresses, run against BOTH hosts.# Checkout moved from Order to Cart once already and silently lost this# behavior (dedup, default promotion, ownership guard) — the shared examples# exist so it cannot happen again without a red suite.-RSpec.shared_examples 'an address book host' do+RSpec.shared_examples 'an addresses host' dolet(:store) { @default_store }let(:user) { create(:user) }@@ -120,20 +120,133 @@ RSpec.shared_examples 'an address book host' doexpect(record.ship_address).to be_nilendend++ describe '#shipping_eq_billing_address?' do+ it 'compares the two addresses' do+ address = create(:address, user: user)+ record.ship_address = address+ record.bill_address = address+ expect(record.shipping_eq_billing_address?).to be(true)++ record.bill_address = create(:address, user: user)+ expect(record.shipping_eq_billing_address?).to be(false)+ end+ end++ describe 'mass attribute assignment for bill_address_id, ship_address_id' do+ let(:address) { create(:address, user: user) }++ it 'is able to mass assign bill_address_id' do+ record.update(bill_address_id: address.id)+ expect(record.bill_address_id).to eq address.id+ end++ it 'is able to mass assign ship_address_id' do+ record.update(ship_address_id: address.id)+ expect(record.ship_address_id).to eq address.id+ end+ end++ describe 'same bill & ship addresses' do+ it 'has equal ids when both ids are set' do+ address = create(:address, user: user)+ record.update!(bill_address_id: address.id, ship_address_id: address.id)++ expect(record.bill_address_id).to eq record.ship_address_id+ end+ end++ describe 'editing an address locked by a completed order' do+ let(:address) { create(:address, user: user) }+ let!(:completed_order) { create(:completed_order_with_totals, user: user, ship_address: address) }++ it 'creates a new address with the updated attributes' do+ expect(+ record.update(+ bill_address_attributes: {+ firstname: 'New name',+ **address.attributes.except(+ 'firstname', 'created_at', 'updated_at', 'deleted_at', 'quick_checkout',+ 'public_metadata', 'private_metadata', 'latitude', 'longitude', 'preferences'+ )+ }+ )+ ).to be_truthy++ expect(record.bill_address_id).not_to eq address.id+ expect(record.bill_address.firstname).to eq 'New name'+ end+ endend-RSpec.describe Spree::Purchase::AddressBook do+RSpec.describe Spree::Purchase::Addresses do+ let(:store) { @default_store }+ let(:user) { create(:user) }+context 'included in Spree::Cart' dolet(:record) { create(:cart, store: store, customer: user, ship_address: nil, bill_address: nil) }let(:guest_record) { create(:cart, store: store, customer: nil) }- it_behaves_like 'an address book host'+ it_behaves_like 'an addresses host'++ # Carts expose only use_shipping through the API — the ship→bill clone+ # is the one wired as a callback.+ describe 'use_shipping clone callback' do+ it 'clones the ship address onto the bill address when truthy' do+ record.ship_address = create(:address, user: user)+ record.use_shipping = true+ record.valid?++ expect(record.bill_address).to eq(record.ship_address)+ end+ endendcontext 'included in Spree::Order' dolet(:record) { create(:order, store: store, user: user, ship_address: nil, bill_address: nil) }let(:guest_record) { create(:order, store: store, user: nil, email: '[email protected]') }- it_behaves_like 'an address book host'+ it_behaves_like 'an addresses host'++ describe 'use_billing clone callback (deprecated bridge, removed in 6.1)' do+ let(:order) { Spree::Order.new }++ before do+ order.bill_address = create(:address)+ order.ship_address = nil+ end++ it 'warns on assignment' do+ expect(Spree::Deprecation).to receive(:warn).with(/use_billing is deprecated/)+ order.use_billing = true+ end++ %w[true 1].push(true).each do |truthy_value|+ context "with #{truthy_value.inspect}" do+ before { order.use_billing = truthy_value }++ it 'clones the bill address to the ship address' do+ order.valid?+ expect(order.ship_address).to eq(order.bill_address)+ end+ end+ end++ context "with something other than a 'truthful' value" do+ before { order.use_billing = '0' }++ it 'does not clone the bill address to the shipping' do+ order.valid?+ expect(order.ship_address).to be_nil+ end+ end++ it 'has equal ids when use_billing is active at create' do+ address = create(:address)+ order = create(:order, use_billing: true, bill_address_id: address.id, ship_address_id: nil).reload++ expect(order.bill_address_id).to eq order.ship_address_id+ end+ endendenddiff --git a/spree/core/spec/models/concerns/spree/purchase/purchase_context_spec.rb b/spree/core/spec/models/concerns/spree/purchase/purchase_context_spec.rbindex aac858f0a1..2f6a5297f6 100644--- a/spree/core/spec/models/concerns/spree/purchase/purchase_context_spec.rb+++ b/spree/core/spec/models/concerns/spree/purchase/purchase_context_spec.rb@@ -69,34 +69,63 @@ RSpec.shared_examples 'a currency host' doenddescribe '#currency_must_be_supported_by_store' do- it 'rejects a currency the store does not support' do- record = new_record- record.currency = 'XOF'+ let(:store) { @default_store }++ before do+ allow(store).to receive(:supported_currencies).and_return(['EUR', 'USD'])+ end++ it 'is valid when the currency is supported by the store' do+ expect(new_record(currency: 'EUR')).to be_valid+ end++ it 'is invalid when the currency is not supported by the store' do+ record = new_record(currency: 'JPY')expect(record).not_to be_valid- expect(record.errors[:currency]).to be_present+ expect(record.errors[:currency]).to include(Spree.t(:currency_not_supported_by_store))endendendRSpec.shared_examples 'a locale host' do+ let(:store) { @default_store }+describe '#ensure_locale' do- it 'fills a blank locale from the current locale or market default' do- record = new_record- record.locale = nil+ it 'sets locale from Spree::Current.locale when blank' do+ allow(Spree::Current).to receive(:locale).and_return('fr')+ allow(store).to receive(:supported_locales_list).and_return(['en', 'fr'])++ record = new_record(locale: nil)record.valid?- expect(record.locale).to be_present+ expect(record.locale).to eq('fr')+ end++ it 'does not override locale when already set' do+ allow(Spree::Current).to receive(:locale).and_return('fr')++ record = new_record(locale: 'en')+ record.valid?++ expect(record.locale).to eq('en')endenddescribe '#locale_must_be_supported_by_store' do- it 'rejects a locale the store does not support' do- record = new_record- record.locale = 'xx'+ before do+ allow(store).to receive(:supported_locales_list).and_return(['en', 'fr'])+ end++ it 'is valid when the locale is supported by the store' do+ expect(new_record(locale: 'fr')).to be_valid+ end++ it 'is invalid when the locale is not supported by the store' do+ record = new_record(locale: 'de')expect(record).not_to be_valid- expect(record.errors[:locale]).to be_present+ expect(record.errors[:locale]).to include(Spree.t(:locale_not_supported_by_store))endendenddiff --git a/spree/core/spec/models/spree/cart_spec.rb b/spree/core/spec/models/spree/cart_spec.rbindex 5f63de44f0..772fed64a3 100644--- a/spree/core/spec/models/spree/cart_spec.rb+++ b/spree/core/spec/models/spree/cart_spec.rb@@ -302,24 +302,6 @@ describe Spree::Cart, type: :model doendend- describe '#shipping_eq_billing_address?' do- it 'compares the two addresses' do- address = create(:address)- expect(build(:cart, ship_address: address, bill_address: address).shipping_eq_billing_address?).to be(true)- expect(build(:cart, ship_address: address, bill_address: create(:address)).shipping_eq_billing_address?).to be(false)- end- end-- describe '#use_shipping?' do- it 'accepts the truthy form values' do- expect(build(:cart, use_shipping: true).use_shipping?).to be(true)- expect(build(:cart, use_shipping: 'true').use_shipping?).to be(true)- expect(build(:cart, use_shipping: '1').use_shipping?).to be(true)- expect(build(:cart, use_shipping: false).use_shipping?).to be(false)- expect(build(:cart).use_shipping?).to be(false)- end- end-describe '#quantity' doit 'sums line item quantities' docart = create(:cart_with_line_items, line_items_count: 2)diff --git a/spree/core/spec/models/spree/order/address_spec.rb b/spree/core/spec/models/spree/order/address_spec.rbdeleted file mode 100644index 0b7d66e575..0000000000--- a/spree/core/spec/models/spree/order/address_spec.rb+++ /dev/null@@ -1,116 +0,0 @@-require 'spec_helper'--describe Spree::Order, type: :model do- let(:order) { Spree::Order.new }-- context 'validation' do- context 'when @use_billing is populated' do- before do- order.bill_address = create(:address)- order.ship_address = nil- end-- context 'with true' do- before { order.use_billing = true }-- it 'clones the bill address to the ship address' do- order.valid?- expect(order.ship_address).to eq(order.bill_address)- end- end-- context "with 'true'" do- before { order.use_billing = 'true' }-- it 'clones the bill address to the shipping' do- order.valid?- expect(order.ship_address).to eq(order.bill_address)- end- end-- context "with '1'" do- before { order.use_billing = '1' }-- it 'clones the bill address to the shipping' do- order.valid?- expect(order.ship_address).to eq(order.bill_address)- end- end-- context "with something other than a 'truthful' value" do- before { order.use_billing = '0' }-- it 'does not clone the bill address to the shipping' do- order.valid?- expect(order.ship_address).to be_nil- end- end- end- end-- context 'address book' do- let(:order) { create(:order) }- let(:address) { create(:address, user: order.user) }-- describe 'mass attribute assignment for bill_address_id, ship_address_id' do- it 'is able to mass assign bill_address_id' do- params = { bill_address_id: address.id }- order.update(params)- expect(order.bill_address_id).to eq address.id- end-- it 'is able to mass assign ship_address_id' do- params = { ship_address_id: address.id }- order.update(params)- expect(order.ship_address_id).to eq address.id- end- end-- describe 'Create order with the same bill & ship addresses' do- it 'has equal ids when set ids' do- address = create(:address)- @order = create(:order, bill_address_id: address.id, ship_address_id: address.id)- expect(@bill_address_id).to eq @order.ship_address_id- end-- it 'has equal ids when option use_billing is active' do- address = create(:address)- @order = create(:order, use_billing: true,- bill_address_id: address.id,- ship_address_id: nil)- @order = @order.reload- expect(@order.bill_address_id).to eq @order.ship_address_id- end- end-- context 'when user wants to update firstname of the address with already completed order' do- let(:address) { create(:address, user: order.user) }- let!(:completed_order) { create(:completed_order_with_totals, user: order.user, ship_address: address) }-- it 'creates new address with updated attributes' do- expect(- order.update(- bill_address_attributes: {- firstname: 'New name',- **address.attributes.except(- 'firstname',- 'created_at',- 'updated_at',- 'deleted_at',- 'quick_checkout',- 'public_metadata',- 'private_metadata',- 'latitude',- 'longitude',- 'preferences'- )- }- )- ).to be_truthy-- expect(order.bill_address_id).not_to eq address.id- expect(order.bill_address.firstname).to eq 'New name'- end- end- end-enddiff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rbindex 92c4769d7e..d64adad4ad 100644--- a/spree/core/spec/models/spree/order_spec.rb+++ b/spree/core/spec/models/spree/order_spec.rb@@ -92,75 +92,6 @@ describe Spree::Order, type: :model doendend- describe 'Validations' do- describe '#currency_must_be_supported_by_store' do- let(:order) { build(:order, store: store, currency: currency) }-- before do- allow(store).to receive(:supported_currencies).and_return(['EUR', 'USD'])- end-- context 'when currency is supported by the store' do- let(:currency) { 'EUR' }-- it 'is valid' do- expect(order).to be_valid- end- end-- context 'when currency is not supported by the store' do- let(:currency) { 'JPY' }-- it 'is invalid' do- expect(order).not_to be_valid- expect(order.errors[:currency]).to include(Spree.t(:currency_not_supported_by_store))- end- end- end-- describe '#locale_must_be_supported_by_store' do- let(:order) { build(:order, store: store, locale: locale) }-- before do- allow(store).to receive(:supported_locales_list).and_return(['en', 'fr'])- end-- context 'when locale is supported by the store' do- let(:locale) { 'fr' }-- it 'is valid' do- expect(order).to be_valid- end- end-- context 'when locale is not supported by the store' do- let(:locale) { 'de' }-- it 'is invalid' do- expect(order).not_to be_valid- expect(order.errors[:locale]).to include(Spree.t(:locale_not_supported_by_store))- end- end- end-- describe '#ensure_locale_presence' do- it 'sets locale from Spree::Current.locale when blank' do- allow(Spree::Current).to receive(:locale).and_return('fr')- order = build(:order, store: store, locale: nil)- allow(store).to receive(:supported_locales_list).and_return(['en', 'fr'])- order.valid?- expect(order.locale).to eq('fr')- end-- it 'does not override locale when already set' do- allow(Spree::Current).to receive(:locale).and_return('fr')- order = build(:order, store: store, locale: 'en')- order.valid?- expect(order.locale).to eq('en')- end- end- end-describe '#full_name' dosubject { order.full_name }