Skip to content

Commit

Permalink
Create new calculator from line item discounts
Browse files Browse the repository at this point in the history
Set it as default in CreateItemAdjustment PromotionAction as the
previous FlatPercentItemTotal only works for discounts on orders
  • Loading branch information
huoxito authored and radar committed Sep 27, 2013
1 parent 30739c0 commit 983e5aa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
15 changes: 15 additions & 0 deletions core/app/models/spree/calculator/percent_on_line_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Spree
class Calculator
class PercentOnLineItem < Calculator
preference :percent, :decimal, default: 0

def self.description
Spree.t(:percent_per_item)
end

def compute(object)
((object.price * object.quantity) * preferred_percent) / 100
end
end
end
end
3 changes: 3 additions & 0 deletions core/app/models/spree/calculator/percent_per_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module Spree
# for all matching products in an order. This should not be used as a
# shipping calculator since it would be the same thing as a flat percent
# off the entire order.
#
#
# TODO Should be deprecated now that we have adjustments at the line item level in spree core

class Calculator::PercentPerItem < Calculator
preference :percent, :decimal, default: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def promotion_credit_exists?(adjustable)

def ensure_action_has_calculator
return if self.calculator
self.calculator = Calculator::FlatPercentItemTotal.new
self.calculator = Calculator::PercentOnLineItem.new
end

def deals_with_adjustments
Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Engine < ::Rails::Engine

app.config.spree.calculators.add_class('promotion_actions_create_item_adjustments')
app.config.spree.calculators.promotion_actions_create_item_adjustments = [
Spree::Calculator::FlatRate
Spree::Calculator::PercentOnLineItem
]
end

Expand Down
15 changes: 15 additions & 0 deletions core/spec/models/spree/calculator/percent_on_line_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

module Spree
class Calculator
describe PercentOnLineItem do
let(:line_item) { double("LineItem", price: 10, quantity: 10) }

before { subject.preferred_percent = 15 }

it "computes based on item price and quantity" do
expect(subject.compute(line_item)).to eq 15
end
end
end
end

0 comments on commit 983e5aa

Please sign in to comment.