Skip to content

Commit

Permalink
Spree 2.4 audit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis I committed Jun 28, 2015
1 parent 324dec8 commit e461024
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
22 changes: 21 additions & 1 deletion app/models/spree/line_item_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
Spree::LineItem.class_eval do
after_save :update_core_charges
before_destroy :destroy_core_charges

def calculate_core_charge
return unless product.core_amount
self.quantity * product.core_amount
end
end

def compute_amount o
calculate_core_charge
end

def core_charge
order.adjustments.find{|adj| adj.source_id == id}
end

def update_core_charges
core_charge and core_charge.update!
end

def destroy_core_charges
core_charge and core_charge.destroy
end

end
15 changes: 6 additions & 9 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

# Called each time an order is updated
def create_core_charges
to_keep, to_destroy = adjustments.core.partition{|x| x.originator and x.originator.eligible?}
to_keep, to_destroy = adjustments.core.partition{|adj| adj.eligible?}
to_destroy.each(&:delete)
core_variant_ids = to_keep.map{|x| x.originator.variant_id }
core_variant_ids = to_keep.map{|a| a.source.variant_id }

Spree::Adjustment.skip_callback :save, :after, :update_adjustable_adjustment_total
Spree::Adjustment.skip_callback :create, :after, :update_adjustable_adjustment_total

line_items.each do |li|
next unless li.product.core_amount # The product has no core charge associated with it
Expand All @@ -15,21 +15,18 @@ def create_core_charges
new_core_charge = Spree::Adjustment.new
new_core_charge.label = "#{Spree.t(:core_charge)} [#{li.variant.sku}]"
new_core_charge.source = li
new_core_charge.order = self
new_core_charge.adjustable = self
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_adjustment_total
Spree::Adjustment.set_callback :create, :after, :update_adjustable_adjustment_total

update_totals

update_columns({
:adjustment_total => adjustment_total,
:total => total
})
persist_totals
end

end

0 comments on commit e461024

Please sign in to comment.