Skip to content

Commit

Permalink
Use Spree::{Order;LineItem;ShippingRate} in all models
Browse files Browse the repository at this point in the history
It's less change to all rules and actions if we just use the core
Solidus models to store discounts.
  • Loading branch information
mamhoff authored and Astr0surf3r committed Nov 19, 2024
1 parent 638b7a5 commit 9ee7585
Show file tree
Hide file tree
Showing 29 changed files with 48 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module SolidusFriendlyPromotions
module Actions
class AdjustLineItem < PromotionAction
def can_discount?(object)
object.is_a? Discountable::LineItem
object.is_a? Spree::LineItem
end

def available_calculators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module SolidusFriendlyPromotions
module Actions
class AdjustShipment < PromotionAction
def can_discount?(object)
object.is_a?(Discountable::Shipment) || object.is_a?(Discountable::ShippingRate)
object.is_a?(Spree::Shipment) || object.is_a?(Spree::ShippingRate)
end

def available_calculators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ class FriendlyPromotionDiscounter
attr_reader :order, :promotions

def initialize(order)
@order = Discountable::Order.new(order)
@order = order
@promotions = PromotionEligibility.new(promotable: order, possible_promotions: possible_promotions).call
end

def call
return nil if order.shipped?

order.reset_current_discounts

SolidusFriendlyPromotions::Promotion.ordered_lanes.each do |lane, _index|
lane_promotions = promotions.select { |promotion| promotion.lane == lane }
item_discounter = ItemDiscounter.new(promotions: lane_promotions)
line_item_discounts = adjust_line_items(item_discounter)
shipment_discounts = adjust_shipments(item_discounter)
shipping_rate_discounts = adjust_shipping_rates(item_discounter)
(line_item_discounts + shipment_discounts + shipping_rate_discounts).each do |item, chosen_discounts|
item.discounts.concat(chosen_discounts)
item.current_discounts.concat(chosen_discounts)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ def initialize(order)
def call
discountable_order = FriendlyPromotionDiscounter.new(order).call

discountable_order.line_items.each do |discountable_line_item|
update_adjustments(discountable_line_item.line_item, discountable_line_item.discounts)
discountable_order.line_items.each do |line_item|
update_adjustments(line_item, line_item.current_discounts)
end

discountable_order.shipments.each do |discountable_shipment|
update_adjustments(discountable_shipment.shipment, discountable_shipment.discounts)
discountable_order.shipments.each do |shipment|
update_adjustments(shipment, shipment.current_discounts)
end

discountable_order.shipments.flat_map(&:shipping_rates).each do |discountable_shipping_rate|
spree_shipping_rate = discountable_shipping_rate.shipping_rate
spree_shipping_rate.discounts = discountable_shipping_rate.discounts.map do |discount|
discountable_order.shipments.flat_map(&:shipping_rates).each do |shipping_rate|
shipping_rate.discounts = shipping_rate.current_discounts.map do |discount|
SolidusFriendlyPromotions::ShippingRateDiscount.new(
shipping_rate: spree_shipping_rate,
shipping_rate: shipping_rate,
amount: discount.amount,
label: discount.label
)
Expand All @@ -42,17 +41,18 @@ def call
#
# @private
# @param [#adjustments] item a {Spree::LineItem} or {Spree::Shipment}
# @param [Array<SolidusFriendlyPromotions::ItemDiscount>] taxed_items a list of calculated discounts for an item
# @param [Array<SolidusFriendlyPromotions::ItemDiscount>] item_discounts a list of calculated discounts for an item
# @return [void]
def update_adjustments(item, taxed_items)
def update_adjustments(item, item_discounts)
promotion_adjustments = item.adjustments.select(&:friendly_promotion?)

active_adjustments = taxed_items.map do |tax_item|
update_adjustment(item, tax_item)
active_adjustments = item_discounts.map do |item_discount|
update_adjustment(item, item_discount)
end
item.update(promo_total: active_adjustments.sum(&:amount))
# Remove any tax adjustments tied to rates which no longer match.
unmatched_adjustments = promotion_adjustments - active_adjustments

item.adjustments.destroy(unmatched_adjustments)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FirstOrder < PromotionRule
attr_reader :user, :email

def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FirstRepeatPurchaseSince < PromotionRule

# This promotion is applicable to orders only.
def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

# This is never eligible if the order does not have a user, and that user does not have any previous completed orders.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.operator_options
end

def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LineItemOptionValue < PromotionRule
preference :eligible_values, :hash

def applicable?(promotable)
promotable.is_a?(Discountable::LineItem)
promotable.is_a?(Spree::LineItem)
end

