Class: Calculator::Advanced

Inherits:
Calculator
  • Object
show all
Defined in:
app/models/calculator/advanced.rb

Direct Known Subclasses

QuantityBucket, WeightBucket

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registerObject



7
8
9
10
11
12
13
14
# File 'app/models/calculator/advanced.rb', line 7

def self.register
  super
  # Chetan -> to use with promotions remove below comment
  # Coupon.register_calculator(self)
  ShippingMethod.register_calculator(self)
  # Chetan -> I suppose we are not using shipping rates anymore in recent versions of Spree
  # ShippingRate.register_calculator(self)
end

Instance Method Details

#get_rate(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/calculator/advanced.rb', line 28

def get_rate(value)
  # First try to find where price falls within price floor and ceiling
  bucket = BucketRate.find(:first,
    :conditions => [
      "calculator_id = ? and floor <= ? and ceiling > ?",
      self.id, value, value
    ])

  if bucket
    return(bucket.rate)
  else
    # find largest one
    bucket = BucketRate.find(:last, :conditions => ['calculator_id = ?', self.id], :order => "ceiling ASC")
    # check if we've found largest one, and item_total is higher then ceiling
    if bucket && value > bucket.ceiling
      return(bucket.rate)
    else
      return(false) # if there's no rates, or we've hit a hole, let calculator use default rate.
    end
  end
end

#nameObject



20
21
22
# File 'app/models/calculator/advanced.rb', line 20

def name
  calculable.respond_to?(:name) ? calculable.name : calculable.to_s
end

#set_advancedObject



16
17
18
# File 'app/models/calculator/advanced.rb', line 16

def set_advanced
  self.advanced = true
end

#unitObject



24
25
26
# File 'app/models/calculator/advanced.rb', line 24

def unit
  self.class.unit
end