Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring to eliminate callback loops

  • Loading branch information...
commit aba3def7c5ada936bcb28b83e115e25c0731e4f1 1 parent 5ab104d
Denis 'jumph4x authored romul committed
Showing with 28 additions and 4 deletions.
  1. +10 −1 app/models/core_charge.rb
  2. +18 −3 lib/spree_core_charges.rb
View
11 app/models/core_charge.rb
@@ -4,6 +4,9 @@ class CoreCharge < ::Adjustment
before_validation :set_amount
+ after_save :update_order
+ after_destroy :update_order
+
# We check if core charges even apply to this order
# For this we see if any associated products have their core amounts set
def applicable?
@@ -17,7 +20,13 @@ def update!
# Calculates core charges by summing relevant ones
def set_amount
- self.amount = source.variant.product.core_amount * source.quantity
+ self.amount ||= source.variant.product.core_amount * source.quantity
end
+private
+
+ def update_order
+ order.update!
+ end
+
end
View
21 lib/spree_core_charges.rb
@@ -40,19 +40,34 @@ def calculate_core_charge
private
def create_core_charges
+ CoreCharge.skip_callback :save, :after, :update_order
+
line_items(true).collect{|item| item if item.variant.product.core_amount }.compact.each do |item|
- adjustments << Adjustment.create({
+ adjustments << CoreCharge.create({
:label => I18n.t(:core_charge) + " [#{item.variant.sku || item.variant.name}]",
:source => item,
:order => self,
:originator => item,
:amount => item.calculate_core_charge
- }) unless core_charges.find(:first, :conditions => {:source_id => item.id})
+ }) unless core_charges.find(:first, :conditions => {:source_id => item.id, :originator_id => item.id})
end
+
+ CoreCharge.set_callback :save, :after, :update_order
+
+ self.update!
end
end
-
+
+ methodz = CoreCharge._save_callbacks.select{|c| c.raw_filter.class == Symbol}
+ CoreCharge.reset_callbacks :save
+
+ methodz.each do |methud|
+ CoreCharge.set_callback :save, methud.kind, methud.raw_filter
+ end
+
+
+
end
config.autoload_paths += %W(#{config.root}/lib)
Please sign in to comment.
Something went wrong with that request. Please try again.