Skip to content

Commit cb337c8

Browse files
committed
Promotions: Specify inverse_of/dependent options
1 parent 41c66d1 commit cb337c8

File tree

8 files changed

+26
-13
lines changed

8 files changed

+26
-13
lines changed

promotions/app/models/solidus_promotions/benefit.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class Benefit < Spree::Base
1616

1717
belongs_to :promotion, inverse_of: :benefits
1818
belongs_to :original_promotion_action, class_name: "Spree::PromotionAction", optional: true
19-
has_many :adjustments, class_name: "Spree::Adjustment", as: :source
20-
has_many :shipping_rate_discounts, class_name: "SolidusPromotions::ShippingRateDiscount", inverse_of: :benefit
19+
has_many :adjustments, class_name: "Spree::Adjustment", as: :source, dependent: :restrict_with_error
20+
has_many :shipping_rate_discounts, class_name: "SolidusPromotions::ShippingRateDiscount", inverse_of: :benefit, dependent: :restrict_with_error
2121
has_many :conditions, class_name: "SolidusPromotions::Condition", inverse_of: :benefit, dependent: :destroy
2222

2323
scope :of_type, ->(type) { where(type: Array.wrap(type).map(&:to_s)) }

promotions/app/models/solidus_promotions/conditions/line_item_product.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class LineItemProduct < Condition
1111
has_many :condition_products,
1212
dependent: :destroy,
1313
foreign_key: :condition_id,
14-
class_name: "SolidusPromotions::ConditionProduct"
14+
class_name: "SolidusPromotions::ConditionProduct",
15+
inverse_of: :condition
1516
has_many :products,
1617
class_name: "Spree::Product",
1718
through: :condition_products

promotions/app/models/solidus_promotions/conditions/line_item_taxon.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ module Conditions
55
class LineItemTaxon < Condition
66
include LineItemLevelCondition
77

8-
has_many :condition_taxons, class_name: "SolidusPromotions::ConditionTaxon", foreign_key: :condition_id,
9-
dependent: :destroy
8+
has_many :condition_taxons,
9+
class_name: "SolidusPromotions::ConditionTaxon",
10+
foreign_key: :condition_id,
11+
dependent: :destroy,
12+
inverse_of: :condition
1013
has_many :taxons, through: :condition_taxons, class_name: "Spree::Taxon"
1114

1215
MATCH_POLICIES = %w[include exclude].freeze

promotions/app/models/solidus_promotions/conditions/product.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class Product < Condition
1212
has_many :condition_products,
1313
dependent: :destroy,
1414
foreign_key: :condition_id,
15-
class_name: "SolidusPromotions::ConditionProduct"
15+
class_name: "SolidusPromotions::ConditionProduct",
16+
inverse_of: :condition
1617
has_many :products, class_name: "Spree::Product", through: :condition_products
1718

1819
def preload_relations

promotions/app/models/solidus_promotions/conditions/store.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ module Conditions
55
class Store < Condition
66
include OrderLevelCondition
77

8-
has_many :condition_stores, class_name: "SolidusPromotions::ConditionStore",
8+
has_many :condition_stores,
9+
class_name: "SolidusPromotions::ConditionStore",
910
foreign_key: :condition_id,
10-
dependent: :destroy
11+
dependent: :destroy,
12+
inverse_of: :condition
1113
has_many :stores, through: :condition_stores, class_name: "Spree::Store"
1214

1315
def preload_relations

promotions/app/models/solidus_promotions/conditions/taxon.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ module Conditions
55
class Taxon < Condition
66
include LineItemApplicableOrderLevelCondition
77

8-
has_many :condition_taxons, class_name: "SolidusPromotions::ConditionTaxon", foreign_key: :condition_id,
9-
dependent: :destroy
8+
has_many :condition_taxons,
9+
class_name: "SolidusPromotions::ConditionTaxon",
10+
foreign_key: :condition_id,
11+
dependent: :destroy,
12+
inverse_of: :condition
1013
has_many :taxons, through: :condition_taxons, class_name: "Spree::Taxon"
1114

1215
def preload_relations

promotions/app/models/solidus_promotions/order_promotion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module SolidusPromotions
77
# 2. The specific code that they used
88
class OrderPromotion < Spree::Base
99
belongs_to :order, class_name: "Spree::Order"
10-
belongs_to :promotion, -> { with_discarded }, class_name: "SolidusPromotions::Promotion"
10+
belongs_to :promotion, -> { with_discarded }, class_name: "SolidusPromotions::Promotion", inverse_of: :order_promotions
1111
belongs_to :promotion_code, class_name: "SolidusPromotions::PromotionCode", optional: true
1212

1313
validates :promotion_code, presence: true, if: :require_promotion_code?

promotions/app/models/solidus_promotions/promotion.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ class Promotion < Spree::Base
66

77
include Spree::SoftDeletable
88

9-
belongs_to :category, class_name: "SolidusPromotions::PromotionCategory",
10-
foreign_key: :promotion_category_id, optional: true
9+
belongs_to :category,
10+
class_name: "SolidusPromotions::PromotionCategory",
11+
foreign_key: :promotion_category_id,
12+
optional: true,
13+
inverse_of: :promotions
1114
belongs_to :original_promotion, class_name: "Spree::Promotion", optional: true
1215
has_many :benefits, class_name: "SolidusPromotions::Benefit", dependent: :destroy
1316
has_many :conditions, through: :benefits

0 commit comments

Comments
 (0)