From 5bc6d16f34995f1ff8c5cb9122ce875e37681c1b Mon Sep 17 00:00:00 2001 From: cyrillefr Date: Fri, 24 May 2024 08:22:20 +0200 Subject: [PATCH] Fix redundant presence validation on belongs part V - presence: true is redundant since Rails 5.0 BUT applies with new default config of belongs_to_required_by_default to true. Lots of files with belongs_to_required_by_default = false (backward compatibility). So: deleting this setting implies to adding optional: true --- .rubocop_todo.yml | 4 +--- app/models/subscription_line_item.rb | 4 ---- app/models/tag_rule.rb | 4 ---- spec/models/subscription_line_item_spec.rb | 4 ++-- spec/models/tag_rule_spec.rb | 2 +- 5 files changed, 4 insertions(+), 14 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a2c635f20a..2fe6e29f41 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -650,14 +650,12 @@ Rails/RedundantActiveRecordAllMethod: - 'app/models/spree/variant.rb' - 'spec/system/admin/product_import_spec.rb' -# Offense count: 7 +# Offense count: 4 # This cop supports unsafe autocorrection (--autocorrect-all). Rails/RedundantPresenceValidationOnBelongsTo: Exclude: - 'app/models/spree/line_item.rb' - 'app/models/spree/order.rb' - - 'app/models/subscription_line_item.rb' - - 'app/models/tag_rule.rb' # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). diff --git a/app/models/subscription_line_item.rb b/app/models/subscription_line_item.rb index a4a0b1ede5..20a98d7ceb 100644 --- a/app/models/subscription_line_item.rb +++ b/app/models/subscription_line_item.rb @@ -1,13 +1,9 @@ # frozen_string_literal: true class SubscriptionLineItem < ApplicationRecord - self.belongs_to_required_by_default = false - belongs_to :subscription, inverse_of: :subscription_line_items belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant' - validates :subscription, presence: true - validates :variant, presence: true validates :quantity, presence: true, numericality: { only_integer: true } default_scope { order('id ASC') } diff --git a/app/models/tag_rule.rb b/app/models/tag_rule.rb index cdff7315b0..a6b5ae4bb6 100644 --- a/app/models/tag_rule.rb +++ b/app/models/tag_rule.rb @@ -1,14 +1,10 @@ # frozen_string_literal: true class TagRule < ApplicationRecord - self.belongs_to_required_by_default = false - belongs_to :enterprise preference :customer_tags, :string, default: "" - validates :enterprise, presence: true - scope :for, ->(enterprise) { where(enterprise_id: enterprise) } scope :prioritised, -> { order('priority ASC') } diff --git a/spec/models/subscription_line_item_spec.rb b/spec/models/subscription_line_item_spec.rb index 7aef546a5d..473c04a1a5 100644 --- a/spec/models/subscription_line_item_spec.rb +++ b/spec/models/subscription_line_item_spec.rb @@ -5,11 +5,11 @@ require 'spec_helper' RSpec.describe SubscriptionLineItem, model: true do describe "validations" do it "requires a subscription" do - expect(subject).to validate_presence_of :subscription + expect(subject).to belong_to :subscription end it "requires a variant" do - expect(subject).to validate_presence_of :variant + expect(subject).to belong_to :variant end it "requires a integer for quantity" do diff --git a/spec/models/tag_rule_spec.rb b/spec/models/tag_rule_spec.rb index 16e2c78ff8..738263cf34 100644 --- a/spec/models/tag_rule_spec.rb +++ b/spec/models/tag_rule_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' RSpec.describe TagRule, type: :model do describe "validations" do it "requires a enterprise" do - expect(subject).to validate_presence_of(:enterprise) + expect(subject).to belong_to(:enterprise) end end end