Skip to content

Commit

Permalink
Merge pull request #1546 from gevann/use-actionmodel-naming-for-calcu…
Browse files Browse the repository at this point in the history
…lators

Use actionmodel naming for calculators
  • Loading branch information
jhawthorn authored Oct 28, 2016
2 parents 759e10c + aa1bfdf commit b75eb29
Show file tree
Hide file tree
Showing 33 changed files with 117 additions and 67 deletions.
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/flat_percent_item_total.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module Spree
class Calculator::FlatPercentItemTotal < Calculator
preference :flat_percent, :decimal, default: 0

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

def compute(object)
computed_amount = (object.amount * preferred_flat_percent / 100).round(2)

Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/flat_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ class Calculator::FlatRate < Calculator
preference :amount, :decimal, default: 0
preference :currency, :string, default: ->{ Spree::Config[:currency] }

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

def compute(object = nil)
if object && preferred_currency.casecmp(object.currency).zero?
preferred_amount
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/flexi_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ class Calculator::FlexiRate < Calculator
preference :max_items, :integer, default: 0
preference :currency, :string, default: ->{ Spree::Config[:currency] }

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

def self.available?(_object)
true
end
Expand Down
3 changes: 0 additions & 3 deletions core/app/models/spree/calculator/free_shipping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ module Spree
# The only case where it was used was for Free Shipping Promotions. There is
# now a Promotion Action which deals with these types of promotions instead.
class Calculator::FreeShipping < Calculator
def self.description
Spree.t(:free_shipping)
end

def compute(object)
if object.is_a?(Array)
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/percent_on_line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ class Calculator
class PercentOnLineItem < Calculator
preference :percent, :decimal, default: 0

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

def compute(object)
(object.amount * preferred_percent) / 100
end
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/percent_per_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ module Spree
class Calculator::PercentPerItem < Calculator
preference :percent, :decimal, default: 0

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

def compute(object = nil)
return 0 if object.nil?
object.line_items.map { |line_item|
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/price_sack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ class Calculator::PriceSack < Calculator
preference :discount_amount, :decimal, default: 0
preference :currency, :string, default: ->{ Spree::Config[:currency] }

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

# as object we always get line items, as calculable we have Coupon, ShippingMethod
def compute(object)
if object.is_a?(Array)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
module Spree
module Calculator::Returns
class DefaultRefundAmount < ReturnsCalculator
def self.description
Spree.t(:default_refund_amount)
end

def compute(return_item)
return 0.0.to_d if return_item.part_of_exchange?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ module Calculator::Shipping
class FlatPercentItemTotal < ShippingCalculator
preference :flat_percent, :decimal, default: 0

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

def compute_package(package)
compute_from_price(total(package.contents))
end
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/shipping/flat_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class FlatRate < ShippingCalculator
preference :amount, :decimal, default: 0
preference :currency, :string, default: ->{ Spree::Config[:currency] }

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

def compute_package(_package)
preferred_amount
end
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/shipping/flexi_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ class FlexiRate < ShippingCalculator
preference :max_items, :integer, default: 0
preference :currency, :string, default: ->{ Spree::Config[:currency] }

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

def compute_package(package)
compute_from_quantity(package.contents.sum(&:quantity))
end
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/shipping/per_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class PerItem < ShippingCalculator
preference :amount, :decimal, default: 0
preference :currency, :string, default: ->{ Spree::Config[:currency] }

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

def compute_package(package)
compute_from_quantity(package.contents.sum(&:quantity))
end
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/shipping/price_sack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ class PriceSack < ShippingCalculator
preference :discount_amount, :decimal, default: 0
preference :currency, :string, default: ->{ Spree::Config[:currency] }

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

def compute_package(package)
compute_from_price(total(package.contents))
end
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/tiered_flat_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ class Calculator::TieredFlatRate < Calculator

validate :preferred_tiers_content

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

def compute(object)
_base, amount = preferred_tiers.sort.reverse.detect{ |b, _| object.amount >= b }
amount || preferred_base_amount
Expand Down
4 changes: 0 additions & 4 deletions core/app/models/spree/calculator/tiered_percent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ class Calculator::TieredPercent < Calculator
}
validate :preferred_tiers_content

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

def compute(object)
order = object.is_a?(Order) ? object : object.order
_base, percent = preferred_tiers.sort.reverse.detect{ |b, _| order.item_total >= b }
Expand Down
45 changes: 45 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,51 @@ en:
spree/calculator/default_tax:
one: Default Tax
other: Default Tax
spree/calculator/flat_percent_item_total:
one: Flat Percent
other: Flat Percent
spree/calculator/flat_rate:
one: Flat Rate
other: Flat Rate
spree/calculator/flexi_rate:
one: Flexible Rate
other: Flexible Rate
spree/calculator/free_shipping:
one: Free Shipping
other: Free Shipping
spree/calculator/percent_on_line_item:
one: Percent Per Item
other: Percent Per Item
spree/calculator/percent_per_item:
one: Percent Per Item
other: Percent Per Item
spree/calculator/price_sack:
one: Price Sack
other: Price Sack
spree/calculator/tiered_percent:
one: Tiered Percent
other: Tiered Percent
spree/calculator/tiered_flat_rate:
one: Tiered Flat Rate
other: Tiered Flat Rate
spree/calculator/returns/default_refund_amount:
one: Default Refund Amount
other: Default Refund Amount
spree/calculator/shipping/flat_percent_item_total:
one: Flat Percent
other: Flat Percent
spree/calculator/shipping/flat_rate:
one: Flat rate
other: Flat rate
spree/calculator/shipping/flexi_rate:
one: Flexible Rate per package item
other: Flexible Rate per package item
spree/calculator/shipping/per_item:
one: Flat rate per package item
other: Flat rate per package item
spree/calculator/shipping/price_sack:
one: Price sack
other: Price sack
spree/country:
one: Country
other: Countries
Expand Down
6 changes: 2 additions & 4 deletions core/spec/models/spree/calculator/default_tax_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

