Module: Spree::CalculatedAdjustments::InstanceMethods
- Defined in:
- lib/spree/calculated_adjustments.rb
Instance Method Summary collapse
- #calculator_type ⇒ Object
- #calculator_type=(calculator_type) ⇒ Object
-
#compute_amount(calculable) ⇒ Object
Calculate the amount to be used when creating an adjustment.
-
#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.
-
#update_adjustment(adjustment, calculable) ⇒ Object
Updates the amount of the adjustment using our Calculator and calling the
compute
method with thecalculable
referenced passed to the method.
Instance Method Details
#calculator_type ⇒ Object
38 39 40 |
# File 'lib/spree/calculated_adjustments.rb', line 38 def calculator_type calculator.class.to_s if calculator end |
#calculator_type=(calculator_type) ⇒ Object
42 43 44 45 |
# File 'lib/spree/calculated_adjustments.rb', line 42 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
64 65 66 |
# File 'lib/spree/calculated_adjustments.rb', line 64 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
50 51 52 53 54 55 |
# File 'lib/spree/calculated_adjustments.rb', line 50 def create_adjustment(label, target, calculable, mandatory=false) a = target.adjustments.create(:amount => compute_amount(calculable), :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.
59 60 61 |
# File 'lib/spree/calculated_adjustments.rb', line 59 def update_adjustment(adjustment, calculable) adjustment.update_attribute_without_callbacks(:amount, compute_amount(calculable)) end |