Skip to content

Commit

Permalink
Rewritten update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jumph4x authored and romul committed Apr 8, 2012
1 parent aba3def commit 347ea25
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/models/core_charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 44 additions & 30 deletions lib/spree_core_charges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def update_adjustment(adjustment, source)
else
0
end

adjustment.save
end

def calculate_core_charge
Expand All @@ -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

Expand Down

0 comments on commit 347ea25

Please sign in to comment.