Skip to content

Commit

Permalink
Merge pull request #86 from friendlycart/extract-applicable-line-items
Browse files Browse the repository at this point in the history
Refactor: Extract `Promotion#applicable_line_items`
  • Loading branch information
tvdeyen authored Nov 10, 2023
2 parents f937d39 + cf08777 commit a28ca37
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 25 deletions.
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
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

0 comments on commit a28ca37

Please sign in to comment.