Class: Spree::Calculator::FlexiRate

Inherits:
Spree::Calculator show all
Defined in:
app/models/spree/calculator/flexi_rate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::Calculator

#available?, calculators, #description, #to_s

Methods inherited from Base

belongs_to_required_by_default, 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

.available?(_object) ⇒ Boolean


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

def self.available?(_object)
  true
end

.descriptionObject


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

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

Instance Method Details

#compute(object) ⇒ Object


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

def compute(object)
  sum = 0
  max = preferred_max_items.to_i
  items_count = object.quantity
  items_count.times do |i|
    if i == 0
      sum += preferred_first_item.to_f
    elsif ((max > 0) && (i <= (max - 1))) || (max == 0)
      sum += preferred_additional_item.to_f
    end
  end

  sum
end