describe Spree::Calculator::DefaultTax, type: :model do
let(:address) { create(:address) }
Expand All @@ -9,10 +10,7 @@
let(:included_in_price) { false }
subject(:calculator) { Spree::Calculator::DefaultTax.new(calculable: rate ) }

describe ".description" do
subject { described_class.description }
it { is_expected.to eq("Default Tax") }
end
it_behaves_like 'a calculator with a description'

context "#compute" do
context "when given an order" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

describe Spree::Calculator::FlatPercentItemTotal, type: :model do
let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new }
let(:line_item) { mock_model Spree::LineItem }

it_behaves_like 'a calculator with a description'

before { allow(calculator).to receive_messages preferred_flat_percent: 10 }

context "compute" do
Expand Down
3 changes: 3 additions & 0 deletions core/spec/models/spree/calculator/flat_rate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

describe Spree::Calculator::FlatRate, type: :model do
let(:calculator) { Spree::Calculator::FlatRate.new }

it_behaves_like 'a calculator with a description'

let(:order) do
mock_model(
Spree::Order, quantity: 10, currency: "USD"
Expand Down
3 changes: 3 additions & 0 deletions core/spec/models/spree/calculator/flexi_rate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

describe Spree::Calculator::FlexiRate, type: :model do
let(:calculator) { Spree::Calculator::FlexiRate.new }

it_behaves_like 'a calculator with a description'

let(:order) do
mock_model(
Spree::Order, quantity: 10
Expand Down
6 changes: 6 additions & 0 deletions core/spec/models/spree/calculator/free_shipping_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

describe Spree::Calculator::FreeShipping, type: :model do
it_behaves_like 'a calculator with a description'
end
13 changes: 9 additions & 4 deletions core/spec/models/spree/calculator/percent_on_line_item_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

module Spree
class Calculator
describe PercentOnLineItem, type: :model do
let(:line_item) { double("LineItem", amount: 100) }
context "compute" do
let(:line_item) { double("LineItem", amount: 100) }

before { subject.preferred_percent = 15 }
before { subject.preferred_percent = 15 }

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

it_behaves_like 'a calculator with a description'
end
end
end
10 changes: 10 additions & 0 deletions core/spec/models/spree/calculator/percent_per_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

module Spree
class Calculator
describe PercentPerItem, type: :model do
it_behaves_like 'a calculator with a description'
end
end
end
3 changes: 3 additions & 0 deletions core/spec/models/spree/calculator/price_sack_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

describe Spree::Calculator::PriceSack, type: :model do
let(:calculator) do
Expand All @@ -9,6 +10,8 @@
calculator
end

it_behaves_like 'a calculator with a description'

let(:order) { stub_model(Spree::Order) }
let(:shipment) { stub_model(Spree::Shipment, amount: 10) }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

describe Spree::Calculator::Returns::DefaultRefundAmount, type: :model do
let(:line_item_quantity) { 2 }
Expand All @@ -9,6 +10,8 @@
let(:calculator) { Spree::Calculator::Returns::DefaultRefundAmount.new }
let(:order) { line_item.order }

it_behaves_like 'a calculator with a description'

subject { calculator.compute(return_item) }

context "not an exchange" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

module Spree
module Calculator::Shipping
describe FlatPercentItemTotal, type: :model do
let(:variant1) { build(:variant, price: 10.11) }
let(:variant2) { build(:variant, price: 20.2222) }

it_behaves_like 'a calculator with a description'

let(:line_item1) { build(:line_item, variant: variant1) }
let(:line_item2) { build(:line_item, variant: variant2) }

Expand Down
3 changes: 3 additions & 0 deletions core/spec/models/spree/calculator/shipping/flat_rate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

module Spree
module Calculator::Shipping
describe FlatRate, type: :model do
subject { Calculator::Shipping::FlatRate.new(preferred_amount: 4.00) }

it_behaves_like 'a calculator with a description'

it 'always returns the same rate' do
expect(subject.compute(build(:stock_package_fulfilled))).to eql 4.00
end
Expand Down
3 changes: 3 additions & 0 deletions core/spec/models/spree/calculator/shipping/flexi_rate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require 'spec_helper'
require 'shared_examples/calculator_shared_examples'

module Spree
module Calculator::Shipping
describe FlexiRate, type: :model do
let(:variant1) { build(:variant, price: 10) }
let(:variant2) { build(:variant, price: 20) }

it_behaves_like 'a calculator with a description'

let(:package) do
build(:stock_package, variants_contents: { variant1 => 4, variant2 => 6 })
end
Expand Down
Loading

0 comments on commit b75eb29

Please sign in to comment.