Skip to content

Commit

Permalink
Legacy Promotions: Move ransackable promo associations from core
Browse files Browse the repository at this point in the history
`Spree::Order#promotions` and `Spree::Order#order_promotions` are now
only defined in the `legacy_promotions` gem. Thus they should not be
safelisted as ransackable in core.
  • Loading branch information
mamhoff authored and tvdeyen committed Nov 4, 2024
1 parent 78e5f28 commit 58b6d35
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class CannotRebuildShipments < StandardError; end
go_to_state :confirm
end

self.allowed_ransackable_associations = %w[shipments user order_promotions promotions bill_address ship_address line_items]
self.allowed_ransackable_associations = %w[shipments user bill_address ship_address line_items]
self.allowed_ransackable_attributes = %w[completed_at created_at email number state payment_state shipment_state total store_id]

attr_reader :coupon_code
Expand Down
6 changes: 6 additions & 0 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
let(:user) { create(:user, email: "[email protected]") }
let(:order) { create(:order, user:, store:) }

describe ".ransackable_associations" do
subject { described_class.ransackable_associations }

it { is_expected.to contain_exactly("user", "line_items", "shipments", "bill_address", "ship_address") }
end

context '#store' do
it { is_expected.to respond_to(:store) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

module SolidusLegacyPromotions
module SpreeOrderDecorator
module ClassMethods
def allowed_ransackable_associations
super + ["promotions", "order_promotions"]
end
end

def self.prepended(base)
base.has_many :order_promotions, class_name: 'Spree::OrderPromotion', dependent: :destroy
base.has_many :promotions, through: :order_promotions
Expand All @@ -17,5 +23,6 @@ def shipping_discount
end

Spree::Order.prepend(self)
Spree::Order.singleton_class.prepend self::ClassMethods
end
end
6 changes: 6 additions & 0 deletions legacy_promotions/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
it { is_expected.to respond_to(:order_promotions) }
it { is_expected.to respond_to(:promotions) }

describe ".ransackable_associations" do
subject { described_class.ransackable_associations }

it { is_expected.to include("promotions", "order_promotions") }
end

context "#apply_shipping_promotions" do
let(:order) { build(:order) }

Expand Down

0 comments on commit 58b6d35

Please sign in to comment.