From 71f1c7dd87ad41012b8b4df248959a7f3a794ff9 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 21 Mar 2023 17:27:11 +0100 Subject: [PATCH] [API][Extract to PR?] Enforce creating option values with an option type Add a NOT NULL validation at the database level on top of removing the existing deprecation warning. Ref https://github.com/solidusio/solidus/pull/4385. --- core/app/models/spree/option_value.rb | 13 ++----------- ...ange_column_null_option_values_option_type_id.rb | 5 +++++ core/spec/models/spree/option_value_spec.rb | 8 ++++---- 3 files changed, 11 insertions(+), 15 deletions(-) create mode 100644 core/db/migrate/20230321161854_change_column_null_option_values_option_type_id.rb diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb index d9ffdefaa09..0f063616722 100644 --- a/core/app/models/spree/option_value.rb +++ b/core/app/models/spree/option_value.rb @@ -2,9 +2,7 @@ module Spree class OptionValue < Spree::Base - # TODO: Remove optional on Solidus v4.0. Don't forget adding a migration to - # enforce at the database layer - belongs_to :option_type, class_name: 'Spree::OptionType', inverse_of: :option_values, optional: true + belongs_to :option_type, class_name: 'Spree::OptionType', inverse_of: :option_values acts_as_list scope: :option_type has_many :option_values_variants, dependent: :destroy @@ -15,15 +13,8 @@ class OptionValue < Spree::Base after_save :touch, if: :saved_changes? after_touch :touch_all_variants - after_save do - Spree::Deprecation.warn <<~MSG if option_type.nil? - Having an option_value with no associated option_type will be deprecated - on Solidus v4.0 - MSG - end - # TODO: Remove allow_nil once option_type is required on Solidus v4.0 - delegate :name, :presentation, to: :option_type, prefix: :option_type, allow_nil: true + delegate :name, :presentation, to: :option_type, prefix: :option_type self.allowed_ransackable_attributes = %w[name presentation] diff --git a/core/db/migrate/20230321161854_change_column_null_option_values_option_type_id.rb b/core/db/migrate/20230321161854_change_column_null_option_values_option_type_id.rb new file mode 100644 index 00000000000..6cd9ec1f7f5 --- /dev/null +++ b/core/db/migrate/20230321161854_change_column_null_option_values_option_type_id.rb @@ -0,0 +1,5 @@ +class ChangeColumnNullOptionValuesOptionTypeId < ActiveRecord::Migration[5.2] + def change + change_column_null(:spree_option_values, :option_type_id, false) + end +end diff --git a/core/spec/models/spree/option_value_spec.rb b/core/spec/models/spree/option_value_spec.rb index 6c42f441102..f55dae08e0f 100644 --- a/core/spec/models/spree/option_value_spec.rb +++ b/core/spec/models/spree/option_value_spec.rb @@ -50,9 +50,9 @@ end end - it 'deprecates creating an option_value with no associated option_type' do - expect(Spree::Deprecation).to receive(:warn).with(/deprecated/) - - create(:option_value, option_type: nil) + it 'raises when creating an option_value with no associated option_type' do + expect { + create(:option_value, option_type: nil) + }.to raise_error(ActiveRecord::RecordInvalid) end end