Permalink
Browse files

More updates to in-memory orders handling.

Allows proper volume discount calculation for fake orders that are
not to be persisted.
  • Loading branch information...
1 parent 4ffd7e6 commit 3b515094c186e71458ab42d7dc61a7da52cba7aa Adam Wróbel committed Apr 13, 2011
Showing with 8 additions and 7 deletions.
  1. +0 −1 app/models/line_item_decorator.rb
  2. +8 −6 app/models/order_decorator.rb
@@ -19,7 +19,6 @@ def update_volume_discount updated_order = nil
end
def amount_with_volume_discount
- check_update_volume_discount
amount_without_volume_discount + self.volume_discount
end
alias_method_chain :amount, :volume_discount
@@ -4,7 +4,11 @@ def volume_discount
end
def products_line_items product
- line_items.all.select {|i| product.all_variant_ids.include? i.variant_id}
+ line_items.to_a.select {|i| product.all_variant_ids.include? i.variant_id}
+ end
+
+ def line_items_dirty?
+ @line_items && @line_items.any? {|i| !i.destroyed? && i.changed?}
end
# By default volume price is calculated based only on quantity of the current
@@ -20,9 +24,7 @@ def variants_starting_quantity *variant_ids
def update_totals_with_volume_discount
# we might need to refresh the items
- if @line_items && !@line_items.any? {|i| !i.destroyed? && i.changed?}
- line_items true
- end
+ line_items true unless line_items_dirty?
update_totals_without_volume_discount
end
alias_method_chain :update_totals, :volume_discount
@@ -45,14 +47,14 @@ def force_volume_discount_update
products_to_update.uniq.each {|p| update_product_volume_discount p}
- update_totals
+ update!
end
# This is required for Volume Customers
# It updates item_total when user logs in
before_save :force_volume_discount_update, :if => :volume_user_changed?
def update_product_volume_discount product
- line_items true
+ line_items true unless line_items_dirty?
items = products_line_items(product).sort_by &:variant_id
return if items.blank?

0 comments on commit 3b51509

Please sign in to comment.