diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 14d29848c8..a2c635f20a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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). diff --git a/app/models/spree/tax_rate.rb b/app/models/spree/tax_rate.rb index d7d91a7b73..af8d82f12d 100644 --- a/app/models/spree/tax_rate.rb +++ b/app/models/spree/tax_rate.rb @@ -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) } diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index af0dbd92fb..ea5dca61b2 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -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 diff --git a/db/migrate/20240517121235_require_tax_category_on_tax_rate.rb b/db/migrate/20240517121235_require_tax_category_on_tax_rate.rb new file mode 100644 index 0000000000..4bc80ca713 --- /dev/null +++ b/db/migrate/20240517121235_require_tax_category_on_tax_rate.rb @@ -0,0 +1,5 @@ +class RequireTaxCategoryOnTaxRate < ActiveRecord::Migration[7.0] + def change + change_column_null :spree_tax_rates, :tax_category_id, false + end +end diff --git a/db/schema.rb b/db/schema.rb index 1cbb2938fa..99bd4c313c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/lib/tasks/data/check_missing_required_ids_in_tax_rates.rake b/lib/tasks/data/check_missing_required_ids_in_tax_rates.rake new file mode 100644 index 0000000000..b2133d8fa6 --- /dev/null +++ b/lib/tasks/data/check_missing_required_ids_in_tax_rates.rake @@ -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