Class: Spree::Calculator::Shipping::FlexiRate

Inherits:
ShippingCalculator show all
Defined in:
app/models/spree/calculator/shipping/flexi_rate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ShippingCalculator

#available?, #compute_shipment

Methods inherited from Spree::Calculator

#available?, calculators, #compute, #description, register, #to_s

Class Method Details

.descriptionObject



11
12
13
# File 'app/models/spree/calculator/shipping/flexi_rate.rb', line 11

def self.description
  Spree.t(:shipping_flexible_rate)
end

Instance Method Details

#compute_package(package) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/spree/calculator/shipping/flexi_rate.rb', line 15

def compute_package(package)
  content_items = package.contents
  sum = 0
  max = self.preferred_max_items.to_i
  items_count = content_items.map(&:quantity).sum
  items_count.times do |i|
    # check max value to avoid divide by 0 errors
    if (max == 0 && i == 0) || (max > 0) && (i % max == 0)
      sum += self.preferred_first_item.to_f
    else
      sum += self.preferred_additional_item.to_f
    end
  end

  sum
end