Skip to content

Commit

Permalink
Working untested version for 1-1-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
jumph4x committed Sep 25, 2012
1 parent 0e4c81f commit cdccf0b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 46 deletions.
16 changes: 8 additions & 8 deletions app/models/spree/line_item_decorator.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
48 changes: 29 additions & 19 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 1 addition & 19 deletions lib/spree_core_charges.rb
Original file line number Diff line number Diff line change
@@ -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'
19 changes: 19 additions & 0 deletions lib/spree_core_charges/engine.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file added spec/factories.rb
Empty file.

0 comments on commit cdccf0b

Please sign in to comment.