Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Drop a redundant optional: false on the tax identifier owner

Damian Legawiec3a224b126185
diff --git a/docs/plans/decisions.md b/docs/plans/decisions.md
index 96a0cb2bea..f9290f0702 100644
--- a/docs/plans/decisions.md
+++ b/docs/plans/decisions.md
@@ -7,20 +7,20 @@
Three findings killed the STI argument, all of them checkable in code that already existed:
1. **Checkout copies attributes, not records.** `Spree::Purchase::Addresses#update_or_create_address` takes a params hash and ends at `Spree::Address.create(attributes)`, hardcoded to the base class. A company location's address entering checkout is reduced to a hash and reborn as a plain `Address`; the `type` never travels. The premise of the STI conversion — that the class survives the copy — was false on the only path that matters.
-2. **Keeping `type` significant for dedup breaks address-book reuse.** `find_existing_address` matches on all attributes. With a `type` column, a business address and a personal address with identical postal details never dedupe, so a B2B buyer re-creates a row on every order.
+2. **Keeping `type` significant for dedup breaks address-book reuse.** Deduplication matches on all attributes (`Spree::Address.find_duplicate`, which spree/spree#14486 consolidated from the three divergent copies that existed while this was being decided). With a `type` column, a business address and a personal address with identical postal details never dedupe, so a B2B buyer re-creates a row on every order.
3. **The tax decision never consults the address class.** Per `6.1-channels-catalogs-b2b.md`, exemption hangs off `Company` and `resolved_tax_identifier` walks order → company → customer. The address's Ruby class contributes nothing.
The base `Address` has carried a `company` column and preference-driven `require_company?` / `show_company_address_field?` predicates all along, so the mechanism for "this address belongs to a business" already existed and was already configurable. STI bought two boolean overrides at the cost of a column on one of the most-written tables in Spree, plus knock-on edits to `empty?`, the exclusion-list constants, `json_api_columns`, factories and six specs.
**Rejected:** keeping STI and teaching `update_or_create_address` to preserve the subclass (it fixes the copy path but leaves finding 2, and still buys nothing the owner does not already say).
-**Consequences:** `Seller#billing_address=` keeps coercing an assigned plain `Address` with `becomes` — necessary precisely because rows are indistinguishable without a type. `CompanyLocation`'s addresses stay `Spree::Address`; if a mandatory company line is wanted there, it belongs on the owner as a validation, not on the address class. **Constraint:** do not add a `type` column to `spree_addresses` without first checking `update_or_create_address` and `find_existing_address` — the copy path and the dedup path are what decide this, not the model hierarchy.
+**Consequences:** `Seller#billing_address=` keeps coercing an assigned plain `Address` with `becomes` — necessary precisely because rows are indistinguishable without a type. `CompanyLocation`'s addresses stay `Spree::Address`; if a mandatory company line is wanted there, it belongs on the owner as a validation, not on the address class. **Constraint:** do not add a `type` column to `spree_addresses` without first checking `update_or_create_address` and `Spree::Address.find_duplicate` — the copy path and the dedup path are what decide this, not the model hierarchy.
## 2026-08-22: The tax identifier's owner is polymorphic
**Context:** `Spree::TaxIdentifier` carried one nullable FK per owner — customer, company, cart, order — with an `exactly_one_owner` validator holding the rule together, following the dual-FK convention from the Cart/Order split. The marketplace work added a fifth owner (Seller, per `6.0-multi-vendor-marketplace.md` Decision 12), which made the shape hard to defend: five columns, three near-identical per-owner uniqueness validations, and an `owner` method that was a five-way `||` chain.
-**Decision:** The table carries `owner_type`/`owner_id`. The dual-FK convention was adopted for cases where a polymorphic type string was hiding several different meanings in one table — split-adjustments removing `adjustable_type`, typed stock movements removing `originator_type`, each with methods dispatching on the string to tell six things apart. A tax identifier is not that: it is one concept, a tax registration, held by different kinds of party. The exactly-one rule becomes structural (a single FK cannot name two owners, and `optional: false` is explicit because `Spree::Base` turns off `belongs_to_required_by_default`), the three uniqueness validations collapse into `scope: [:owner_type, :owner_id]`, and a sixth owner costs no migration. Cardinality still varies by owner and is declared owner-side, where it always lived: `has_many` for customer, company and seller, `has_one` for cart and order. Because 6.0 is unreleased, the original `create_table` migration was amended in place rather than layering a conversion migration.
+**Decision:** The table carries `owner_type`/`owner_id`. The dual-FK convention was adopted for cases where a polymorphic type string was hiding several different meanings in one table — split-adjustments removing `adjustable_type`, typed stock movements removing `originator_type`, each with methods dispatching on the string to tell six things apart. A tax identifier is not that: it is one concept, a tax registration, held by different kinds of party. The exactly-one rule becomes structural — a single FK cannot name two owners, and the owner is required by Rails' `belongs_to` default (spree/spree#14483 removed `Spree::Base`'s opt-out the same week), the three uniqueness validations collapse into `scope: [:owner_type, :owner_id]`, and a sixth owner costs no migration. Cardinality still varies by owner and is declared owner-side, where it always lived: `has_many` for customer, company and seller, `has_one` for cart and order. Because 6.0 is unreleased, the original `create_table` migration was amended in place rather than layering a conversion migration.
**Rejected:** keeping five FKs on consistency grounds alone (the convention's rationale does not reach this table); a shared `exactly_one_owner` concern (it would formalise the workaround rather than remove it).
diff --git a/spree/core/app/models/spree/tax_identifier.rb b/spree/core/app/models/spree/tax_identifier.rb
index 4f69b9815d..99526bd74b 100644
--- a/spree/core/app/models/spree/tax_identifier.rb
+++ b/spree/core/app/models/spree/tax_identifier.rb
@@ -48,7 +48,7 @@ module Spree
# per owner: there is one relationship here with two cardinalities — a
# customer, company or seller holds one per kind, a cart or order holds
# one — and a further owner should cost no schema change.
- belongs_to :owner, polymorphic: true, optional: false
+ belongs_to :owner, polymorphic: true
# Whitespace and case only. Punctuation is deliberately kept: for several
# kinds it is part of the canonical number (Switzerland's CHE-123.456.789