diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 98b60bb..365e35b 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -1,16 +1,16 @@ Spree::LineItem.class_eval do - def update_adjustment(adjustment, source) - current_amount = adjustment.amount - adjustment.amount = if adjustment.adjustable(true).line_items.include? adjustment.source + def update_adjustment(adjustment, source) #source being the order + new_amount = if adjustment.originator and source.line_items.include? adjustment.originator calculate_core_charge else 0 end - if current_amount != adjustment.amount - Spree::Adjustment.skip_callback :save, :after, :update_adjustable - adjustment.save - Spree::Adjustment.set_callback :save, :after, :update_adjustable - end + + adjustment.update_attribute_without_callbacks(:amount, new_amount) + + # Spree::Adjustment.skip_callback :save, :after, :update_adjustable + # adjustment.save + # Spree::Adjustment.set_callback :save, :after, :update_adjustable end def eligible?(source = nil) diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 6199f3f..9aa5cc1 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -1,26 +1,36 @@ Spree::Order.class_eval do - def core_charges - adjustments.where(:source_type => 'Spree::LineItem') - end - + # Called each time an orderis updated def create_core_charges - applicable_charges, charges_to_destroy = self.core_charges.partition{|cc| cc.originator && cc.originator.eligible? } - charges_to_destroy.each(&:delete) - line_item_with_core_charge_ids = applicable_charges.map(&:source_id) - - self.line_items.reload.each do |li| - next unless li.product.core_amount - - self.core_charges << Spree::Adjustment.create({ - :label => "#{I18n.t(:core_charge)} [#{li.variant.sku}]", - :source => li, - :adjustable => self, - :originator => li, - :amount => li.calculate_core_charge, - :eligible => true - }) unless line_item_with_core_charge_ids.include?(li.id) + to_keep, to_destroy = adjustments.core.partition{|x| x.originator and x.originator.eligible?} + to_destroy.each(&:delete) + core_variant_ids = to_keep.map{|x| x.originator.variant_id } + + Spree::Adjustment.skip_callback :save, :after, :update_adjustable + + line_items.each do |li| + next unless li.product.core_amount # The product has no core charge associated with it + next if core_variant_ids.include? li.variant_id # The charge for this line item already exists + + new_core_charge = Spree::Adjustment.new + new_core_charge.label = "#{I18n.t(:core_charge)} [#{li.variant.sku}]" + new_core_charge.source = li + new_core_charge.adjustable = self + new_core_charge.originator = li + new_core_charge.amount = li.calculate_core_charge + new_core_charge.eligible = true + + new_core_charge.save end + + Spree::Adjustment.set_callback :save, :after, :update_adjustable + + update_totals + + update_attributes_without_callbacks({ + :adjustment_total => adjustment_total, + :total => total + }) end end diff --git a/lib/spree_core_charges.rb b/lib/spree_core_charges.rb index e8a2d86..65bc97e 100644 --- a/lib/spree_core_charges.rb +++ b/lib/spree_core_charges.rb @@ -1,20 +1,2 @@ require 'spree_core' - -module Spree - module CoreCharges - class Engine < Rails::Engine - isolate_namespace Spree - engine_name 'spree_core_charges' - - def self.activate - Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c| - Rails.configuration.cache_classes ? require(c) : load(c) - end - Order.register_update_hook('create_core_charges') - end - - config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/overrides) - config.to_prepare &method(:activate).to_proc - end - end -end +require 'spree_core_charges/engine' diff --git a/lib/spree_core_charges/engine.rb b/lib/spree_core_charges/engine.rb new file mode 100644 index 0000000..a27407b --- /dev/null +++ b/lib/spree_core_charges/engine.rb @@ -0,0 +1,19 @@ +module Spree + module CoreCharges + class Engine < Rails::Engine + isolate_namespace Spree + engine_name 'spree_core_charges' + + def self.activate + Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c| + Rails.configuration.cache_classes ? require(c) : load(c) + end + + Spree::Order.register_update_hook(:create_core_charges) + end + + config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/overrides) + config.to_prepare &method(:activate).to_proc + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb new file mode 100644 index 0000000..e69de29