Skip to content
Browse files

Another attempt at eliminating AR callback loop

  • Loading branch information...
1 parent 347ea25 commit 66f813211d4a4ac1cfa48c9718b2666a580c2459 @jumph4x committed with romul Dec 21, 2011
Showing with 11 additions and 22 deletions.
  1. +2 −7 app/models/core_charge.rb
  2. +9 −15 lib/spree_core_charges.rb
View
9 app/models/core_charge.rb
@@ -4,9 +4,6 @@ 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?
@@ -22,11 +19,9 @@ def update!
def set_amount
self.amount = (source.product.core_amount * source.quantity)
end
-
-private
-
+
def update_order
- order.update!
+
end
end
View
24 lib/spree_core_charges.rb
@@ -19,7 +19,9 @@ def update_adjustment(adjustment, source)
0
end
+ Adjustment.skip_callback :save, :after, :update_order
adjustment.save
+ Adjustment.set_callback :save, :after, :update_order
end
def calculate_core_charge
@@ -31,37 +33,37 @@ def calculate_core_charge
private
def create_core_charges
+ return true if order.core_charges.detect{|cc| cc.source_id == id}
+
order.core_charges << CoreCharge.create({
:label => "#{I18n.t(:core_charge)} [#{variant.sku}]",
:source => self,
:order => order,
:originator => self,
:amount => calculate_core_charge
- }) unless order.core_charges.detect{|cc| cc.source_id == id}
+ })
end
def destroy_core_charges
order.core_charges.select{|cc| cc.source_id == id}.map(&:destroy)
-
- order.update!
end
def update_core_charges
- return order.update! unless self.product.core_amount
if self.destroyed?
destroy_core_charges
elsif
create_core_charges
end
+
end
def update_order
+ update_core_charges if product.core_amount
+
# update the order totals, etc.
- update_core_charges
- # implied in the above action
- #order.update!
+ order.update!
end
end
@@ -74,14 +76,6 @@ def update_order
:conditions => "source_type='LineItem'"
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)

0 comments on commit 66f8132

Please sign in to comment.
Something went wrong with that request. Please try again.