Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dependent to associations #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/app/models/concerns/spree/user_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module UserMethods
include Spree::RansackableAttributes

included do
has_many :role_users, class_name: 'Spree::RoleUser', foreign_key: :user_id
has_many :role_users, class_name: 'Spree::RoleUser', foreign_key: :user_id, dependent: :destroy
has_many :spree_roles, through: :role_users, class_name: 'Spree::Role', source: :role

has_many :promotion_rule_users, class_name: 'Spree::PromotionRuleUser'
has_many :promotion_rule_users, class_name: 'Spree::PromotionRuleUser', dependent: :destroy
has_many :promotion_rules, through: :promotion_rule_users, class_name: 'Spree::PromotionRule'

has_many :orders, foreign_key: :user_id, class_name: "Spree::Order"
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Spree
class CreditCard < Spree::Base
belongs_to :payment_method
belongs_to :user, class_name: Spree.user_class, foreign_key: 'user_id'
has_many :payments, as: :source
has_many :payments, as: :source, dependent: :restrict_with_error

before_save :set_last_digits

Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/option_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class OptionType < Spree::Base

has_many :products, through: :product_option_types

has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype'
has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype', dependent: :destroy
has_many :prototypes, through: :option_type_prototypes, class_name: 'Spree::Prototype'

with_options presence: true do
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Order < Spree::Base
dependent: :destroy,
inverse_of: :order

has_many :order_promotions, class_name: 'Spree::OrderPromotion'
has_many :order_promotions, class_name: 'Spree::OrderPromotion', dependent: :destroy
has_many :promotions, through: :order_promotions, class_name: 'Spree::Promotion'

has_many :shipments, dependent: :destroy, inverse_of: :order do
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Payment < Spree::Base
belongs_to :source, polymorphic: true

has_many :offsets, -> { offset_payment }, class_name: "Spree::Payment", foreign_key: :source_id
has_many :log_entries, as: :source
has_many :state_changes, as: :stateful
has_many :log_entries, as: :source, dependent: :restrict_with_error
has_many :state_changes, as: :stateful, dependent: :restrict_with_error
has_many :capture_events, class_name: 'Spree::PaymentCaptureEvent'
has_many :refunds, inverse_of: :payment

Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class Product < Spree::Base
has_many :product_properties, dependent: :destroy, inverse_of: :product
has_many :properties, through: :product_properties

has_many :classifications, dependent: :delete_all, inverse_of: :product
has_many :classifications, dependent: :destroy, inverse_of: :product
has_many :taxons, through: :classifications, before_remove: :remove_taxon

has_many :product_promotion_rules, class_name: 'Spree::ProductPromotionRule'
has_many :product_promotion_rules, class_name: 'Spree::ProductPromotionRule', dependent: :destroy
has_many :promotion_rules, through: :product_promotion_rules, class_name: 'Spree::PromotionRule'

belongs_to :tax_category, class_name: 'Spree::TaxCategory'
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Promotion < Spree::Base
has_many :promotion_actions, autosave: true, dependent: :destroy
alias_method :actions, :promotion_actions

has_many :order_promotions, class_name: 'Spree::OrderPromotion'
has_many :order_promotions, class_name: 'Spree::OrderPromotion', dependent: :destroy
has_many :orders, through: :order_promotions, class_name: 'Spree::Order'

accepts_nested_attributes_for :promotion_actions, :promotion_rules
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion/rules/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Promotion
module Rules
class Product < PromotionRule
has_many :product_promotion_rules, class_name: 'Spree::ProductPromotionRule',
foreign_key: :promotion_rule_id
foreign_key: :promotion_rule_id, dependent: :destroy
has_many :products, through: :product_promotion_rules, class_name: 'Spree::Product'

MATCH_POLICIES = %w(any all none)
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion/rules/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Spree
class Promotion
module Rules
class Taxon < PromotionRule
has_many :promotion_rule_taxons, class_name: 'Spree::PromotionRuleTaxon', foreign_key: 'promotion_rule_id'
has_many :promotion_rule_taxons, class_name: 'Spree::PromotionRuleTaxon', foreign_key: 'promotion_rule_id', dependent: :destroy
has_many :taxons, through: :promotion_rule_taxons, class_name: 'Spree::Taxon'

MATCH_POLICIES = %w(any all)
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/promotion/rules/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class User < PromotionRule
belongs_to :user, class_name: "::#{Spree.user_class.to_s}"

has_many :promotion_rule_users, class_name: 'Spree::PromotionRuleUser',
foreign_key: :promotion_rule_id
foreign_key: :promotion_rule_id, dependent: :destroy
has_many :users, through: :promotion_rule_users, class_name: "::#{Spree.user_class.to_s}"