def eligible?(line_item, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LineItemProduct < PromotionRule
preference :match_policy, :string, default: MATCH_POLICIES.first

def applicable?(promotable)
promotable.is_a?(Discountable::LineItem)
promotable.is_a?(Spree::LineItem)
end

def eligible?(line_item, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LineItemTaxon < PromotionRule

preference :match_policy, :string, default: MATCH_POLICIES.first
def applicable?(promotable)
promotable.is_a?(Discountable::LineItem)
promotable.is_a?(Spree::LineItem)
end

def eligible?(line_item, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NthOrder < PromotionRule

# This promotion is applicable to orders only.
def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

# This is never eligible if the order does not have a user, and that user does not have any previous completed orders.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module SolidusFriendlyPromotions
module Rules
class OneUsePerUser < PromotionRule
def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class OptionValue < PromotionRule
preference :eligible_values, :hash

def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def eligible_products
end

def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ShippingMethod < PromotionRule
preference :shipping_method_ids, type: :array, default: []

def applicable?(promotable)
promotable.is_a?(Discountable::Shipment) || promotable.is_a?(Discountable::ShippingRate)
promotable.is_a?(Spree::Shipment) || promotable.is_a?(Spree::ShippingRate)
end

def eligible?(promotable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def preload_relations
end

def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def preload_relations

preference :match_policy, :string, default: MATCH_POLICIES.first
def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def preload_relations
end

def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module SolidusFriendlyPromotions
module Rules
class UserLoggedIn < PromotionRule
def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UserRole < PromotionRule
preference :match_policy, default: MATCH_POLICIES.first

def applicable?(promotable)
promotable.is_a?(Discountable::Order)
promotable.is_a?(Spree::Order)
end

def eligible?(order, _options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
subject { action.can_discount?(promotable) }

context "with a line item" do
let(:promotable) { SolidusFriendlyPromotions::Discountable::LineItem.new(Spree::Order.new, order: double) }
let(:promotable) { Spree::Order.new }

it { is_expected.to be false }
end

context "with a shipment" do
let(:promotable) { SolidusFriendlyPromotions::Discountable::Shipment.new(Spree::Shipment.new, order: double) }
let(:promotable) { Spree::Shipment.new }

it { is_expected.to be true }
end

context "with a shipping rate" do
let(:promotable) { SolidusFriendlyPromotions::Discountable::ShippingRate.new(Spree::ShippingRate.new, shipment: double) }
let(:promotable) { Spree::ShippingRate.new }

it { is_expected.to be true }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
end
let(:rules) { [] }
let(:action) { SolidusFriendlyPromotions::Actions::AdjustLineItem.create(calculator: calculator) }
let(:spree_order) { create(:order_with_line_items, line_items_attributes: line_items_attributes) }
let(:order) { SolidusFriendlyPromotions::Discountable::Order.new(spree_order) }
let(:order) { create(:order_with_line_items, line_items_attributes: line_items_attributes) }
let(:currency) { "USD" }

context "applied to an order" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
context "compute" do
let(:currency) { "USD" }
let(:order) { double(currency: currency) }
let(:line_item) { double("SolidusFriendlyPromotions::Discountable::LineItem", discountable_amount: 100, order: order) }
let(:line_item) { double("Spree::LineItem", discountable_amount: 100, order: order) }

before { subject.preferred_percent = 15 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
line_items_price: amount
)
end
let(:line_item) { SolidusFriendlyPromotions::Discountable::LineItem.new(order.line_items.first, order: order) }
let(:line_item) { order.line_items.first }
let(:preferred_currency) { "USD" }

before do
Expand Down Expand Up @@ -109,8 +109,7 @@
context "with a shipment" do
subject { calculator.compute(shipment) }

let(:spree_shipment) { Spree::Shipment.new(order: order, amount: shipping_cost) }
let(:shipment) { SolidusFriendlyPromotions::Discountable::Shipment.new(spree_shipment, order: order) }
let(:shipment) { Spree::Shipment.new(order: order, amount: shipping_cost) }
let(:line_item_count) { 1 }
let(:amount) { 10 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
require "spec_helper"

RSpec.describe SolidusFriendlyPromotions::DistributedAmountsHandler, type: :model do
let(:spree_order) do
let(:order) do
FactoryBot.create(
:order_with_line_items,
line_items_attributes: line_items_attributes
)
end

let(:order) do
SolidusFriendlyPromotions::Discountable::Order.new(spree_order)
end

let(:handler) {
described_class.new(order.line_items, total_amount)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

let(:variant) { create(:variant) }
let(:order) { create(:order) }
let(:discountable) { SolidusFriendlyPromotions::Discountable::LineItem.new(line_item, order: SolidusFriendlyPromotions::Discountable::Order.new(order)) }
let(:line_item) { Spree::LineItem.new(order: order, variant: variant, price: 10) }
let(:discountable) { Spree::LineItem.new(order: order, variant: variant, price: 10) }
let(:promotion) { SolidusFriendlyPromotions::Promotion.new(name: "20 Perzent off") }
let(:action) { described_class.new(promotion: promotion) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
subject { described_class.new.applicable?(promotable) }

context "when the promotable is an order" do
let(:promotable) { SolidusFriendlyPromotions::Discountable::Order.new(Spree::Order.new) }
let(:promotable) { Spree::Order.new }

it { is_expected.to be true }
end

context "when the promotable is not a order" do
let(:promotable) { SolidusFriendlyPromotions::Discountable::LineItem.new(Spree::LineItem.new, order: double) }
let(:promotable) { Spree::LineItem.new }

it { is_expected.to be false }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
subject { described_class.new.applicable?(promotable) }

context "when the promotable is an order" do
let(:promotable) { SolidusFriendlyPromotions::Discountable::Order.new(Spree::Order.new) }
let(:promotable) { Spree::Order.new }

it { is_expected.to be true }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
subject { rule.applicable?(promotable) }

context "when promotable is an order" do
let(:promotable) { SolidusFriendlyPromotions::Discountable::Order.new(Spree::Order.new) }
let(:promotable) { Spree::Order.new }

it { is_expected.to be true }
end

context "when promotable is not an order" do
let(:promotable) { SolidusFriendlyPromotions::Discountable::LineItem.new(Spree::LineItem.new, order: double) }
let(:promotable) { Spree::LineItem.new }

it { is_expected.to be false }
end
Expand Down

0 comments on commit 9ee7585

Please sign in to comment.