Skip to content

Commit

Permalink
Merge pull request #10 from friendlycart/remove-delegators
Browse files Browse the repository at this point in the history
Remove delegators
  • Loading branch information
mamhoff authored Oct 3, 2023
2 parents dea262b + 068650d commit c516b62
Show file tree
Hide file tree
Showing 46 changed files with 220 additions and 297 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module SolidusFriendlyPromotions
module LineItemDecorator
Spree::LineItem.prepend SolidusFriendlyPromotions::DiscountableAmount
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def ensure_promotions_eligible
super
end

def reset_current_discounts
line_items.each(&:reset_current_discounts)
shipments.each(&:reset_current_discounts)
end

Spree::Order.prepend self
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module SolidusFriendlyPromotions
module ShipmentDecorator
Spree::Shipment.prepend SolidusFriendlyPromotions::DiscountableAmount

def reset_current_discounts
super
shipping_rates.each(&:reset_current_discounts)
end

Spree::Shipment.prepend self
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def total_before_tax
def promo_total
discounts.sum(&:amount)
end

Spree::ShippingRate.prepend SolidusFriendlyPromotions::DiscountableAmount
Spree::ShippingRate.prepend self
end
end

Spree::ShippingRate.prepend(SolidusFriendlyPromotions::ShippingRateDecorator)
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
23 changes: 0 additions & 23 deletions app/models/solidus_friendly_promotions/discountable/line_item.rb

This file was deleted.

19 changes: 0 additions & 19 deletions app/models/solidus_friendly_promotions/discountable/order.rb

This file was deleted.

24 changes: 0 additions & 24 deletions app/models/solidus_friendly_promotions/discountable/shipment.rb

This file was deleted.

This file was deleted.

21 changes: 21 additions & 0 deletions app/models/solidus_friendly_promotions/discountable_amount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module SolidusFriendlyPromotions
module DiscountableAmount
def discountable_amount
amount + current_discounts.sum(&:amount)
end

def current_discounts
@current_discounts ||= []
end

def current_discounts=(args)
@current_discounts = args
end

def reset_current_discounts
@current_discounts = []
end
end
end
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
24 changes: 12 additions & 12 deletions app/models/solidus_friendly_promotions/order_discounter.rb
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
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/rules/item_total.rb
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
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/rules/nth_order.rb
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
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/rules/product.rb
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
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/rules/store.rb
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
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/rules/taxon.rb
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
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/rules/user.rb
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
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/rules/user_role.rb
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
Loading

0 comments on commit c516b62

Please sign in to comment.