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.

19.6 KB
Raw Blame

Store-Scoped Configuration

Status: In Progress — Phases 1 and 2 implemented (2026-08-12): all eight movers live on Store, exposed in the Admin API and dashboard. The two capture booleans were folded into one capture_method string on 2026-08-13. Phase 3 (deleting the globals) remains for 6.1 Target: Spree 6.0 (globals deleted in 6.1) Depends on: Single-owner Product (6.0-channels-catalogs-b2b.md, shipped 5.5), Cart/Order split (6.0-cart-order-split.md), Tax provider (6.0-tax-provider.md, for the tax_using_ship_address retirement) Author: Damian + Claude Last updated: 2026-08-12

Summary

A full usage audit of Spree::Config (2026-08-04, multi-agent trace with adversarial verification over all 68 preferences) found 38 genuinely live settings, 6 that only subsystems 6.0 replaced ever read, and 7 with zero read sites anywhere. The live settings split into two kinds: application configuration (security, limits, jobs, URLs — properties of the installation) and commerce behavior (capture timing, inventory tracking, catalog visibility — properties of a store). Now that Products, Promotions, PaymentMethods and StockLocations are single-store resources, a global flag for per-store behavior is a design smell. This plan moves eight commerce-behavior globals to Spree::Store preferences — finishing the migration that company → company_field_enabled and allow_guest_checkout → guest_checkout already started — and retires the dead ones.

Problem

One finding motivates the whole plan. default_stock_reservation_ttl_minutes is read only when the Store preference it backs is blank — but Store#stock_reservation_ttl_minutes carries default: 10 and a greater_than: 0 validation, so it is never blank. The global has been silently unreachable for every order with a store, and because both default to 10 nobody noticed.

That is what two sources of truth for one behavior produces: one quietly wins, the other becomes a lie in the documentation. The audit found the same shape forming elsewhere — auto_capture exists both globally and as a per-payment-method column, and the dashboard only exposes the column.

Classification test

