Module: Spree::Core::CalculatedAdjustments
- Defined in:
- lib/spree/core/calculated_adjustments.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
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.
Class Method Details
.included(receiver) ⇒ Object
50 51 52 |
# File 'lib/spree/core/calculated_adjustments.rb', line 50 def self.included(receiver) receiver.extend ClassMethods end |
Instance Method Details
#calculator_type ⇒ Object
17 18 19 |
# File 'lib/spree/core/calculated_adjustments.rb', line 17 def calculator_type calculator.class.to_s if calculator end |
#calculator_type=(calculator_type) ⇒ Object
21 22 23 24 |
# File 'lib/spree/core/calculated_adjustments.rb', line 21 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
46 47 48 |
# File 'lib/spree/core/calculated_adjustments.rb', line 46 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
29 30 31 32 33 34 35 36 37 |
# File 'lib/spree/core/calculated_adjustments.rb', line 29 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}, :without_protection => true) 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.
41 42 43 |
# File 'lib/spree/core/calculated_adjustments.rb', line 41 def update_adjustment(adjustment, calculable) adjustment.update_attribute_without_callbacks(:amount, compute_amount(calculable)) end |