From e6c679cb34ed7b82052cad58be9799733ac670db Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 28 Jul 2023 16:25:22 +1000 Subject: [PATCH] Apply belongs_to_required_by_default to all models Unless they state otherwise. The new standard also changes the default behaviour of the Shoulda matcher in Rspec. It now defaults to asserting that an association is required. That needed some spec updates. In one case, Spree::Product, I also had to update the model because the presence validation was somehow not recognised by the Shoulda matcher. The error message may change slightly but the outcome should be the same. --- app/models/spree/product.rb | 7 ++----- config/application.rb | 1 - spec/models/spree/adjustment_spec.rb | 8 ++++---- spec/models/spree/product_spec.rb | 4 ++-- spec/models/subscription_spec.rb | 14 +++++++------- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index c9ca6ca063..68437005e8 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -34,8 +34,8 @@ module Spree searchable_scopes :active, :with_properties belongs_to :shipping_category, class_name: 'Spree::ShippingCategory' - belongs_to :supplier, class_name: 'Enterprise', touch: true - belongs_to :primary_taxon, class_name: 'Spree::Taxon', touch: true + belongs_to :supplier, class_name: 'Enterprise', optional: false, touch: true + belongs_to :primary_taxon, class_name: 'Spree::Taxon', optional: false, touch: true has_one :image, class_name: "Spree::Image", as: :viewable, dependent: :destroy @@ -54,9 +54,6 @@ module Spree validates :name, presence: true validates :shipping_category, presence: true - validates :supplier, presence: true - validates :primary_taxon, presence: true - validates :variant_unit, presence: true validates :unit_value, presence: { if: ->(p) { %w(weight volume).include?(p.variant_unit) && new_record? } } diff --git a/config/application.rb b/config/application.rb index 3f01f13856..0d2ae298fc 100644 --- a/config/application.rb +++ b/config/application.rb @@ -227,7 +227,6 @@ module Openfoodnetwork # https://guides.rubyonrails.org/configuring.html#results-of-config-load-defaults config.load_defaults 6.1 config.action_view.form_with_generates_remote_forms = false - config.active_record.belongs_to_required_by_default = false config.active_record.cache_versioning = false config.active_record.has_many_inversing = false config.active_record.yaml_column_permitted_classes = [BigDecimal, Symbol] diff --git a/spec/models/spree/adjustment_spec.rb b/spec/models/spree/adjustment_spec.rb index 5482d03024..f8e0388c67 100644 --- a/spec/models/spree/adjustment_spec.rb +++ b/spec/models/spree/adjustment_spec.rb @@ -11,10 +11,10 @@ module Spree it { is_expected.to have_one(:metadata) } it { is_expected.to have_many(:adjustments) } - it { is_expected.to belong_to(:adjustable) } - it { is_expected.to belong_to(:originator) } - it { is_expected.to belong_to(:order) } - it { is_expected.to belong_to(:tax_category) } + it { is_expected.to belong_to(:adjustable).optional } + it { is_expected.to belong_to(:originator).optional } + it { is_expected.to belong_to(:order).optional } + it { is_expected.to belong_to(:tax_category).optional } end describe "scopes" do diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index dc2be5b509..d99fe291f9 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -154,8 +154,8 @@ module Spree end describe "associations" do - it { is_expected.to belong_to(:supplier) } - it { is_expected.to belong_to(:primary_taxon) } + it { is_expected.to belong_to(:supplier).required } + it { is_expected.to belong_to(:primary_taxon).required } end describe "validations and defaults" do diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb index 06fee1dfcb..742e1952f1 100644 --- a/spec/models/subscription_spec.rb +++ b/spec/models/subscription_spec.rb @@ -4,13 +4,13 @@ require 'spec_helper' describe Subscription, type: :model do describe "associations" do - it { expect(subject).to belong_to(:shop) } - it { expect(subject).to belong_to(:customer) } - it { expect(subject).to belong_to(:schedule) } - it { expect(subject).to belong_to(:shipping_method) } - it { expect(subject).to belong_to(:payment_method) } - it { expect(subject).to belong_to(:ship_address) } - it { expect(subject).to belong_to(:bill_address) } + it { expect(subject).to belong_to(:shop).optional } + it { expect(subject).to belong_to(:customer).optional } + it { expect(subject).to belong_to(:schedule).optional } + it { expect(subject).to belong_to(:shipping_method).optional } + it { expect(subject).to belong_to(:payment_method).optional } + it { expect(subject).to belong_to(:ship_address).optional } + it { expect(subject).to belong_to(:bill_address).optional } it { expect(subject).to have_many(:subscription_line_items) } it { expect(subject).to have_many(:order_cycles) } it { expect(subject).to have_many(:proxy_orders) }