Would a second store on the same installation plausibly want a different value?

  • Yes → Store preference (an EU store needs track_price_history; its non-EU sibling doesn't)
  • No → stays in Spree.config (password length protects the app, not a shop)

Key Decisions (do not deviate without discussion)

  • The eight movers (names and defaults unchanged on Store, except the two capture booleans, which land on one string — see below):

    Global today Store preference Default
    auto_capture + auto_capture_on_dispatch preferred_capture_method 'checkout'
    track_inventory_levels preferred_track_inventory_levels true
    stock_reservations_enabled preferred_stock_reservations_enabled true
    track_price_history preferred_track_price_history true
    show_products_without_price preferred_show_products_without_price false
    address_requires_phone preferred_address_requires_phone false
    disable_sku_validation preferred_disable_sku_validation false
  • Store preference is authoritative — no runtime fallback to the global (decided 2026-08-04). Core reads the store preference and the declared default, never the old global. A fallback chain would reintroduce exactly the drift that made default_stock_reservation_ttl_minutes unreachable. The company precedent already works this way.

  • A backfill rake carries existing installs over, not a fallback: spree:store_settings:backfill_from_config copies each global whose value differs from its default onto every store. Added to the 5.6→6.0 upgrade manifest (same mechanism as sanitize_rich_text). Idempotent; logs each store and setting it writes.

  • Globals become deprecated shells for one release (deprecated: 'Set …on the store' in the preference DSL, which warns through Spree::Deprecation on access), deleted in 6.1. Repo-wide deprecation-bridge rule.

  • The two capture booleans became one capture_method string (decided 2026-08-13, reversing this plan's own one-preference-per-global rule for this pair). auto_capture and auto_capture_on_dispatch encoded three behaviors in four combinations, one of which contradicted itself: with both on, money was already taken at checkout and the dispatch capture was a dead no-op. Worse, the third real behavior — authorize now, let staff charge later — existed only as "turn both off", which no merchant would discover. The vocabulary is checkout | on_dispatch | manual (Spree::CaptureMethod, matching Shopify's three-option payment-capture setting), so the contradictory state is unrepresentable and the manual mode is visible. Both old names survive one release as deprecated Store accessors that derive from the string; a boolean write cannot express the full vocabulary, so preferred_auto_capture = false keeps an existing on_dispatch choice rather than guessing.

  • capture_method resolution chain: payment method column when set → legacy auto_capture column when not nil → store preference → 'checkout'. The method-level override moved from a boolean column to the same three-value vocabulary (nullable = inherit, mirroring Channel::Gating), because a boolean there could not express "charge this method on dispatch". PaymentMethod#resolved_capture_method is the reader; auto_capture? stays as a deprecated question and deliberately does not warn — it runs on every payment and would flood the logs.

  • On PaymentMethod it is a column, not a preference (decided 2026-08-13, correcting a first pass that used a preference). auto_capture was a column beside active and position, and the replacement stays one: the preferences blob on a payment method holds per-provider gateway credentials, so a preference there rendered a core setting as a credential field in the provider configuration form and needed an exclusion list in PreferenceSchema to hide it — the escape hatch was the signal that the storage was wrong. A column is also queryable, which the per-payment dispatch check wants, and null carries the inherit meaning the boolean already had. On Store it stays a preference, because that is how every store setting is stored. spree:migrate_capture_methods copies auto_capture: true to 'checkout' and leaves false rows empty — the boolean only recorded "not at checkout" and could not distinguish dispatch from manual, so those rows keep inheriting the store's choice.

  • Dispatch capture is decided per payment, not store-wide (decided 2026-08-13). Fulfillment#process_order_payments captures only pending payments whose method resolves to on_dispatch, so a method set to manual is never swept up by a dispatch — which is the entire reason a merchant picks it. The dispatch readiness guard follows the same rule: an order that is authorized but uncaptured may be handed over when a payment method deliberately charges later, instead of when a store-wide boolean happens to be on.

  • Storeless contexts read Spree::Current.store, falling back to the declared default (decided 2026-08-04). Two readers have no record-level store: Address (no store association) and the Product availability scope (class-level). Both resolve through Spree::Current.store. Accepted trade-off: validation or a catalog query run outside a request — a console session, a seed, a job that forgets to set the store — silently uses the default rather than the store's value. This is why the alternative (threading a store argument through every caller, breaking a public scope) was rejected as disproportionate. Any job that validates addresses or queries the catalog must set Spree::Current.store.

  • default_stock_reservation_ttl_minutes is deprecated, not moved (done 2026-08-12). Store#stock_reservation_ttl_minutes already owns the value. StockReservation.ttl_for dropped the global read and keeps its hard 10-minute floor for storeless contexts.

  • track_price_history goes to Store in 6.0, with Market as a noted future refinement (decided 2026-08-04). EU Omnibus is per-country law, and Market already owns return_window_days and the other legal settings — but prices are not market-scoped today, so a Market home needs design work this plan will not carry. Store now; revisit when price/market scoping is on the table.

  • Stay global despite looking behavioral: credit_to_new_allocation (ledger-shape convention — per-store variance makes one install's accounting inconsistent), non_expiring_credit_types (reference data — since deprecated outright on 2026-08-18 when store credit categories were removed) and geocode_addresses (decided 2026-08-12 — geocoding is infrastructure: it depends on the install's provider credentials and quota, not on how a shop sells).

  • allow_checkout_on_gateway_error is dropped rather than moved (decided 2026-08-12, reversing this plan's own list). Nothing reads it in Spree 6. It used to gate a state-machine transition through the return value of process_payments!, but both completion workflows now discard that value and check whether payments actually cover the total (Carts::Complete#process_payments, Orders::Complete#process_payments) — and the rescue adds the gateway message to errors regardless, which Orders::Complete fails on. So with the flag on, a gateway error still cannot complete an order. Shipping it as a store preference would have put a dead toggle in the dashboard, which is worse than a dead global. Deprecated as "nothing reads this"; restoring the behavior means teaching the workflows to honor it, which is a feature, not a settings move.

  • address_requires_state is dropped rather than moved (decided 2026-08-12). It was already deprecated and gated a check the country's own states_required flag decides. Two switches for one question is the drift this plan exists to remove, so Address#state_validate now reads the country alone. Installs that set it to false while their countries required states will see those addresses start failing validation — the country flag is the place to change.

  • The seven dead settings got deprecation shells in 6.0 (done 2026-08-12), deleted in 6.1: products_per_page, alternative_shipping_phone, show_variant_full_price, reserve_stock_on, storefront_products_path, storefront_taxons_path, storefront_pages_path. Zero read sites, audit-verified including dynamic-lookup forms. Shells only so an initializer that sets one doesn't crash boot mid-upgrade.

  • Already deprecated this cycle, recorded here for completeness: tax_using_ship_address (the tax provider owns address choice per market), allow_empty_price_amount (price lists already allow blank), always_include_confirm_step (confirm is an advertised step, never enforced — the storefront renders a review screen when it wants one), restock_inventory / return_eligibility_number_of_days / expedited_exchanges / expedited_exchanges_days_window (the returns rework owns these), use_user_locale (locale comes from x-spree-locale or the locale param), enable_legacy_default_price (leave off).

  • New behavior flags never start global. From 6.0 on, a setting that shapes how a store sells is born on Store (or Market/Channel where regional), never in Spree.config.

Design Details

Read sites re-pointed (all done 2026-08-12)

Setting Read sites Store reached via
capture_method payment_method.rb (resolved_capture_method), fulfillment.rb (process_order_payments), fulfillments/fulfill.rb PaymentMethod is store-owned; fulfillment → order/cart → store
track_inventory_levels variant.rb:669, product.rb:868 variant → product → store
stock_reservations_enabled stock_reservations/reserve.rb:11, extend.rb:11, stock/quantifier.rb:69 cart/order → store; stock_item → stock_location → store
track_price_history price.rb:189 price → variant → product → store
show_products_without_price product_scopes.rb:322 Spree::Current.store (class-level scope)
address_requires_phone address.rb:66,210, addresses/phone_validator.rb:11 Spree::Current.store (no store association)
disable_sku_validation variant.rb:84,774 variant → product → store

Product.store is optional: true (product/channels.rb:12), so every re-pointed reader keeps the preference default as its nil-store fallback.

Admin API + Dashboard

Store preferences already flow through the Admin API v3 store settings surface and the dashboard settings pages. Phase 2 adds the eight to the store serializer and permitted params, and surfaces them in the relevant dashboard sections (payments, inventory, catalog, checkout). Serializer changes ride the type-generation pipeline.

Incidental fixes (do regardless of the move)

  • coupon_codes_total_limit is interpolated into the Promotion numericality validation at class-load time (promotion.rb:68), so setting it in an initializer that loads later has no effect until restart. Make the validation option a lambda.
  • The definition comment on disable_sku_validation (configuration.rb:52, "when turned off disables") is backwards — true disables the validation.
  • storefront_products_path isn't merely unread: base_helper.rb:37 and the Google feed presenter hardcode /products/, so overriding it today silently produces broken feed URLs. Deleting it makes that explicit.

Migration Path

Phase 1 (6.0): move + deprecate

Shipped 2026-08-12. Spree::StorePreferences is the shared reader: including models define preference_store to say how they reach a store (Variant through its product, Price through variant → product, Fulfillment through its owner), and Spree::StorePreferences.read(store, name) falls back to the preference's declared default when there is no store — so a console session, a seed, or a job that never set the ambient store gets the default instead of an exception. Class-level callers with no record use .current(name), which resolves Spree::Current.store.

The two resolve differently and the difference matters for address validation and product availability: Spree::Current.store itself falls back to Spree::Store.default, so .current(name) returns the default store's configured value and only reaches the declared default when the installation has no default store at all. .read(nil, name) always returns the declared default. Prefer .current when the ambient store is the right answer, and .read(record_store, name) when a specific record's store is.

Two fixes rode along: Address#show_company_address_field? used to raise on a storeless read (Spree::Store.current.prefers_…), and the product availability scope took its fallback currency from Spree::Store.default rather than the ambient store, so a multi-store install filtered the catalog by the wrong shop's currency.

The backfill has a wrinkle worth knowing: a store writes every declared default into its preferences hash the moment it is instantiated, so the key is always present and nothing distinguishes "the merchant chose this value" from "this is the seeded default" — not preferences.key?, and not comparing against preference_default. There is no explicit-vs-seeded flag to consult. spree:store_settings:backfill_from_config therefore records a marker in store metadata and visits each store at most once. The residual risk is one-directional and small: on the single run, a store whose value happens to equal the default takes the global's value. Afterwards the marker protects every later change. Two settings were also renamed on the way over (company → company_field_enabled, default_stock_reservation_ttl_minutes → stock_reservation_ttl_minutes), so the task maps global names to preference names rather than assuming they match.

Specs drive these through stub_store_preferences (Spree::TestingSupport::Preferences), which stubs rather than writes — the default store is shared suite-wide, so persisting a setting in one example would change the next. The stub is keyed on store id (code under test reaches its store through an association or a reload, so it holds a different instance of the same row) and leaves other stores and unnamed preferences answering for themselves.

  1. Add the eight preferences to Spree::Store with defaults matching the globals.
  2. Re-point the read sites per the table above; Address and the product scope read Spree::Current.store.
  3. Mark the eight globals plus the dead settings deprecated: in spree/core/lib/spree/core/configuration.rb.
  4. Ship spree:store_settings:backfill_from_config and add it to the 5.6→6.0 upgrade manifest.
  5. StockReservation.ttl_for drops its global read.

Phase 2 (6.0): expose

  1. Admin API v3 store serializer and permitted params for the eight; regenerate types.
  2. Dashboard settings pages gain the new toggles in their natural sections.
  3. Docs: docs/v6/developer/customization/configuration.mdx already carries the commerce/application split — move the eight into the store-settings docs once shipped.

Phase 3 (6.1): delete

  1. Delete the eight deprecated globals, the dead settings, and the legacy shells listed above.

Constraints on Current Work

  • Don't add new Spree::Config reads of the movers — they are deprecated and no longer read by core. Read them through Spree::StorePreferences (include the concern and define preference_store, or call .read/.current).
  • New behavior flags go on Store from the start — never add a commerce-behavior preference to Spree.config.
  • Don't build UI or code against auto_capture / auto_capture_on_dispatch. Both are deprecated on Store and on PaymentMethod. Read resolved_capture_method (or the capture_at_checkout? / capture_on_dispatch? / capture_manually? questions); write capture_method. A new setting that means "when does money move" belongs in the Spree::CaptureMethod vocabulary, never as another boolean beside it.
  • Background jobs that validate addresses or query the catalog must set Spree::Current.store — after Phase 1 those paths read the ambient store and will otherwise silently use defaults.
  • The v6 configuration docs page keeps its commerce/application two-group structure; new settings slot into the right group.

Open Questions

None blocking. Settled 2026-08-04: storeless reads use Spree::Current.store with a default fallback; track_price_history goes to Store with Market noted as a future refinement; upgrade is a backfill rake with no runtime fallback.

Deferred, not blocking:

  • Market-scoped track_price_history. Revisit when price/market scoping is designed; needs prices to be market-aware first.
  • Cart expiry settings (guest_cart_expiry_days and siblings) stay application housekeeping. Per-store retention deferred until someone asks.

References

  • Config usage audit, 2026-08-04 — multi-agent trace with adversarial verification; 38 active / 6 legacy / 7 unused of 51 non-deprecated preferences
  • Precedents: company → Store#company_field_enabled (configuration.rb:47), allow_guest_checkout → Store#guest_checkout, Store#stock_reservation_ttl_minutes (store.rb:58)
  • 5.6-6.0-single-store-promotions-payment-methods.md — the backfill + deprecation-bridge pattern this plan reuses
  • 6.0-tax-provider.md — owns the tax_using_ship_address retirement
  • 6.0-returns-exchanges-claims.md — owns the returns-related retirements
  • 6.0-stock-reservations.md — carries the superseding note for reserve_stock_on and the TTL global
  • Docs page: docs/v6/developer/customization/configuration.mdx