Module: Spree::Core::CalculatedAdjustments::InstanceMethods

Defined in:
lib/spree/core/calculated_adjustments.rb

Instance Method Summary collapse

Instance Method Details

#calculator_typeObject



19
20
21
# File 'lib/spree/core/calculated_adjustments.rb', line 19

def calculator_type
  calculator.class.to_s if calculator
end

#calculator_type=(calculator_type) ⇒ Object



23
24
25
26
# File 'lib/spree/core/calculated_adjustments.rb', line 23

def calculator_type=(calculator_type)
  clazz = calculator_type.constantize if calculator_type
  self.calculator = clazz.new if clazz and not self.calculator.is_a? clazz
end

#compute_amount(calculable) ⇒ Object

Calculate the amount to be used when creating an adjustment



48
49
50
# File 'lib/spree/core/calculated_adjustments.rb', line 48

def compute_amount(calculable)
  self.calculator.compute(calculable)
end

#create_adjustment(label, target, calculable, mandatory = false) ⇒ Object

Creates a new adjustment for the target object (which is any class that has_many :adjustments) and sets amount based on the calculator as applied to the calculable argument (Order, LineItems[], Shipment, etc.) By default the adjustment will not be considered mandatory



31
32
33
34
35
36
37
38
39
# File 'lib/spree/core/calculated_adjustments.rb', line 31

def create_adjustment(label, target, calculable, mandatory=false)
  amount = compute_amount(calculable)
  return if amount == 0 && !mandatory
  target.adjustments.create(:amount => amount,
                            :source => calculable,
                            :originator => self,
                            :label => label,
                            :mandatory => mandatory)
end

#update_adjustment(adjustment, calculable) ⇒ Object

Updates the amount of the adjustment using our Calculator and calling the compute method with the calculable referenced passed to the method.



43
44
45
# File 'lib/spree/core/calculated_adjustments.rb', line 43

def update_adjustment(adjustment, calculable)
  adjustment.update_attribute_without_callbacks(:amount, compute_amount(calculable))
end