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, #to_s

Methods inherited from Base

page, spree_base_scopes

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

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_from_quantity(quantity) ⇒ Object



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

def compute_from_quantity(quantity)
  sum = 0
  max = self.preferred_max_items.to_i
  quantity.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

#compute_package(package) ⇒ Object



15
16
17
# File 'app/models/spree/calculator/shipping/flexi_rate.rb', line 15

def compute_package(package)
  compute_from_quantity(package.contents.sum(&:quantity))
end