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.
This commit is contained in:
Maikel Linke
2023-07-28 16:25:22 +10:00
parent 8ef6966891
commit e6c679cb34
5 changed files with 15 additions and 19 deletions

View File

@@ -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? } }

View File

@@ -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]

View File

@@ -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

View File

@@ -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

View File

@@ -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) }