class Returnly::DiscountCalculator
Constants
- DISCOUNTER_CLASSES
Attributes
order[RW]
Public Class Methods
new(order)
click to toggle source
# File lib/returnly/discount_calculator.rb, line 10 def initialize(order) @order = order end
Public Instance Methods
calculate(line_items = [])
click to toggle source
# File lib/returnly/discount_calculator.rb, line 14 def calculate(line_items = []) line_items.inject(0) do |discount_amount, item| line_item = find_line_item(item[:order_line_item_id]) next discount_amount if line_item.nil? discount_amount += discount_amount_for(line_item, item[:units].to_i) discount_amount end end
Private Instance Methods
build_discounter_by(promotion_adjustment)
click to toggle source
# File lib/returnly/discount_calculator.rb, line 37 def build_discounter_by(promotion_adjustment) discounter_class = DISCOUNTER_CLASSES[promotion_adjustment.adjustable_type] return nil if discounter_class.nil? discounter_class.new(order, promotion_adjustment) end
discount_amount_for(line_item, units)
click to toggle source
# File lib/returnly/discount_calculator.rb, line 26 def discount_amount_for(line_item, units) discounters.inject(0) do |amount, discounter| amount += discounter.discount_amount(line_item, units) amount end end
discounters()
click to toggle source
# File lib/returnly/discount_calculator.rb, line 33 def discounters order_promotion_adjustments.map { |promotion_adjustment| build_discounter_by(promotion_adjustment) }.compact end
find_line_item(id)
click to toggle source
# File lib/returnly/discount_calculator.rb, line 47 def find_line_item(id) order.line_items.find_by(id: id) end
order_promotion_adjustments()
click to toggle source
# File lib/returnly/discount_calculator.rb, line 43 def order_promotion_adjustments order.all_adjustments.where(source_type: 'Spree::PromotionAction', eligible: true) end