Skip to content

Commit

Permalink
Works as expected now.
Browse files Browse the repository at this point in the history
Inheriting from Adjustment and using 0.00 values to determine applicability
  • Loading branch information
Denis 'jumph4x committed May 18, 2011
1 parent eea8924 commit c43fe81
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions lib/spree_core_charges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,48 @@ module SpreeCoreCharges
class Engine < Rails::Engine

def self.activate
# Dir.glob(File.join(File.dirname(__FILE__), "../app/models/*.rb")) do |c|
# Rails.env.production? ? require(c) : load(c)
# end

Adjustment.class_eval do
scope :core, lambda { where('label like ?',"#{I18n.t(:core_charge)}%") }
end

LineItem.class_eval do

def update_adjustment(adjustment, source)
adjustment.amount = if adjustment.order.line_items.include? source
calculate_core_charge
else
0
end
end

def calculate_core_charge
return unless product.core_amount

self.quantity * product.core_amount
end

end

Order.register_update_hook('create_core_charges')

Order.class_eval do
#has_many :core_charges
before_save :update_core_charges

has_many :core_charges,
:dependent => :destroy,
:class_name => 'Adjustment',
:conditions => "source_type='LineItem'"

private

def update_core_charges
line_items.collect{|item| item if item.variant.product.core_amount }.compact.each do |item|
adjustments << CoreCharge.create({
def create_core_charges
line_items(true).collect{|item| item if item.variant.product.core_amount }.compact.each do |item|
adjustments << Adjustment.create({
:label => I18n.t(:core_charge) + " [#{item.variant.sku || item.variant.name}]",
:source => item,
:order => self
}) unless adjustments.find(:first, :conditions => {:source_id => item.id})
:order => self,
:originator => item,
:amount => item.calculate_core_charge
}) unless core_charges.find(:first, :conditions => {:source_id => item.id})
end
end

Expand Down

0 comments on commit c43fe81

Please sign in to comment.