Permalink
Browse files

Finished main part of upgrade to Spree 1.0 (changes in adjustments lo…

…gic)
  • Loading branch information...
romul committed Apr 10, 2012
1 parent 06cbaa2 commit 215e7899ccfcf917737a862b35dab07b677190be
Showing with 38 additions and 79 deletions.
  1. +13 −46 app/models/line_item_decorator.rb
  2. +23 −5 app/models/order_decorator.rb
  3. +0 −27 app/models/spree/core_charge.rb
  4. +2 −1 lib/spree_core_charges.rb
@@ -1,57 +1,24 @@
Spree::LineItem.class_eval do
-
def update_adjustment(adjustment, source)
- adjustment.amount = if adjustment.order(true).line_items.include? source
+ current_amount = adjustment.amount
+ adjustment.amount = if adjustment.adjustable(true).line_items.include? adjustment.source
calculate_core_charge
else
0
end
-
- Spree::Adjustment.skip_callback :save, :after, :update_order
- adjustment.save
- Spree::Adjustment.set_callback :save, :after, :update_order
- end
-
- def calculate_core_charge
- return unless product.core_amount
-
- self.quantity * product.core_amount
- end
-
-private
-
- def create_core_charges
- return true if order.core_charges.detect{|cc| cc.source_id == id}
-
- order.core_charges << Spree::CoreCharge.create({
- :label => "#{I18n.t(:core_charge)} [#{variant.sku}]",
- :source => self,
- :order => order,
- :originator => self,
- :amount => calculate_core_charge
- })
- end
-
- def destroy_core_charges
- order.core_charges.select{|cc| cc.source_id == id}.map(&:destroy)
- end
-
- def update_core_charges
-
- if self.destroyed?
- destroy_core_charges
- elsif
- create_core_charges
+ if current_amount != adjustment.amount
+ Spree::Adjustment.skip_callback :save, :after, :update_adjustable
+ adjustment.save
+ Spree::Adjustment.set_callback :save, :after, :update_adjustable
end
-
+ end
+
+ def eligible?(source = nil)
+ !!self.product.core_amount
end
- def update_order
- update_core_charges if product.core_amount
-
- # update the order totals, etc.
-
- order.update!
+ def calculate_core_charge
+ return unless product.core_amount
+ - self.quantity * product.core_amount
end
-
end
@@ -1,8 +1,26 @@
Spree::Order.class_eval do
- has_many :core_charges,
- :dependent => :destroy,
- #:class_name => 'Spree::Adjustment',
- :conditions => "source_type='Spree::LineItem'"
-
+ def core_charges
+ adjustments.where(:source_type => 'Spree::LineItem')
+ end
+
+ 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)
+ end
+ end
+
end
@@ -1,27 +0,0 @@
-class Spree::CoreCharge < Spree::Adjustment
- belongs_to :order
- scope :with_order, :conditions => "order_id IS NOT NULL"
-
- before_validation :set_amount
-
- # 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?
- order.line_items.include? source
- end
-
- def update!
- set_amount
- save
- end
-
- # Calculates core charges by summing relevant ones
- def set_amount
- self.amount = (source.product.core_amount * source.quantity)
- end
-
- def update_order
-
- end
-
-end
@@ -7,9 +7,10 @@ class Engine < Rails::Engine
engine_name 'spree_core_charges'
def self.activate
- Dir.glob(File.join(File.dirname(__FILE__), "../../../app/**/*_decorator*.rb")) do |c|
+ 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)

0 comments on commit 215e789

Please sign in to comment.