def applicable?(promotable)
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/property.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Spree
class Property < Spree::Base
has_many :property_prototypes, class_name: 'Spree::PropertyPrototype'
has_many :property_prototypes, class_name: 'Spree::PropertyPrototype', dependent: :destroy
has_many :prototypes, through: :property_prototypes, class_name: 'Spree::Prototype'

has_many :product_properties, dependent: :delete_all, inverse_of: :property
has_many :product_properties, dependent: :destroy, inverse_of: :property
has_many :products, through: :product_properties

validates :name, :presentation, presence: true
Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/prototype.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Spree
class Prototype < Spree::Base
has_many :property_prototypes, class_name: 'Spree::PropertyPrototype'
has_many :property_prototypes, class_name: 'Spree::PropertyPrototype', dependent: :destroy
has_many :properties, through: :property_prototypes, class_name: 'Spree::Property'

has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype'
has_many :option_type_prototypes, class_name: 'Spree::OptionTypePrototype', dependent: :destroy
has_many :option_types, through: :option_type_prototypes, class_name: 'Spree::OptionType'

has_many :prototype_taxons, class_name: 'Spree::PrototypeTaxon'
has_many :prototype_taxons, class_name: 'Spree::PrototypeTaxon', dependent: :destroy
has_many :taxons, through: :prototype_taxons, class_name: 'Spree::Taxon'

validates :name, presence: true
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/role.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Spree
class Role < Spree::Base
has_many :role_users, class_name: 'Spree::RoleUser'
has_many :role_users, class_name: 'Spree::RoleUser', dependent: :destroy
has_many :users, through: :role_users, class_name: Spree.user_class.to_s

validates :name, presence: true, uniqueness: { allow_blank: true }
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class Shipment < Spree::Base
end
belongs_to :stock_location, class_name: 'Spree::StockLocation'

with_options dependent: :delete_all do
with_options dependent: :destroy do
has_many :adjustments, as: :adjustable
has_many :inventory_units, inverse_of: :shipment
has_many :shipping_rates, -> { order(:cost) }
end
has_many :shipping_methods, through: :shipping_rates
has_many :state_changes, as: :stateful
has_many :state_changes, as: :stateful, dependent: :restrict_with_error

after_save :update_adjustments

Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/shipping_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ShippingCategory < Spree::Base

with_options inverse_of: :shipping_category do
has_many :products
has_many :shipping_method_categories
has_many :shipping_method_categories, dependent: :destroy
end
has_many :shipping_methods, through: :shipping_method_categories
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/shipping_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ShippingMethod < Spree::Base

has_many :shipping_method_categories, dependent: :destroy
has_many :shipping_categories, through: :shipping_method_categories
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_zones, class_name: 'Spree::ShippingMethodZone',
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/stock_location.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Spree
class StockLocation < Spree::Base
has_many :shipments
has_many :stock_items, dependent: :delete_all, inverse_of: :stock_location
has_many :stock_items, dependent: :destroy, inverse_of: :stock_location
has_many :stock_movements, through: :stock_items

belongs_to :state, class_name: 'Spree::State'
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/stock_transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class StockTransfer < Spree::Base
extend FriendlyId
friendly_id :number, slug_column: :number, use: :slugged

has_many :stock_movements, as: :originator
has_many :stock_movements, as: :originator, dependent: :restrict_with_error

belongs_to :source_location, class_name: 'StockLocation'
belongs_to :destination_location, class_name: 'StockLocation'
Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Taxon < Spree::Base
acts_as_nested_set dependent: :destroy

belongs_to :taxonomy, class_name: 'Spree::Taxonomy', inverse_of: :taxons
has_many :classifications, -> { order(:position) }, dependent: :delete_all, inverse_of: :taxon
has_many :classifications, -> { order(:position) }, dependent: :destroy, inverse_of: :taxon
has_many :products, through: :classifications

has_many :prototype_taxons, class_name: 'Spree::PrototypeTaxon'
has_many :prototype_taxons, class_name: 'Spree::PrototypeTaxon', dependent: :destroy
has_many :prototypes, through: :prototype_taxons, class_name: 'Spree::Prototype'

has_many :promotion_rule_taxons, class_name: 'Spree::PromotionRuleTaxon'
has_many :promotion_rule_taxons, class_name: 'Spree::PromotionRuleTaxon', dependent: :destroy
has_many :promotion_rules, through: :promotion_rule_taxons, class_name: 'Spree::PromotionRule'

validates :name, presence: true
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def build_option_type_with_values(name, values)

it "will delete all classifications" do
reflection = Spree::Product.reflect_on_association(:classifications)
expect(reflection.options[:dependent]).to eq(:delete_all)
expect(reflection.options[:dependent]).to eq(:destroy)
end
end

Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def perform() true end
context "#destroy" do
it "destroys linked shipping_rates" do
reflection = Spree::Shipment.reflect_on_association(:shipping_rates)
expect(reflection.options[:dependent]).to be(:delete_all)
expect(reflection.options[:dependent]).to be(:destroy)
end
end

Expand Down