Module: Spree::CalculatedAdjustments::InstanceMethods

Defined in:
lib/spree/calculated_adjustments.rb

Instance Method Summary collapse

Instance Method Details

#calculator_typeObject



39
40
41
# File 'lib/spree/calculated_adjustments.rb', line 39

def calculator_type
  calculator.class.to_s if calculator
end

#calculator_type=(calculator_type) ⇒ Object



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

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

#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



51
52
53
54
55
56
57
# File 'lib/spree/calculated_adjustments.rb', line 51

def create_adjustment(label, target, calculable, mandatory=false)
  amount = self.calculator.compute(calculable)
  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.



61
62
63
# File 'lib/spree/calculated_adjustments.rb', line 61

def update_adjustment(adjustment, calculable)
  adjustment.update_attribute_without_callbacks(:amount, self.calculator.compute(calculable))
end