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
Rename Prototype to ProductType with fulfillment_types (product-types Phase 1)
Damian Legawiec90172468f0d8
diff --git a/spree/core/app/models/spree/category.rb b/spree/core/app/models/spree/category.rbindex 3df47eeb00..10e13f4e6f 100644--- a/spree/core/app/models/spree/category.rb+++ b/spree/core/app/models/spree/category.rb@@ -40,10 +40,8 @@ module Spreeproduct_categoriesend- # spree_prototype_taxons keeps its taxon_id column (the prototype join is- # renamed by 6.0-product-types), so pin the foreign key there.- has_many :prototype_taxons, class_name: 'Spree::PrototypeTaxon', foreign_key: :taxon_id, dependent: :destroy- has_many :prototypes, through: :prototype_taxons, class_name: 'Spree::Prototype'+ has_many :product_type_categories, class_name: 'Spree::ProductTypeCategory', foreign_key: :category_id, dependent: :destroy+ has_many :product_types, through: :product_type_categories, class_name: 'Spree::ProductType'has_many :promotion_rule_categories, class_name: 'Spree::PromotionRuleCategory', dependent: :destroyhas_many :promotion_rules, through: :promotion_rule_categories, class_name: 'Spree::PromotionRule'diff --git a/spree/core/app/models/spree/option_type.rb b/spree/core/app/models/spree/option_type.rbindex 214273e5e2..8604b5b671 100644--- a/spree/core/app/models/spree/option_type.rb+++ b/spree/core/app/models/spree/option_type.rb@@ -44,8 +44,8 @@ module Spreehas_many :product_option_typesendhas_many :products, through: :product_option_types- has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype'- has_many :prototypes, through: :option_type_prototypes, class_name: 'Spree::Prototype'+ has_many :option_type_product_types, class_name: 'Spree::OptionTypeProductType', dependent: :destroy+ has_many :product_types, through: :option_type_product_types, class_name: 'Spree::ProductType'# 5.5 API naming bridge (`label` → `presentation`) lives in# Spree::PresentationTranslatable.diff --git a/spree/core/app/models/spree/option_type_product_type.rb b/spree/core/app/models/spree/option_type_product_type.rbnew file mode 100644index 0000000000..5fb61228ff--- /dev/null+++ b/spree/core/app/models/spree/option_type_product_type.rb@@ -0,0 +1,9 @@+module Spree+ class OptionTypeProductType < Spree.base_class+ belongs_to :option_type, class_name: 'Spree::OptionType'+ belongs_to :product_type, class_name: 'Spree::ProductType'++ validates :product_type, :option_type, presence: true+ validates :product_type_id, uniqueness: { scope: :option_type_id }+ end+enddiff --git a/spree/core/app/models/spree/option_type_prototype.rb b/spree/core/app/models/spree/option_type_prototype.rbdeleted file mode 100644index 8f280cdac4..0000000000--- a/spree/core/app/models/spree/option_type_prototype.rb+++ /dev/null@@ -1,9 +0,0 @@-module Spree- class OptionTypePrototype < Spree.base_class- belongs_to :option_type, class_name: 'Spree::OptionType'- belongs_to :prototype, class_name: 'Spree::Prototype'-- validates :prototype, :option_type, presence: true- validates :prototype_id, uniqueness: { scope: :option_type_id }- end-enddiff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex c64537fffb..e7012704ae 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -124,6 +124,7 @@ module Spreebelongs_to :tax_category, class_name: 'Spree::TaxCategory'belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products+ belongs_to :product_type, class_name: 'Spree::ProductType', optional: true, counter_cache: :products_counthas_many :shipping_methods, through: :shipping_category, class_name: 'Spree::ShippingMethod'# Every product has at least one variant. `default_variant` is the "face" of@@ -159,7 +160,7 @@ module Spreebefore_validation :ensure_default_shipping_category- after_create :add_associations_from_prototype+ after_create :sync_associations_from_product_typeafter_create :apply_pending_variants, if: :pending_variants?after_create :ensure_default_variantafter_create :set_default_variant@@ -500,9 +501,6 @@ module Spree@tax_category ||= super || TaxCategory.defaultend- # Adding properties and option types on creation based on a chosen prototype- attr_accessor :prototype_id-def first_or_default_variant(currency)if !has_variants?default_variant@@ -850,10 +848,16 @@ module Spreeassociation(:default_variant).resetend- def add_associations_from_prototype- if prototype_id && prototype = Spree::Prototype.find_by(id: prototype_id)- self.option_types = prototype.option_types- self.categories = prototype.taxons+ # Additive, unlike the legacy prototype callback which replaced both sets —+ # the type is a floor the product builds on, never a ceiling.+ def sync_associations_from_product_type+ return unless product_type++ product_type.option_types.each do |option_type|+ option_types << option_type unless option_types.include?(option_type)+ end+ product_type.categories.each do |category|+ categories << category unless categories.include?(category)endenddiff --git a/spree/core/app/models/spree/product_type.rb b/spree/core/app/models/spree/product_type.rbnew file mode 100644index 0000000000..646b75cfc8--- /dev/null+++ b/spree/core/app/models/spree/product_type.rb@@ -0,0 +1,43 @@+module Spree+ class ProductType < Spree.base_class+ has_prefix_id :pt++ include Spree::Metadata+ include Spree::TranslatableResource+ include Spree::SingleStoreResource++ TRANSLATABLE_FIELDS = %i[name].freeze+ translates(*TRANSLATABLE_FIELDS, column_fallback: Spree.mobility_column_fallback)++ attribute :fulfillment_types, default: -> { ['shipping'] }++ belongs_to :store, class_name: 'Spree::Store'++ has_many :option_type_product_types, class_name: 'Spree::OptionTypeProductType', dependent: :destroy+ has_many :option_types, through: :option_type_product_types, class_name: 'Spree::OptionType'++ has_many :product_type_categories, class_name: 'Spree::ProductTypeCategory', dependent: :destroy+ has_many :categories, through: :product_type_categories, class_name: 'Spree::Category'++ # restrict, not nullify — removing a product's type (deletion included) would+ # invalidate its type-driven data, so types in use cannot be deleted+ 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 :fulfillment_types, presence: true++ self.whitelisted_ransackable_attributes = %w[name]+ self.whitelisted_ransackable_associations = %w[option_types]++ # @return [Boolean] true when products of this type are delivered digitally only+ def digital?+ fulfillment_types == ['digital']+ end++ # @return [Boolean] true when products of this type require physical delivery+ def requires_shipping?+ fulfillment_types.include?('shipping')+ end+ end+enddiff --git a/spree/core/app/models/spree/product_type_category.rb b/spree/core/app/models/spree/product_type_category.rbnew file mode 100644index 0000000000..77458bcc15--- /dev/null+++ b/spree/core/app/models/spree/product_type_category.rb@@ -0,0 +1,9 @@+module Spree+ class ProductTypeCategory < Spree.base_class+ belongs_to :category, class_name: 'Spree::Category'+ belongs_to :product_type, class_name: 'Spree::ProductType'++ validates :product_type, :category, presence: true+ validates :product_type_id, uniqueness: { scope: :category_id }+ end+enddiff --git a/spree/core/app/models/spree/prototype.rb b/spree/core/app/models/spree/prototype.rbindex 2ea73ef021..c9a389cf1e 100644--- a/spree/core/app/models/spree/prototype.rb+++ b/spree/core/app/models/spree/prototype.rb@@ -1,15 +1,5 @@module Spree- class Prototype < Spree.base_class- has_prefix_id :proto-- include Spree::Metadata-- has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype'- has_many :option_types, through: :option_type_prototypes, class_name: 'Spree::OptionType'-- has_many :prototype_taxons, class_name: 'Spree::PrototypeTaxon'- has_many :taxons, through: :prototype_taxons, class_name: 'Spree::Category'-- validates :name, presence: true- end+ # Constant alias for the legacy Spree::Prototype class, renamed to+ # Spree::ProductType in 6.0. Remove in 6.1.+ Prototype = ProductTypeenddiff --git a/spree/core/app/models/spree/prototype_taxon.rb b/spree/core/app/models/spree/prototype_taxon.rbdeleted file mode 100644index 519b520c81..0000000000--- a/spree/core/app/models/spree/prototype_taxon.rb+++ /dev/null@@ -1,9 +0,0 @@-module Spree- class PrototypeTaxon < Spree.base_class- belongs_to :taxon, class_name: 'Spree::Category'- belongs_to :prototype, class_name: 'Spree::Prototype'-- validates :prototype, :taxon, presence: true- validates :prototype_id, uniqueness: { scope: :taxon_id }- end-enddiff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex 929c6b55ea..9b363e68d3 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -149,6 +149,8 @@ en:on_hand: On Handshipping_category: Shipping Categorytax_category: Tax Category+ spree/product_type:+ name: Namespree/promotion:advertise: Advertisecode: Code@@ -167,8 +169,6 @@ en:spree/property:name: Namepresentation: Presentation- spree/prototype:- name: Namespree/return_authorization:amount: Amountspree/role:@@ -473,6 +473,9 @@ en:spree/product:one: Productother: Products+ spree/product_type:+ one: Product Type+ other: Product Typesspree/promotion:one: Promotionother: Promotions@@ -482,9 +485,6 @@ en:spree/property:one: Propertyother: Properties- spree/prototype:- one: Prototype- other: Prototypesspree/refund_reason:one: Refund Reasonother: Refund Reasons@@ -920,6 +920,11 @@ en:cannot_delete_channel_with_active_api_keys: Channel cannot be deleted while active API keys are bound to it. Revoke those keys first.cannot_delete_default_channel: Default channel cannot be deleted. Promote another channel to default first.channel_store_mismatch: must belong to the same store+ delivery_zone_postal_definition_required: postal code member requires a prefix or a from/to range+ delivery_zone_prefix_and_range_exclusive: cannot be combined with a from/to range+ delivery_zone_range_inverted: must be greater than or equal to postal_code_from+ delivery_zone_range_not_supported: postal code ranges are not supported for this country — use prefixes+ exactly_one_of_cart_or_order: must belong to exactly one of cart or orderinvalid_order_routing_rule: is not a registered order routing ruleinvalid_order_routing_strategy: is not a registered order routing strategyinvalid_storefront_access: is not a valid storefront access level@@ -1322,8 +1327,6 @@ en:propagate_all_variants: Propagate all variantsproperties: Propertiesproperty: Property- prototype: Prototype- prototypes: Prototypesprovider: Providerpublished_at: Published atqty: Qtydiff --git a/spree/core/db/migrate/20260728000001_rename_prototypes_to_product_types.rb b/spree/core/db/migrate/20260728000001_rename_prototypes_to_product_types.rbnew file mode 100644index 0000000000..ea94eea4d3--- /dev/null+++ b/spree/core/db/migrate/20260728000001_rename_prototypes_to_product_types.rb@@ -0,0 +1,59 @@+class RenamePrototypesToProductTypes < ActiveRecord::Migration[7.2]+ def change+ # Old-name indexes are dropped up front and re-added under stable names after+ # the renames — Rails' automatic index renaming would generate names over+ # PostgreSQL's 63-char identifier limit for the composite ones.+ remove_index :spree_option_type_prototypes, column: [:prototype_id, :option_type_id], unique: true,+ name: 'spree_option_type_prototypes_prototype_id_option_type_id', if_exists: true+ remove_index :spree_option_type_prototypes, column: :prototype_id,+ name: 'index_spree_option_type_prototypes_on_prototype_id', if_exists: true+ remove_index :spree_option_type_prototypes, column: :option_type_id,+ name: 'index_spree_option_type_prototypes_on_option_type_id', if_exists: true+ remove_index :spree_prototype_taxons, column: [:prototype_id, :taxon_id],+ name: 'index_spree_prototype_taxons_on_prototype_id_and_taxon_id', if_exists: true+ remove_index :spree_prototype_taxons, column: :prototype_id,+ name: 'index_spree_prototype_taxons_on_prototype_id', if_exists: true+ remove_index :spree_prototype_taxons, column: :taxon_id,+ name: 'index_spree_prototype_taxons_on_taxon_id', if_exists: true++ rename_table :spree_prototypes, :spree_product_types+ rename_table :spree_option_type_prototypes, :spree_option_type_product_types+ rename_table :spree_prototype_taxons, :spree_product_type_categories++ rename_column :spree_option_type_product_types, :prototype_id, :product_type_id+ rename_column :spree_product_type_categories, :prototype_id, :product_type_id+ rename_column :spree_product_type_categories, :taxon_id, :category_id++ add_index :spree_option_type_product_types, [:product_type_id, :option_type_id], unique: true,+ name: 'index_option_type_product_types_uniqueness'+ add_index :spree_option_type_product_types, :option_type_id,+ name: 'index_option_type_product_types_on_option_type_id'+ add_index :spree_product_type_categories, [:product_type_id, :category_id], unique: true,+ name: 'index_product_type_categories_uniqueness'+ add_index :spree_product_type_categories, :category_id,+ name: 'index_product_type_categories_on_category_id'++ add_reference :spree_products, :product_type, null: true, if_not_exists: true+ add_reference :spree_product_types, :store, null: true, if_not_exists: true++ if connection.adapter_name.downcase.include?('postgresql')+ add_column :spree_product_types, :fulfillment_types, :jsonb+ else+ add_column :spree_product_types, :fulfillment_types, :json+ end++ add_column :spree_product_types, :products_count, :integer, null: false, default: 0++ create_table :spree_product_type_translations do |t|+ t.string :name+ t.string :locale, null: false+ t.references :spree_product_type, null: false, index: false++ t.timestamps null: false+ end+ add_index :spree_product_type_translations, :locale,+ name: 'index_spree_product_type_translations_on_locale'+ add_index :spree_product_type_translations, [:spree_product_type_id, :locale], unique: true,+ name: 'index_product_type_translations_on_product_type_and_locale'+ end+enddiff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rbindex 845abf8e9d..d5087a4f7e 100644--- a/spree/core/lib/spree/core/engine.rb+++ b/spree/core/lib/spree/core/engine.rb@@ -286,6 +286,7 @@ module SpreeSpree::OptionType,Spree::OptionValue,Spree::Product,+ Spree::ProductType,Spree::Collection,Spree::Category,Spree::Taxonomy,@@ -331,6 +332,7 @@ module SpreeSpree::PaymentMethod,Spree::PaymentSource,Spree::Product,+ Spree::ProductType,Spree::Promotion,Spree::Refund,Spree::Shipment,diff --git a/spree/core/lib/spree/permitted_attributes.rb b/spree/core/lib/spree/permitted_attributes.rbindex 8726a8563b..90bc0a5055 100644--- a/spree/core/lib/spree/permitted_attributes.rb+++ b/spree/core/lib/spree/permitted_attributes.rb@@ -185,7 +185,7 @@ module Spree@@product_attributes = [:name, :description, :available_on, :make_active_at, :discontinue_on, :permalink, :meta_description,- :meta_keywords, :meta_title, :price, :sku, :deleted_at, :prototype_id,+ :meta_keywords, :meta_title, :price, :sku, :deleted_at,:weight, :height, :width, :depth,:shipping_category_id, :tax_category_id,:cost_currency, :cost_price, :compare_at_price,diff --git a/spree/core/lib/spree/testing_support/factories/product_type_factory.rb b/spree/core/lib/spree/testing_support/factories/product_type_factory.rbnew file mode 100644index 0000000000..b4d29f606d--- /dev/null+++ b/spree/core/lib/spree/testing_support/factories/product_type_factory.rb@@ -0,0 +1,12 @@+FactoryBot.define do+ factory :product_type, aliases: [:prototype], class: Spree::ProductType do+ name { 'Baseball Cap' }+ store { Spree::Store.find_by(default: true) || association(:store) }+ end++ factory :product_type_with_option_types, aliases: [:prototype_with_option_types], class: Spree::ProductType do+ name { 'Baseball Cap' }+ store { Spree::Store.find_by(default: true) || association(:store) }+ option_types { [build(:option_type)] }+ end+enddiff --git a/spree/core/lib/spree/testing_support/factories/prototype_factory.rb b/spree/core/lib/spree/testing_support/factories/prototype_factory.rbdeleted file mode 100644index 025357e43a..0000000000--- a/spree/core/lib/spree/testing_support/factories/prototype_factory.rb+++ /dev/null@@ -1,9 +0,0 @@-FactoryBot.define do- factory :prototype, class: Spree::Prototype do- name { 'Baseball Cap' }- end- factory :prototype_with_option_types, class: Spree::Prototype do- name { 'Baseball Cap' }- option_types { [build(:option_type)] }- end-enddiff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rbindex 634525a132..88883e538f 100644--- a/spree/core/spec/models/spree/product_spec.rb+++ b/spree/core/spec/models/spree/product_spec.rb@@ -424,32 +424,52 @@ describe Spree::Product, type: :model doenddescribe '#create' do- let!(:prototype) { create(:prototype) }- let!(:product) { build(:product, name: 'Foo', price: 1.99, shipping_category: create(:shipping_category)) }-- before { product.prototype_id = prototype.id }+ let!(:product_type) { create(:product_type) }+ let!(:product) do+ build(:product, name: 'Foo', price: 1.99, shipping_category: create(:shipping_category), product_type: product_type)+ end- context 'when prototype with option types is supplied' do+ context 'when a product type with option types is supplied' dodef build_option_type_with_values(name, values)values.each_with_object(create(:option_type, name: name)) do |val, ot|ot.option_values.create(name: val.downcase, presentation: val)endend- let(:prototype) do+ let(:product_type) dosize = build_option_type_with_values('size', %w(Small Medium Large))- create(:prototype, name: 'Size', option_types: [size])+ create(:product_type, name: 'Size', option_types: [size])end- it 'creates option types based on the prototype' do+ it 'adds the type option types to the product' doproduct.saveexpect(product.option_type_ids.length).to eq(1)- expect(product.option_type_ids).to eq(prototype.option_type_ids)+ expect(product.option_type_ids).to eq(product_type.option_type_ids)end- it 'creates product option types based on the prototype' do+ it 'creates product option types based on the product type' do+ product.save+ expect(product.product_option_types.pluck(:option_type_id)).to eq(product_type.option_type_ids)+ end++ it 'keeps option types the product already has (additive sync)' do+ color = create(:option_type, name: 'color')+ product.option_types = [color]++ product.save++ expect(product.option_type_ids).to match_array([color.id, *product_type.option_type_ids])+ end+ end++ context 'when a product type with categories is supplied' do+ let(:category) { create(:category) }++ before { product_type.categories << category }++ it 'adds the type categories to the product' doproduct.save- expect(product.product_option_types.pluck(:option_type_id)).to eq(prototype.option_type_ids)+ expect(product.reload.categories).to include(category)endenddiff --git a/spree/core/spec/models/spree/product_type_spec.rb b/spree/core/spec/models/spree/product_type_spec.rbnew file mode 100644index 0000000000..f2abf2192f--- /dev/null+++ b/spree/core/spec/models/spree/product_type_spec.rb@@ -0,0 +1,50 @@+require 'spec_helper'++describe Spree::ProductType, type: :model do+ it_behaves_like 'metadata'++ describe 'fulfillment_types' do+ it 'defaults to shipping' do+ expect(described_class.new.fulfillment_types).to eq(['shipping'])+ end++ it 'persists an explicit value' do+ product_type = create(:product_type, fulfillment_types: %w[shipping pickup])++ expect(product_type.reload.fulfillment_types).to eq(%w[shipping pickup])+ end+ end++ describe '#digital?' do+ it 'is true only when digital is the sole fulfillment type' do+ expect(build(:product_type, fulfillment_types: ['digital'])).to be_digital+ expect(build(:product_type, fulfillment_types: %w[digital shipping])).not_to be_digital+ expect(build(:product_type)).not_to be_digital+ end+ end++ describe '#requires_shipping?' do+ it 'is true when shipping is among the fulfillment types' do+ expect(build(:product_type)).to be_requires_shipping+ expect(build(:product_type, fulfillment_types: ['digital'])).not_to be_requires_shipping+ end+ end++ describe 'deletion' do+ let(:product_type) { create(:product_type) }++ it 'is restricted while products use the type' do+ create(:product, product_type: product_type)++ expect(product_type.destroy).to be(false)+ expect(product_type.errors[:base]).to be_present+ expect(product_type.reload).to be_persisted+ end++ it 'destroys its joins when unused' do+ product_type.option_types << create(:option_type)++ expect { product_type.destroy }.to change(Spree::OptionTypeProductType, :count).by(-1)+ end+ end+enddiff --git a/spree/core/spec/models/spree/prototype_spec.rb b/spree/core/spec/models/spree/prototype_spec.rbdeleted file mode 100644index 9db5713cc0..0000000000--- a/spree/core/spec/models/spree/prototype_spec.rb+++ /dev/null@@ -1,5 +0,0 @@-require 'spec_helper'--describe Spree::Prototype, type: :model do- it_behaves_like 'metadata'-end