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

Refactor: Extract Promotion#applicable_line_items #86

Merged
merged 1 commit into from
Nov 10, 2023
Merged
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 @@ -58,7 +58,7 @@ def compute_amount(line_item)
adjustment_amount = adjustment_amount.abs

order = line_item.order
line_items = actionable_line_items(order)
line_items = promotion.applicable_line_items(order)

item_units = line_items.sort_by do |line_item|
[-line_item.quantity, line_item.id]
Expand All @@ -78,14 +78,6 @@ def compute_amount(line_item)

private

def actionable_line_items(order)
order.discountable_line_items.select do |item|
promotion.rules.select do |rule|
rule.applicable?(item)
end.all? { |rule| rule.eligible?(item) }
end
end

##
# Used specifically for PercentOnLineItem calculator. That calculator uses
# `line_item.amount`, however we might not necessarily want to discount the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,14 @@ def compute_line_item(line_item)
return 0 unless line_item
return 0 unless preferred_currency.casecmp(line_item.currency).zero?

distributable_line_items = eligible_line_items(line_item.order)
distributable_line_items = calculable.promotion.applicable_line_items(line_item.order)
return 0 unless line_item.in?(distributable_line_items)

DistributedAmountsHandler.new(
distributable_line_items,
preferred_amount
).amount(line_item)
end

private

def eligible_line_items(order)
order.discountable_line_items.select do |line_item|
calculable.promotion.eligible_by_applicable_rules?(line_item)
end
end
end
end
end
6 changes: 6 additions & 0 deletions app/models/solidus_friendly_promotions/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ def eligible_by_applicable_rules?(promotable, dry_run: false)
end.all?
end

def applicable_line_items(order)
order.discountable_line_items.select do |line_item|
eligible_by_applicable_rules?(line_item)
end
end

private

def apply_automatically_disallowed_with_paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ class MinimumQuantity < PromotionRule
# @param order [Spree::Order] the order we want to check eligibility on
# @return [Boolean] true if promotion is eligible, false otherwise
def eligible?(order)
applicable_line_items = order.line_items.select do |line_item|
promotion.rules.select do |rule|
rule.applicable?(line_item)
end.all? { _1.eligible?(line_item) }
end

if applicable_line_items.sum(&:quantity) < preferred_minimum_quantity
if promotion.applicable_line_items(order).sum(&:quantity) < preferred_minimum_quantity
eligibility_errors.add(
:base,
eligibility_error_message(:quantity_less_than_minimum, count: preferred_minimum_quantity),
Expand Down