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.

Make SingleStoreResource concern now useful for something

Damian Legawiec0ecc01c87bd8
diff --git a/docs/plans/5.6-6.0-single-store-promotions-payment-methods.md b/docs/plans/5.6-6.0-single-store-promotions-payment-methods.md
index ebee807aa6..b4da19a1e0 100644
--- a/docs/plans/5.6-6.0-single-store-promotions-payment-methods.md
+++ b/docs/plans/5.6-6.0-single-store-promotions-payment-methods.md
@@ -77,7 +77,7 @@ Both models adopt the existing `Spree::SingleStoreResource` concern (already use
include Spree::SingleStoreResource # for_store + ensure_store + immutability
belongs_to :store, class_name: 'Spree::Store'
-validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
+validates :store, presence: true
include Spree::LegacyMultiStoreSupport unless defined?(SpreeMultiStore)
```
diff --git a/docs/plans/6.0-delivery-rate-provider.md b/docs/plans/6.0-delivery-rate-provider.md
index f06415b20b..fdb23f3381 100644
--- a/docs/plans/6.0-delivery-rate-provider.md
+++ b/docs/plans/6.0-delivery-rate-provider.md
@@ -176,7 +176,7 @@ class Spree::ShippingMethod < Spree.base_class
belongs_to :store, class_name: 'Spree::Store'
- validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
+ validates :store, presence: true
end
```
@@ -436,7 +436,7 @@ The Store API already speaks this vocabulary: `Spree::Api::V3::DeliveryRateSeria
### Phase 1 — store ownership (independent, ship first)
- Migration: `add_reference :spree_shipping_methods, :store, null: true, if_not_exists: true`, following `20260628000001_add_store_id_to_spree_promotions.rb`.
-- `ShippingMethod` includes `SingleStoreResource`, declares `belongs_to :store` + presence validation gated on `Spree::Config[:disable_store_presence_validation]`.
+- `ShippingMethod` includes `SingleStoreResource`, declares `belongs_to :store`.
- Backfill rake task. **Unlike Promotion/PaymentMethod there is no join table to read from**, so ownership is derived: assign `Spree::Store.default` when only one store exists (the overwhelmingly common case); where multiple stores exist, use the zone-overlap signal from `Store#countries_with_shipping_coverage` (`store.rb:342`) and log every ambiguous method loudly rather than guessing silently. Idempotent — skip rows that already have `store_id`.
- Update `shipping_method_factory.rb` and the admin controller (`shipping_methods_controller.rb`) to set/scope by store.
- `null: false` is deferred to the 6.0 cleanup, after the vendor interaction is settled.
diff --git a/docs/plans/6.0-product-types.md b/docs/plans/6.0-product-types.md
index 66c11d34ae..eb2b02aa5c 100644
--- a/docs/plans/6.0-product-types.md
+++ b/docs/plans/6.0-product-types.md
@@ -75,7 +75,7 @@ class Spree::ProductType < Spree.base_class
has_many :products, class_name: 'Spree::Product', dependent: :restrict_with_error
validates :name, presence: true, uniqueness: { scope: spree_base_uniqueness_scope }
- validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
+ validates :store, presence: true
validates :fulfillment_types, presence: true
attribute :fulfillment_types, default: ['shipping'] # model-level default; column has none (CLAUDE.md rule)
diff --git a/spree/core/app/models/concerns/spree/legacy_multi_store_support.rb b/spree/core/app/models/concerns/spree/legacy_multi_store_support.rb
deleted file mode 100644
index 07767f51a1..0000000000
--- a/spree/core/app/models/concerns/spree/legacy_multi_store_support.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-module Spree
- # Backwards-compatible shim for resources migrated from a many-to-many store
- # relationship to a single-store +belongs_to :store+ FK (Spree::Product in 5.5,
- # Spree::Promotion and Spree::PaymentMethod in 5.6). Maps the historic
- # +stores+/+store_ids+ accessors onto the singular +store+/+store_id+, warning
- # on use. The +spree_multi_store+ extension defines +SpreeMultiStore+ to
- # suppress the include and restore the real +has_many :stores+ association.
- module LegacyMultiStoreSupport
- extend ActiveSupport::Concern
-
- included do
- # Legacy accessors for the many-to-many store relationship. These are deprecated
- # and will be removed in Spree 6.0 once the models finish migrating to single-store ownership.
- # @return [ActiveRecord::Relation<Spree::Store>] the single store wrapped in a relation, or an empty relation if no store is set
- def stores
- Spree::Deprecation.warn(
- "#{self.class.base_class}#stores is deprecated. Please use #{self.class.base_class}#store instead. " \
- 'If you want to continue using multiple stores please install spree_multi_store gem'
- )
- store ? Spree::Store.where(id: store.id) : Spree::Store.none
- end
-
- def store_ids
- Spree::Deprecation.warn(
- "#{self.class.base_class}#store_ids is deprecated. Please use #{self.class.base_class}#store_id instead. " \
- 'If you want to continue using multiple stores please install spree_multi_store gem'
- )
- store_id ? [store_id] : []
- end
-
- def stores=(stores)
- Spree::Deprecation.warn(
- "#{self.class.base_class}#stores= is deprecated. Please use #{self.class.base_class}#store= instead. " \
- 'If you want to continue using multiple stores please install spree_multi_store gem'
- )
- self.store = Array(stores).compact.first
- end
-
- def store_ids=(store_ids)
- Spree::Deprecation.warn(
- "#{self.class.base_class}#store_ids= is deprecated. Please use #{self.class.base_class}#store_id= instead. " \
- 'If you want to continue using multiple stores please install spree_multi_store gem'
- )
- self.store_id = Array(store_ids).compact_blank.first
- end
- end
- end
-end
diff --git a/spree/core/app/models/concerns/spree/single_store_resource.rb b/spree/core/app/models/concerns/spree/single_store_resource.rb
index 29f5137abe..bb2b7893ee 100644
--- a/spree/core/app/models/concerns/spree/single_store_resource.rb
+++ b/spree/core/app/models/concerns/spree/single_store_resource.rb
@@ -3,7 +3,10 @@ module Spree
extend ActiveSupport::Concern
included do
+ belongs_to :store, class_name: 'Spree::Store'
+
before_validation :ensure_store, unless: :store_id?
+ validates :store, presence: true
validate :ensure_store_association_is_not_changed
scope :for_store, ->(store) { where(store_id: store.id) }
diff --git a/spree/core/app/models/concerns/spree/store_scoped_resource.rb b/spree/core/app/models/concerns/spree/store_scoped_resource.rb
deleted file mode 100644
index 3bd5e4abdd..0000000000
--- a/spree/core/app/models/concerns/spree/store_scoped_resource.rb
+++ /dev/null
@@ -1,37 +0,0 @@
-module Spree
- # @deprecated Use a single-store +belongs_to :store+ FK instead. This concern
- # modelled the historic many-to-many store relationship (Promotion,
- # PaymentMethod) and is removed in Spree 6.0 once those models finish
- # migrating to single-store ownership. See
- # docs/plans/5.6-6.0-single-store-promotions-payment-methods.md.
- module StoreScopedResource
- extend ActiveSupport::Concern
-
- included do
- Spree::Deprecation.warn(
- "Spree::StoreScopedResource is deprecated and will be removed in Spree 6.0. " \
- "Use a single-store `belongs_to :store` FK instead (see Spree::Promotion / Spree::PaymentMethod)."
- )
-
- scope :for_store, ->(store) { joins(:stores).where(Store.table_name => { id: store.id }) }
-
- before_validation :set_default_store, if: :new_record?
- end
-
- protected
-
- def set_default_store
- return if disable_store_presence_validation?
- # records built through a store's association (`store.payment_methods.build`)
- # carry their link on the join association — `stores` can't see it until save
- join_association = self.class.reflect_on_association(:stores).through_reflection.name
- return if stores.any? || send(join_association).any?
-
- stores << Spree::Store.default
- end
-
- def disable_store_presence_validation?
- Spree::Config[:disable_store_presence_validation]
- end
- end
-end
diff --git a/spree/core/app/models/spree/api_key.rb b/spree/core/app/models/spree/api_key.rb
index 7b2e4f7d15..f5767c72af 100644
--- a/spree/core/app/models/spree/api_key.rb
+++ b/spree/core/app/models/spree/api_key.rb
@@ -2,6 +2,8 @@ module Spree
class ApiKey < Spree.base_class
has_prefix_id :key # Spree-specific: api key
+ include Spree::SingleStoreResource
+
KEY_TYPES = %w[publishable secret].freeze
PREFIXES = { 'publishable' => 'pk_', 'secret' => 'sk_' }.freeze
TOKEN_LENGTH = 24
@@ -60,7 +62,6 @@ module Spree
validates :token, presence: true, uniqueness: { scope: spree_base_uniqueness_scope }, if: :publishable?
validates :token_digest, presence: true, uniqueness: true, if: :secret?
validates :token_prefix, presence: true, if: :secret?
- validates :store, presence: true
validates :scopes, presence: true, if: -> { secret? && scopes_enforceable? }
validate :validate_known_scopes, if: -> { secret? && scopes_enforceable? }
# Scopes are fixed for the life of the secret — mirroring Stripe/GitHub/AWS:
diff --git a/spree/core/app/models/spree/category.rb b/spree/core/app/models/spree/category.rb
index 10e13f4e6f..e9f8d58069 100644
--- a/spree/core/app/models/spree/category.rb
+++ b/spree/core/app/models/spree/category.rb
@@ -57,7 +57,6 @@ module Spree
#
validates :name, presence: true
validates :taxonomy, presence: true, if: :requires_taxonomy?
- validates :store, presence: true
# Taxonomy-backed categories are unique within their taxonomy; taxonomy-less
# categories (store-owned) are unique within their store, so two stores can
# each have a top-level "Shoes".
diff --git a/spree/core/app/models/spree/customer_group.rb b/spree/core/app/models/spree/customer_group.rb
index cbc0b24899..711ea64927 100644
--- a/spree/core/app/models/spree/customer_group.rb
+++ b/spree/core/app/models/spree/customer_group.rb
@@ -4,6 +4,8 @@ module Spree
acts_as_paranoid
+ include Spree::SingleStoreResource
+
#
# Associations
#
@@ -20,12 +22,6 @@ module Spree
# Validations
#
validates :name, presence: true, uniqueness: { scope: [:store_id], conditions: -> { where(deleted_at: nil) } }
- validates :store, presence: true
-
- #
- # Scopes
- #
- scope :for_store, ->(store) { where(store: store) }
#
# Instance Methods
diff --git a/spree/core/app/models/spree/customer_return.rb b/spree/core/app/models/spree/customer_return.rb
index 688e96619c..46d6a00f43 100644
--- a/spree/core/app/models/spree/customer_return.rb
+++ b/spree/core/app/models/spree/customer_return.rb
@@ -2,6 +2,7 @@ module Spree
class CustomerReturn < Spree.base_class
has_prefix_id :cr
+ include Spree::SingleStoreResource
include Spree::Core::NumberGenerator.new(prefix: 'CR', length: 9)
include Spree::NumberIdentifier
include Spree::Metafields
@@ -18,7 +19,6 @@ module Spree
after_create :process_return!
- validates :store, presence: true
validates :return_items, :stock_location, presence: true
validate :must_have_return_authorization, on: :create
validate :return_items_belong_to_same_order
diff --git a/spree/core/app/models/spree/delivery_method.rb b/spree/core/app/models/spree/delivery_method.rb
index d16cdb433d..8cddbe5739 100644
--- a/spree/core/app/models/spree/delivery_method.rb
+++ b/spree/core/app/models/spree/delivery_method.rb
@@ -1,9 +1,9 @@
module Spree
class DeliveryMethod < Spree.base_class
- include Spree::SingleStoreResource
has_prefix_id :dm
acts_as_paranoid
+ include Spree::SingleStoreResource
include Spree::CalculatedAdjustments
include Spree::Metafields
include Spree::Metadata
@@ -28,8 +28,6 @@ module Spree
has_many :delivery_rates, class_name: 'Spree::DeliveryRate', inverse_of: :delivery_method
has_many :fulfillments, through: :delivery_rates, class_name: 'Spree::Fulfillment'
- belongs_to :store, class_name: 'Spree::Store'
-
has_many :delivery_method_stock_locations, class_name: 'Spree::DeliveryMethodStockLocation',
dependent: :destroy, inverse_of: :delivery_method
has_many :delivery_method_rules, class_name: 'Spree::DeliveryMethodRule',
@@ -58,7 +56,6 @@ module Spree
has_many :zones, through: :delivery_method_zones, source: :delivery_zone, deprecated: true
validates :name, :display_on, :fulfillment_type, presence: true
- validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
validates :estimated_transit_business_days_min, numericality: { greater_than_or_equal_to: 1 }, allow_nil: true
validates :estimated_transit_business_days_max, numericality: { greater_than_or_equal_to: 1 }, allow_nil: true
diff --git a/spree/core/app/models/spree/delivery_zone.rb b/spree/core/app/models/spree/delivery_zone.rb
index 744ac75d0c..4b79633321 100644
--- a/spree/core/app/models/spree/delivery_zone.rb
+++ b/spree/core/app/models/spree/delivery_zone.rb
@@ -15,7 +15,7 @@ module Spree
has_many :delivery_methods, through: :delivery_method_zones, class_name: 'Spree::DeliveryMethod'
validates :name, presence: true, uniqueness: { scope: [:store_id, *spree_base_uniqueness_scope] }
- validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
+ validates :store, presence: true
self.whitelisted_ransackable_attributes = %w[name]
diff --git a/spree/core/app/models/spree/payment_method.rb b/spree/core/app/models/spree/payment_method.rb
index 3bd5cbac46..7d80067e3c 100644
--- a/spree/core/app/models/spree/payment_method.rb
+++ b/spree/core/app/models/spree/payment_method.rb
@@ -12,8 +12,6 @@ module Spree
if defined?(Spree::Security::PaymentMethods)
include Spree::Security::PaymentMethods
end
- # Multi-store sharing moved to the spree_multi_store extension in 5.6.
- include Spree::LegacyMultiStoreSupport unless defined?(SpreeMultiStore)
scope :active, -> { where(active: true).order(position: :asc) }
scope :available, -> { active.where(display_on: [:front_end, :back_end, :both]) }
@@ -22,7 +20,7 @@ module Spree
after_initialize :set_name, if: :new_record?
validates :name, presence: true
- validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
+ validates :store, presence: true
normalizes :name, with: ->(value) { value&.to_s&.squish&.presence }
belongs_to :store, class_name: 'Spree::Store'
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index af3d95d05c..1a7f6bd1cc 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -32,7 +32,6 @@ module Spree
include Spree::Product::Webhooks
include Spree::Product::Slugs
include Spree::Product::Channels
- include Spree::LegacyMultiStoreSupport unless defined?(SpreeMultiStore)
include Spree::SearchIndexable
if defined?(Spree::VendorConcern)
include Spree::VendorConcern
diff --git a/spree/core/app/models/spree/product_type.rb b/spree/core/app/models/spree/product_type.rb
index 646b75cfc8..28d0f682e9 100644
--- a/spree/core/app/models/spree/product_type.rb
+++ b/spree/core/app/models/spree/product_type.rb
@@ -24,7 +24,7 @@ module Spree
has_many :products, class_name: 'Spree::Product', dependent: :restrict_with_error
validates :name, presence: true, uniqueness: { case_sensitive: false, scope: :store_id }
- validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
+ validates :store, presence: true
validates :fulfillment_types, presence: true
self.whitelisted_ransackable_attributes = %w[name]
diff --git a/spree/core/app/models/spree/promotion.rb b/spree/core/app/models/spree/promotion.rb
index eec48e1f76..2368615447 100644
--- a/spree/core/app/models/spree/promotion.rb
+++ b/spree/core/app/models/spree/promotion.rb
@@ -8,8 +8,6 @@ module Spree
if defined?(Spree::Security::Promotions)
include Spree::Security::Promotions
end
- # Multi-store sharing moved to the spree_multi_store extension in 5.6.
- include Spree::LegacyMultiStoreSupport unless defined?(SpreeMultiStore)
publishes_lifecycle_events
@@ -39,7 +37,6 @@ module Spree
has_many :coupon_codes, -> { order(created_at: :asc) }, dependent: :destroy, class_name: 'Spree::CouponCode'
has_many :order_promotions, class_name: 'Spree::OrderPromotion'
has_many :orders, through: :order_promotions, class_name: 'Spree::Order'
- belongs_to :store, class_name: 'Spree::Store'
after_save :apply_pending_rules_and_actions, if: :pending_rules_or_actions?
@@ -61,7 +58,6 @@ module Spree
#
validates_associated :rules
validates :name, presence: true
- validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
validates :usage_limit, numericality: { greater_than: 0, allow_nil: true }
validates :description, length: { maximum: 255 }, allow_blank: true
validate :expires_at_must_be_later_than_starts_at, if: -> { starts_at && expires_at }
diff --git a/spree/core/app/models/spree/role_user.rb b/spree/core/app/models/spree/role_user.rb
index 05a3022f3b..70b2afbe4f 100644
--- a/spree/core/app/models/spree/role_user.rb
+++ b/spree/core/app/models/spree/role_user.rb
@@ -8,7 +8,6 @@ module Spree
belongs_to :role, class_name: 'Spree::Role', foreign_key: :role_id
belongs_to :user, polymorphic: true
belongs_to :resource, polymorphic: true
- belongs_to :store, class_name: 'Spree::Store'
belongs_to :invitation, class_name: 'Spree::Invitation', optional: true, inverse_of: :role_user
#
@@ -17,7 +16,6 @@ module Spree
validates :role, presence: true
validates :user, presence: true
validates :resource, presence: true
- validates :store, presence: true
validates :role_id, uniqueness: { scope: [:user_id, :resource_id, :user_type, :resource_type] }
#
diff --git a/spree/core/app/models/spree/stock_location.rb b/spree/core/app/models/spree/stock_location.rb
index 9a1b605552..d47cbd2cf9 100644
--- a/spree/core/app/models/spree/stock_location.rb
+++ b/spree/core/app/models/spree/stock_location.rb
@@ -1,6 +1,5 @@
module Spree
class StockLocation < Spree.base_class
- include Spree::SingleStoreResource
has_prefix_id :sloc # Spree-specific: stock location
# Categorizes the location. Open string — extensible by setting any value;
@@ -12,6 +11,7 @@ module Spree
# (ship-to-store). See docs/plans/6.0-fulfillment-and-delivery.md.
PICKUP_STOCK_POLICIES = %w[local any].freeze
+ include Spree::SingleStoreResource
include Spree::UniqueName
if defined?(Spree::Security::StockLocations)
include Spree::Security::StockLocations
@@ -22,8 +22,6 @@ module Spree
acts_as_paranoid
- belongs_to :store, class_name: 'Spree::Store'
-
has_many :fulfillments, class_name: 'Spree::Fulfillment'
has_many :shipments, class_name: 'Spree::Fulfillment', foreign_key: :stock_location_id, deprecated: true
has_many :stock_items, dependent: :delete_all, inverse_of: :stock_location
@@ -34,7 +32,6 @@ module Spree
belongs_to :country, class_name: 'Spree::Country'
validates :kind, presence: true
- validates :store, presence: true, unless: -> { Spree::Config[:disable_store_presence_validation] }
validates :pickup_stock_policy, inclusion: { in: PICKUP_STOCK_POLICIES }
validates :pickup_ready_in_minutes,
numericality: { only_integer: true, greater_than_or_equal_to: 0 },
diff --git a/spree/core/lib/spree/core/configuration.rb b/spree/core/lib/spree/core/configuration.rb
index 833c2d8e54..ff95c1dbd9 100644
--- a/spree/core/lib/spree/core/configuration.rb
+++ b/spree/core/lib/spree/core/configuration.rb
@@ -46,7 +46,7 @@ module Spree
preference :disable_migration_check, :boolean, default: false # when turned on disables the startup warning about missing engine migrations
preference :enable_legacy_default_price, :boolean, default: false # when enabled, keeps the legacy DefaultPrice concern active (has_one :default_price, variant.price= delegation, check_price validation). Disable (default) to use set_price exclusively.
preference :disable_sku_validation, :boolean, default: false # when turned off disables the built-in SKU uniqueness validation
- preference :disable_store_presence_validation, :boolean, default: false # when turned off disables Store presence validation for Products and Payment Methods
+ preference :disable_store_presence_validation, :boolean, default: false, deprecated: true # when turned off disables Store presence validation for Products and Payment Methods
preference :events_log_enabled, :boolean, default: true # Log all Spree events to Rails logger
preference :expedited_exchanges, :boolean, default: false # NOTE this requires payment profiles to be supported on your gateway of choice as well as a delayed job handler to be configured with activejob. kicks off an exchange shipment upon return authorization save. charge customer if they do not return items within timely manner.
preference :expedited_exchanges_days_window, :integer, default: 14 # the amount of days the customer has to return their item after the expedited exchange is shipped in order to avoid being charged