Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(order)

def call(persist: true)
all_items = line_items + shipments
ActiveRecord::Associations::Preloader.new(records: all_items, associations: :adjustments).call
all_items.each do |item|
promotion_adjustments = item.adjustments.select(&:promotion?)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def initialize(_adjustments)
let(:order) { create(:order_with_line_items, line_items_count: 1, line_items_price: 10) }
let(:line_item) { order.line_items[0] }

context "with multiple line items" do
let(:order) { create(:order_with_line_items, line_items_count: 3) }

before { order.reload }

it "loads line item adjustments in a single query" do
expect { subject }.to make_database_queries(matching: /from .spree_adjustments..*adjustable_id. IN \(/im, count: 1)
end
end

context "when the quantity changes with a CreateQuantityAdjustments promotion" do
let(:promotion) { create(:promotion, promotion_actions: [promotion_action]) }
let(:promotion_action) { Spree::Promotion::Actions::CreateQuantityAdjustments.new(calculator:, preferred_group_size: 2) }
Expand Down
2 changes: 2 additions & 0 deletions promotions/app/models/solidus_promotions/order_adjuster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def initialize(order, dry_run_promotion: nil)
def call(persist: true) # rubocop:disable Lint/UnusedMethodArgument
return order unless SolidusPromotions::Promotion.order_activatable?(order)

ActiveRecord::Associations::Preloader.new(records: order.line_items + order.shipments, associations: :adjustments).call
Comment thread
ikraamg marked this conversation as resolved.

SetDiscountsToZero.call(order)

DiscountOrder.new(order, promotions, dry_run: dry_run).call
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,21 @@
end
end
end

context "with multiple line items" do
Comment thread
ikraamg marked this conversation as resolved.
let(:order) { create(:order_with_line_items, line_items_count: 3) }
let!(:benefit) do
SolidusPromotions::Benefits::AdjustLineItem.create(promotion: promotion, calculator: calculator)
Comment thread
ikraamg marked this conversation as resolved.
end

before do
order_adjuster.call
order.save!
order.reload
end

it "loads line item adjustments in a single query" do
Comment thread
ikraamg marked this conversation as resolved.
expect { order_adjuster.call }.to make_database_queries(matching: /from .spree_adjustments..*adjustable_id. IN \(/im, count: 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [133/80]

end
end
end
Loading