Fix RedundantPresenceValidationOnBelongs on two files

- 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
     - added 'NOT NULL' constraints so model constraints match
       with contraints on DB tables.
     - corresponding migration files to match AR Models &
       DB tables
     - rake tasks to check corrupt data (ie: NULL/nil in id fields)
     - updated the todo
This commit is contained in:
cyrillefr
2024-05-17 15:32:55 +02:00
parent 5fdea3e8ce
commit 46cd60aa3c
6 changed files with 27 additions and 13 deletions

View File

@@ -650,16 +650,14 @@ Rails/RedundantActiveRecordAllMethod:
- 'app/models/spree/variant.rb'
- 'spec/system/admin/product_import_spec.rb'
# Offense count: 9
# Offense count: 7
# This cop supports unsafe autocorrection (--autocorrect-all).
Rails/RedundantPresenceValidationOnBelongsTo:
Exclude:
- 'app/models/spree/line_item.rb'
- 'app/models/spree/order.rb'
- 'app/models/spree/tax_rate.rb'
- 'app/models/subscription_line_item.rb'
- 'app/models/tag_rule.rb'
- 'app/models/variant_override.rb'
# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).

View File

@@ -14,17 +14,14 @@ end
module Spree
class TaxRate < ApplicationRecord
self.belongs_to_required_by_default = false
acts_as_paranoid
include CalculatedAdjustments
belongs_to :zone, class_name: "Spree::Zone", inverse_of: :tax_rates
belongs_to :zone, class_name: "Spree::Zone", inverse_of: :tax_rates, optional: true
belongs_to :tax_category, class_name: "Spree::TaxCategory", inverse_of: :tax_rates
has_many :adjustments, as: :originator, dependent: nil
validates :amount, presence: true, numericality: true
validates :tax_category, presence: true
validates_with DefaultTaxZoneValidator
scope :by_zone, ->(zone) { where(zone_id: zone) }

View File

@@ -6,15 +6,11 @@ class VariantOverride < ApplicationRecord
extend Spree::LocalizedNumber
include StockSettingsOverrideValidation
self.belongs_to_required_by_default = false
acts_as_taggable
belongs_to :hub, class_name: 'Enterprise'
belongs_to :variant, class_name: 'Spree::Variant'
validates :hub, presence: true
validates :variant, presence: true
# Default stock can be nil, indicating stock should not be reset or zero, meaning reset to zero.
# Need to ensure this can be set by the user.
validates :default_stock, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true

View File

@@ -0,0 +1,5 @@
class RequireTaxCategoryOnTaxRate < ActiveRecord::Migration[7.0]
def change
change_column_null :spree_tax_rates, :tax_category_id, false
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2024_05_01_075735) do
ActiveRecord::Schema[7.0].define(version: 2024_05_17_121235) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
@@ -871,7 +871,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_01_075735) do
create_table "spree_tax_rates", id: :serial, force: :cascade do |t|
t.decimal "amount", precision: 8, scale: 5
t.integer "zone_id"
t.integer "tax_category_id"
t.integer "tax_category_id", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.boolean "included_in_price", default: false

View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
namespace :ofn do
namespace :data do
desc 'Checking missing required ids in Spree::TaxRate'
task check_missing_required_missing_ids_in_spree_tax_rates: :environment do
puts 'Checking for null tax_category_id'
ids = Spree::TaxRate.where(tax_category_id: nil).pluck(:id)
if ids.empty?
puts 'No NULL tax_category_id found in spree_tax_rates'
else
puts 'NULL tax_category_ids s have been found in spree_tax_rates:'
print ids
end
end
end
end