Class: Spree::Calculator::PerItem
- Inherits:
-
Spree::Calculator
- Object
- ActiveRecord::Base
- Spree::Calculator
- Spree::Calculator::PerItem
- Defined in:
- app/models/spree/calculator/per_item.rb
Class Method Summary collapse
Instance Method Summary collapse
- #compute(object = nil) ⇒ Object
-
#matching_products ⇒ Object
Returns all products that match this calculator, but only if the calculator is attached to a promotion.
Methods inherited from Spree::Calculator
#available?, calculators, #description, register, #to_s
Class Method Details
.description ⇒ Object
9 10 11 |
# File 'app/models/spree/calculator/per_item.rb', line 9 def self.description I18n.t(:flat_rate_per_item) end |
Instance Method Details
#compute(object = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/models/spree/calculator/per_item.rb', line 13 def compute(object=nil) return 0 if object.nil? self.preferred_amount * object.line_items.reduce(0) do |sum, value| if !matching_products || matching_products.include?(value.product) value_to_add = value.quantity else value_to_add = 0 end sum + value_to_add end end |
#matching_products ⇒ Object
Returns all products that match this calculator, but only if the calculator is attached to a promotion. If attached to a ShippingMethod, nil is returned.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/spree/calculator/per_item.rb', line 27 def matching_products # Regression check for #1596 # Calculator::PerItem can be used in two cases. # The first is in a typical promotion, providing a discount per item of a particular item # The second is a ShippingMethod, where it applies to an entire order # # Shipping methods do not have promotions attached, but promotions do # Therefore we must check for promotions if self.calculable.respond_to?(:promotion) self.calculable.promotion.rules.map(&:products).flatten end end |