Fix Rails/HasManyOrHasOneDependent with Destroy

This commit is contained in:
Neal Chambers
2023-08-30 09:32:03 +09:00
parent 53f5d63e4a
commit b76fb10d46
15 changed files with 42 additions and 37 deletions

View File

@@ -16,7 +16,7 @@ class Customer < ApplicationRecord
belongs_to :enterprise
belongs_to :user, class_name: "Spree::User", optional: true
has_many :orders, class_name: "Spree::Order"
has_many :orders, class_name: "Spree::Order", dependent: :destroy
before_validation :downcase_email
before_validation :empty_code
before_create :associate_user

View File

@@ -37,31 +37,32 @@ class Enterprise < ApplicationRecord
dependent: :destroy
has_and_belongs_to_many :groups, join_table: 'enterprise_groups_enterprises',
class_name: 'EnterpriseGroup'
has_many :producer_properties, foreign_key: 'producer_id'
has_many :producer_properties, foreign_key: 'producer_id', dependent: :destroy
has_many :properties, through: :producer_properties
has_many :supplied_products, class_name: 'Spree::Product',
foreign_key: 'supplier_id',
dependent: :destroy
has_many :supplied_variants, through: :supplied_products, source: :variants
has_many :distributed_orders, class_name: 'Spree::Order', foreign_key: 'distributor_id'
has_many :distributed_orders, class_name: 'Spree::Order', foreign_key: 'distributor_id',
dependent: :destroy
belongs_to :address, class_name: 'Spree::Address'
belongs_to :business_address, optional: true, class_name: 'Spree::Address', dependent: :destroy
has_many :enterprise_fees
has_many :enterprise_fees, dependent: :destroy
has_many :enterprise_roles, dependent: :destroy
has_many :users, through: :enterprise_roles
belongs_to :owner, class_name: 'Spree::User',
inverse_of: :owned_enterprises
has_many :distributor_payment_methods,
inverse_of: :distributor, foreign_key: :distributor_id
inverse_of: :distributor, foreign_key: :distributor_id, dependent: :destroy
has_many :distributor_shipping_methods,
inverse_of: :distributor, foreign_key: :distributor_id
inverse_of: :distributor, foreign_key: :distributor_id, dependent: :destroy
has_many :payment_methods, through: :distributor_payment_methods
has_many :shipping_methods, through: :distributor_shipping_methods
has_many :customers
has_many :inventory_items
has_many :tag_rules
has_many :customers, dependent: :destroy
has_many :inventory_items, dependent: :destroy
has_many :tag_rules, dependent: :destroy
has_one :stripe_account, dependent: :destroy
has_many :vouchers
has_many :vouchers, dependent: :destroy
has_one :custom_tab, dependent: :destroy
delegate :latitude, :longitude, :city, :state_name, to: :address

View File

@@ -11,7 +11,7 @@ class OrderCycle < ApplicationRecord
belongs_to :coordinator, class_name: 'Enterprise'
has_many :coordinator_fee_refs, class_name: 'CoordinatorFee'
has_many :coordinator_fee_refs, class_name: 'CoordinatorFee', dependent: :destroy
has_many :coordinator_fees, through: :coordinator_fee_refs, source: :enterprise_fee,
dependent: :destroy
@@ -19,12 +19,16 @@ class OrderCycle < ApplicationRecord
# These scope names are prepended with "cached_" because there are existing accessor methods
# :incoming_exchanges and :outgoing_exchanges.
has_many :cached_incoming_exchanges, -> { where incoming: true }, class_name: "Exchange"
has_many :cached_outgoing_exchanges, -> { where incoming: false }, class_name: "Exchange"
has_many :cached_incoming_exchanges, -> {
where incoming: true
}, class_name: "Exchange", dependent: :destroy
has_many :cached_outgoing_exchanges, -> {
where incoming: false
}, class_name: "Exchange", dependent: :destroy
has_many :suppliers, -> { distinct }, source: :sender, through: :cached_incoming_exchanges
has_many :distributors, -> { distinct }, source: :receiver, through: :cached_outgoing_exchanges
has_many :order_cycle_schedules
has_many :order_cycle_schedules, dependent: :destroy
has_many :schedules, through: :order_cycle_schedules
has_and_belongs_to_many :selected_distributor_payment_methods,
class_name: 'DistributorPaymentMethod',

View File

@@ -36,7 +36,7 @@ module Spree
# Deletion of metadata is handled in the database.
# So we don't need the option `dependent: :destroy` as long as
# AdjustmentMetadata has no destroy logic itself.
has_one :metadata, class_name: 'AdjustmentMetadata'
has_one :metadata, class_name: 'AdjustmentMetadata', dependent: :destroy
has_many :adjustments, as: :adjustable, dependent: :destroy
belongs_to :adjustable, polymorphic: true

View File

@@ -2,7 +2,7 @@
module Spree
class Country < ApplicationRecord
has_many :states, -> { order('name ASC') }
has_many :states, -> { order('name ASC') }, dependent: :destroy
validates :name, :iso_name, presence: true

View File

