Skip to content

Commit 8ba4a68

Browse files
committed
Add foreign keys to shipping method related models
The `Spree::ShippingMethodCategory` is a join model and needs foreign keys on both ends. The shipping method id on products was mandatory before, so it's safe to remove the `optional: true` on the `belongs_to` declaration and add a foreign key constraint here as well.
1 parent 3867232 commit 8ba4a68

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

backend/spec/features/admin/orders/customer_details_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
# Regression test for https://github.com/spree/spree/issues/942
165165
context "errors when no shipping methods are available" do
166166
before do
167-
Spree::ShippingMethod.delete_all
167+
Spree::ShippingMethod.update_all(available_to_all: false, available_to_users: false)
168168
end
169169

170170
specify do

backend/spec/features/admin/products/products_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def build_option_type_with_values(name, values)
216216
fill_in "product_sku", with: "B100"
217217
fill_in "product_price", with: "100"
218218
click_button "Create"
219-
expect(page).to have_content("Shipping category can't be blank")
219+
expect(page).to have_content("Shipping Category must exist")
220220
end
221221

222222
context "using a locale with a different decimal format " do

core/app/models/spree/product.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Product < Spree::Base
3131
has_many :taxons, through: :classifications, before_remove: :remove_taxon
3232

3333
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
34-
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products, optional: true
34+
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products
3535
belongs_to :primary_taxon, class_name: 'Spree::Taxon', optional: true
3636

3737
has_one :master,
@@ -122,7 +122,6 @@ def find_or_build_master
122122
validates :meta_title, length: { maximum: 255 }
123123
validates :name, presence: true
124124
validates :price, presence: true, if: proc { Spree::Config[:require_master_price] }
125-
validates :shipping_category_id, presence: true
126125
validates :slug, presence: true, uniqueness: { allow_blank: true, case_sensitive: true }
127126

128127
attr_accessor :option_values_hash

core/app/models/spree/shipping_method_category.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Spree
44
class ShippingMethodCategory < Spree::Base
5-
belongs_to :shipping_method, class_name: 'Spree::ShippingMethod', optional: true
6-
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :shipping_method_categories, optional: true
5+
belongs_to :shipping_method, class_name: 'Spree::ShippingMethod'
6+
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :shipping_method_categories
77
end
88
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class AddShippingCategoryForeignKeys < ActiveRecord::Migration[7.0]
4+
def change
5+
add_foreign_key :spree_products, :spree_shipping_categories, column: :shipping_category_id, null: false
6+
add_foreign_key :spree_shipping_method_categories, :spree_shipping_methods, column: :shipping_method_id, null: false
7+
add_foreign_key :spree_shipping_method_categories, :spree_shipping_categories, column: :shipping_category_id, null: false
8+
end
9+
end

0 commit comments

Comments
 (0)