From 347ea25abfd3b041364c98edb5fe2b1985bca8a7 Mon Sep 17 00:00:00 2001 From: Denis Ivanov Date: Wed, 14 Dec 2011 16:56:58 -0500 Subject: [PATCH] Rewritten update logic --- app/models/core_charge.rb | 4 +-- lib/spree_core_charges.rb | 74 +++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/app/models/core_charge.rb b/app/models/core_charge.rb index 4d7b509..614f02f 100644 --- a/app/models/core_charge.rb +++ b/app/models/core_charge.rb @@ -15,12 +15,12 @@ def applicable? def update! set_amount - save! + save end # Calculates core charges by summing relevant ones def set_amount - self.amount ||= source.variant.product.core_amount * source.quantity + self.amount = (source.product.core_amount * source.quantity) end private diff --git a/lib/spree_core_charges.rb b/lib/spree_core_charges.rb index 643f567..454d3e8 100644 --- a/lib/spree_core_charges.rb +++ b/lib/spree_core_charges.rb @@ -18,6 +18,8 @@ def update_adjustment(adjustment, source) else 0 end + + adjustment.save end def calculate_core_charge @@ -26,47 +28,59 @@ def calculate_core_charge self.quantity * product.core_amount end - end - - Order.register_update_hook('create_core_charges') - - Order.class_eval do - - has_many :core_charges, - :dependent => :destroy, - :class_name => 'Adjustment', - :conditions => "source_type='LineItem'" - private - + def create_core_charges - CoreCharge.skip_callback :save, :after, :update_order + 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 - line_items(true).collect{|item| item if item.variant.product.core_amount }.compact.each do |item| - 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, :originator_id => item.id}) - end + def destroy_core_charges + order.core_charges.select{|cc| cc.source_id == id}.map(&:destroy) - CoreCharge.set_callback :save, :after, :update_order - - self.update! + order.update! end - 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 - methodz = CoreCharge._save_callbacks.select{|c| c.raw_filter.class == Symbol} - CoreCharge.reset_callbacks :save + def update_order + # update the order totals, etc. + update_core_charges + + # implied in the above action + #order.update! + end - methodz.each do |methud| - CoreCharge.set_callback :save, methud.kind, methud.raw_filter end + Order.class_eval do + + has_many :core_charges, + :dependent => :destroy, + #:class_name => 'Adjustment', + :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