@@ -45,7 +45,7 @@ module Spree
belongs_to :ship_address, class_name: 'Spree::Address'
alias_attribute :shipping_address, :ship_address
has_many :state_changes, as: :stateful
has_many :state_changes, as: :stateful, dependent: :destroy
has_many :line_items, -> {
order('created_at ASC')
}, class_name: "Spree::LineItem", dependent: :destroy
@@ -71,12 +71,12 @@ module Spree
},
class_name: 'Spree::Adjustment',
dependent: :destroy
has_many :invoices
has_many :invoices, dependent: :destroy
belongs_to :order_cycle
belongs_to :distributor, class_name: 'Enterprise'
belongs_to :customer
has_one :proxy_order
has_one :proxy_order, dependent: :destroy
has_one :subscription, through: :proxy_order
accepts_nested_attributes_for :line_items

View File

@@ -15,7 +15,7 @@ module Spree
DISPLAY = [:both, :back_end].freeze
default_scope -> { where(deleted_at: nil) }
has_many :credit_cards, class_name: "Spree::CreditCard"
has_many :credit_cards, class_name: "Spree::CreditCard", dependent: :destroy
validates :name, presence: true
validate :distributor_validation

View File

@@ -4,7 +4,7 @@ module Spree
class Property < ApplicationRecord
has_many :product_properties, dependent: :destroy
has_many :products, through: :product_properties
has_many :producer_properties
has_many :producer_properties, dependent: :destroy
validates :name, :presentation, presence: true

View File

@@ -12,7 +12,7 @@ module Spree
has_many :shipping_rates, dependent: :delete_all
has_many :shipping_methods, through: :shipping_rates
has_many :state_changes, as: :stateful
has_many :state_changes, as: :stateful, dependent: :destroy
has_many :inventory_units, dependent: :delete_all
has_many :adjustments, as: :adjustable, dependent: :destroy

View File

@@ -4,7 +4,7 @@ module Spree
class ShippingCategory < ApplicationRecord
validates :name, presence: true
has_many :products
has_many :shipping_method_categories, inverse_of: :shipping_method
has_many :shipping_method_categories, inverse_of: :shipping_method, dependent: :destroy
has_many :shipping_methods, through: :shipping_method_categories
end
end

View File

@@ -13,11 +13,11 @@ module Spree
default_scope -> { where(deleted_at: nil) }
has_many :shipping_rates, inverse_of: :shipping_method
has_many :shipping_rates, inverse_of: :shipping_method, dependent: :destroy
has_many :shipments, through: :shipping_rates
has_many :shipping_method_categories
has_many :shipping_categories, through: :shipping_method_categories
has_many :distributor_shipping_methods
has_many :shipping_method_categories, dependent: :destroy
has_many :shipping_categories, through: :shipping_method_categories, dependent: :destroy
has_many :distributor_shipping_methods, dependent: :destroy
has_many :distributors, through: :distributor_shipping_methods,
class_name: 'Enterprise',
foreign_key: 'distributor_id'

View File

@@ -8,7 +8,7 @@ module Spree
belongs_to :stock_location, class_name: 'Spree::StockLocation', inverse_of: :stock_items
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
has_many :stock_movements
has_many :stock_movements, dependent: :destroy
validates :stock_location, :variant, presence: true
validates :variant_id, uniqueness: { scope: [:stock_location_id, :deleted_at] }

View File

@@ -21,7 +21,7 @@ module Spree
belongs_to :zone, class_name: "Spree::Zone", inverse_of: :tax_rates
belongs_to :tax_category, class_name: "Spree::TaxCategory", inverse_of: :tax_rates
has_many :adjustments, as: :originator
has_many :adjustments, as: :originator, dependent: :destroy
validates :amount, presence: true, numericality: true
validates :tax_category, presence: true

View File

@@ -36,8 +36,8 @@ module Spree
foreign_key: :owner_id, inverse_of: :owner
has_many :owned_groups, class_name: 'EnterpriseGroup',
foreign_key: :owner_id, inverse_of: :owner
has_many :customers
has_many :credit_cards
has_many :customers, dependent: :destroy
has_many :credit_cards, dependent: :destroy
has_many :report_rendering_options, class_name: "::ReportRenderingOptions", dependent: :destroy
has_many :webhook_endpoints, dependent: :destroy

View File

@@ -33,12 +33,12 @@ module Spree
delegate :name, :name=, :description, :description=, :meta_keywords, to: :product
has_many :inventory_units, inverse_of: :variant
has_many :line_items, inverse_of: :variant
has_many :inventory_units, inverse_of: :variant, dependent: :destroy
has_many :line_items, inverse_of: :variant, dependent: :destroy
has_many :stock_items, dependent: :destroy, inverse_of: :variant
has_many :stock_locations, through: :stock_items
has_many :stock_movements
has_many :stock_movements, dependent: :destroy
has_many :images, -> { order(:position) }, as: :viewable,
dependent: :destroy,
class_name: "Spree::Image"
@@ -54,10 +54,10 @@ module Spree
delegate :display_price, :display_amount, :price, :price=, :currency, :currency=,
to: :find_or_build_default_price
has_many :exchange_variants
has_many :exchange_variants, dependent: :destroy
has_many :exchanges, through: :exchange_variants
has_many :variant_overrides
has_many :inventory_items
has_many :variant_overrides, dependent: :destroy
has_many :inventory_items, dependent: :destroy
localize_number :price, :weight