diff --git a/core/app/models/spree/calculator/flat_percent_item_total.rb b/core/app/models/spree/calculator/flat_percent_item_total.rb index 3c58fe9ee4a..5b0173ef69f 100644 --- a/core/app/models/spree/calculator/flat_percent_item_total.rb +++ b/core/app/models/spree/calculator/flat_percent_item_total.rb @@ -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) diff --git a/core/app/models/spree/calculator/flat_rate.rb b/core/app/models/spree/calculator/flat_rate.rb index 503112d400c..992af63c7f8 100644 --- a/core/app/models/spree/calculator/flat_rate.rb +++ b/core/app/models/spree/calculator/flat_rate.rb @@ -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 diff --git a/core/app/models/spree/calculator/flexi_rate.rb b/core/app/models/spree/calculator/flexi_rate.rb index 79c04582b69..b4f19a6dae3 100644 --- a/core/app/models/spree/calculator/flexi_rate.rb +++ b/core/app/models/spree/calculator/flexi_rate.rb @@ -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 diff --git a/core/app/models/spree/calculator/free_shipping.rb b/core/app/models/spree/calculator/free_shipping.rb index 5a70acba8a1..928b4b69590 100644 --- a/core/app/models/spree/calculator/free_shipping.rb +++ b/core/app/models/spree/calculator/free_shipping.rb @@ -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) diff --git a/core/app/models/spree/calculator/percent_on_line_item.rb b/core/app/models/spree/calculator/percent_on_line_item.rb index e4f0076b744..bedb5a14392 100644 --- a/core/app/models/spree/calculator/percent_on_line_item.rb +++ b/core/app/models/spree/calculator/percent_on_line_item.rb @@ -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 diff --git a/core/app/models/spree/calculator/percent_per_item.rb b/core/app/models/spree/calculator/percent_per_item.rb index e33d5de9056..b44216f36de 100644 --- a/core/app/models/spree/calculator/percent_per_item.rb +++ b/core/app/models/spree/calculator/percent_per_item.rb @@ -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| diff --git a/core/app/models/spree/calculator/price_sack.rb b/core/app/models/spree/calculator/price_sack.rb index dc0206dd408..39b0170748d 100644 --- a/core/app/models/spree/calculator/price_sack.rb +++ b/core/app/models/spree/calculator/price_sack.rb @@ -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) diff --git a/core/app/models/spree/calculator/returns/default_refund_amount.rb b/core/app/models/spree/calculator/returns/default_refund_amount.rb index 013ff4c9e10..f3dd8680c52 100644 --- a/core/app/models/spree/calculator/returns/default_refund_amount.rb +++ b/core/app/models/spree/calculator/returns/default_refund_amount.rb @@ -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? diff --git a/core/app/models/spree/calculator/shipping/flat_percent_item_total.rb b/core/app/models/spree/calculator/shipping/flat_percent_item_total.rb index aef369d19f4..1af0e665017 100644 --- a/core/app/models/spree/calculator/shipping/flat_percent_item_total.rb +++ b/core/app/models/spree/calculator/shipping/flat_percent_item_total.rb @@ -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 diff --git a/core/app/models/spree/calculator/shipping/flat_rate.rb b/core/app/models/spree/calculator/shipping/flat_rate.rb index 838b8513c70..1e76dd24675 100644 --- a/core/app/models/spree/calculator/shipping/flat_rate.rb +++ b/core/app/models/spree/calculator/shipping/flat_rate.rb @@ -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 diff --git a/core/app/models/spree/calculator/shipping/flexi_rate.rb b/core/app/models/spree/calculator/shipping/flexi_rate.rb index 21b208a4293..1f20a57385a 100644 --- a/core/app/models/spree/calculator/shipping/flexi_rate.rb +++ b/core/app/models/spree/calculator/shipping/flexi_rate.rb @@ -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 diff --git a/core/app/models/spree/calculator/shipping/per_item.rb b/core/app/models/spree/calculator/shipping/per_item.rb index f50b22065bb..3ca2e1f13b3 100644 --- a/core/app/models/spree/calculator/shipping/per_item.rb +++ b/core/app/models/spree/calculator/shipping/per_item.rb @@ -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 diff --git a/core/app/models/spree/calculator/shipping/price_sack.rb b/core/app/models/spree/calculator/shipping/price_sack.rb index 59fa87dd381..e9c5d77ad19 100644 --- a/core/app/models/spree/calculator/shipping/price_sack.rb +++ b/core/app/models/spree/calculator/shipping/price_sack.rb @@ -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 diff --git a/core/app/models/spree/calculator/tiered_flat_rate.rb b/core/app/models/spree/calculator/tiered_flat_rate.rb index 10f206f30dc..1f9b0e30a67 100644 --- a/core/app/models/spree/calculator/tiered_flat_rate.rb +++ b/core/app/models/spree/calculator/tiered_flat_rate.rb @@ -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 diff --git a/core/app/models/spree/calculator/tiered_percent.rb b/core/app/models/spree/calculator/tiered_percent.rb index ae159ccc036..981f7be3f60 100644 --- a/core/app/models/spree/calculator/tiered_percent.rb +++ b/core/app/models/spree/calculator/tiered_percent.rb @@ -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 } diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml index 8836a653d38..447980dd001 100644 --- a/core/config/locales/en.yml +++ b/core/config/locales/en.yml @@ -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 diff --git a/core/spec/models/spree/calculator/default_tax_spec.rb b/core/spec/models/spree/calculator/default_tax_spec.rb index f314d9ed90a..9d509efaff4 100644 --- a/core/spec/models/spree/calculator/default_tax_spec.rb +++ b/core/spec/models/spree/calculator/default_tax_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'shared_examples/calculator_shared_examples' describe Spree::Calculator::DefaultTax, type: :model do let(:address) { create(:address) } @@ -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 diff --git a/core/spec/models/spree/calculator/flat_percent_item_total_spec.rb b/core/spec/models/spree/calculator/flat_percent_item_total_spec.rb index 65e21f48270..8d572db14e1 100644 --- a/core/spec/models/spree/calculator/flat_percent_item_total_spec.rb +++ b/core/spec/models/spree/calculator/flat_percent_item_total_spec.rb @@ -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 diff --git a/core/spec/models/spree/calculator/flat_rate_spec.rb b/core/spec/models/spree/calculator/flat_rate_spec.rb index 40bb97e8ef8..0f4bfce8d2d 100644 --- a/core/spec/models/spree/calculator/flat_rate_spec.rb +++ b/core/spec/models/spree/calculator/flat_rate_spec.rb @@ -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" diff --git a/core/spec/models/spree/calculator/flexi_rate_spec.rb b/core/spec/models/spree/calculator/flexi_rate_spec.rb index 2587e039b8f..3b38d26b664 100644 --- a/core/spec/models/spree/calculator/flexi_rate_spec.rb +++ b/core/spec/models/spree/calculator/flexi_rate_spec.rb @@ -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 diff --git a/core/spec/models/spree/calculator/free_shipping_spec.rb b/core/spec/models/spree/calculator/free_shipping_spec.rb new file mode 100644 index 00000000000..f84c705f6e0 --- /dev/null +++ b/core/spec/models/spree/calculator/free_shipping_spec.rb @@ -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 diff --git a/core/spec/models/spree/calculator/percent_on_line_item_spec.rb b/core/spec/models/spree/calculator/percent_on_line_item_spec.rb index 0a47c2c75cf..d01e739801f 100644 --- a/core/spec/models/spree/calculator/percent_on_line_item_spec.rb +++ b/core/spec/models/spree/calculator/percent_on_line_item_spec.rb @@ -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 diff --git a/core/spec/models/spree/calculator/percent_per_item_spec.rb b/core/spec/models/spree/calculator/percent_per_item_spec.rb new file mode 100644 index 00000000000..0ef781f7169 --- /dev/null +++ b/core/spec/models/spree/calculator/percent_per_item_spec.rb @@ -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 diff --git a/core/spec/models/spree/calculator/price_sack_spec.rb b/core/spec/models/spree/calculator/price_sack_spec.rb index a769959c47d..e12796197f9 100644 --- a/core/spec/models/spree/calculator/price_sack_spec.rb +++ b/core/spec/models/spree/calculator/price_sack_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'shared_examples/calculator_shared_examples' describe Spree::Calculator::PriceSack, type: :model do let(:calculator) do @@ -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) } diff --git a/core/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb b/core/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb index 9431231438d..7397724b187 100644 --- a/core/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +++ b/core/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb @@ -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 } @@ -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 diff --git a/core/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb b/core/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb index 0b14c9ee833..c7dfd1faace 100644 --- a/core/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +++ b/core/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'shared_examples/calculator_shared_examples' module Spree module Calculator::Shipping @@ -6,6 +7,8 @@ module Calculator::Shipping 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) } diff --git a/core/spec/models/spree/calculator/shipping/flat_rate_spec.rb b/core/spec/models/spree/calculator/shipping/flat_rate_spec.rb index 280679564d3..560d62a8ab3 100644 --- a/core/spec/models/spree/calculator/shipping/flat_rate_spec.rb +++ b/core/spec/models/spree/calculator/shipping/flat_rate_spec.rb @@ -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 diff --git a/core/spec/models/spree/calculator/shipping/flexi_rate_spec.rb b/core/spec/models/spree/calculator/shipping/flexi_rate_spec.rb index a65b42ce678..83e2c8588b9 100644 --- a/core/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +++ b/core/spec/models/spree/calculator/shipping/flexi_rate_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'shared_examples/calculator_shared_examples' module Spree module Calculator::Shipping @@ -6,6 +7,8 @@ module Calculator::Shipping 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 diff --git a/core/spec/models/spree/calculator/shipping/per_item_spec.rb b/core/spec/models/spree/calculator/shipping/per_item_spec.rb index 3cf1d751437..efd8e440df9 100644 --- a/core/spec/models/spree/calculator/shipping/per_item_spec.rb +++ b/core/spec/models/spree/calculator/shipping/per_item_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'shared_examples/calculator_shared_examples' module Spree module Calculator::Shipping @@ -6,6 +7,8 @@ module Calculator::Shipping let(:variant1) { build(:variant) } let(:variant2) { build(:variant) } + it_behaves_like 'a calculator with a description' + let(:package) do build(:stock_package, variants_contents: { variant1 => 5, variant2 => 3 }) end diff --git a/core/spec/models/spree/calculator/shipping/price_sack_spec.rb b/core/spec/models/spree/calculator/shipping/price_sack_spec.rb index a769959c47d..8611133789a 100644 --- a/core/spec/models/spree/calculator/shipping/price_sack_spec.rb +++ b/core/spec/models/spree/calculator/shipping/price_sack_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' +require 'shared_examples/calculator_shared_examples' -describe Spree::Calculator::PriceSack, type: :model do +describe Spree::Calculator::Shipping::PriceSack, type: :model do let(:calculator) do calculator = Spree::Calculator::PriceSack.new calculator.preferred_minimal_amount = 5 @@ -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) } diff --git a/core/spec/models/spree/calculator/tiered_flat_rate_spec.rb b/core/spec/models/spree/calculator/tiered_flat_rate_spec.rb index e8422e2b50f..fb25a164a51 100644 --- a/core/spec/models/spree/calculator/tiered_flat_rate_spec.rb +++ b/core/spec/models/spree/calculator/tiered_flat_rate_spec.rb @@ -1,8 +1,11 @@ require 'spec_helper' +require 'shared_examples/calculator_shared_examples' describe Spree::Calculator::TieredFlatRate, type: :model do let(:calculator) { Spree::Calculator::TieredFlatRate.new } + it_behaves_like 'a calculator with a description' + describe "#valid?" do subject { calculator.valid? } context "when tiers is a hash" do diff --git a/core/spec/models/spree/calculator/tiered_percent_spec.rb b/core/spec/models/spree/calculator/tiered_percent_spec.rb index 917b9401043..10efe34f59f 100644 --- a/core/spec/models/spree/calculator/tiered_percent_spec.rb +++ b/core/spec/models/spree/calculator/tiered_percent_spec.rb @@ -1,8 +1,11 @@ require 'spec_helper' +require 'shared_examples/calculator_shared_examples' describe Spree::Calculator::TieredPercent, type: :model do let(:calculator) { Spree::Calculator::TieredPercent.new } + it_behaves_like 'a calculator with a description' + describe "#valid?" do subject { calculator.valid? } context "when base percent is less than zero" do diff --git a/core/spec/shared_examples/calculator_shared_examples.rb b/core/spec/shared_examples/calculator_shared_examples.rb new file mode 100644 index 00000000000..ffc40f1316c --- /dev/null +++ b/core/spec/shared_examples/calculator_shared_examples.rb @@ -0,0 +1,8 @@ +shared_examples_for 'a calculator with a description' do + describe ".description" do + subject { described_class.description } + it "has a description" do + expect(subject.size).to be > 0 + end + end +end