mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Refactor voucher: 3 percentage rate voucher
This commit is contained in:
18
app/models/vouchers/percentage_rate.rb
Normal file
18
app/models/vouchers/percentage_rate.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
module Vouchers
|
||||
class PercentageRate < Voucher
|
||||
validates :amount,
|
||||
presence: true,
|
||||
numericality: { greater_than: 0, less_than_or_equal_to: 100 }
|
||||
|
||||
def display_value
|
||||
ActionController::Base.helpers.number_to_percentage(amount, precision: 2)
|
||||
end
|
||||
|
||||
def compute_amount(order)
|
||||
percentage = amount / 100
|
||||
-percentage * order.pre_discount_total
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -9,4 +9,8 @@ FactoryBot.define do
|
||||
factory :voucher_flat_rate, parent: :voucher, class: Vouchers::FlatRate do
|
||||
amount { 15 }
|
||||
end
|
||||
|
||||
factory :voucher_percentage_rate, parent: :voucher, class: Vouchers::PercentageRate do
|
||||
amount { rand(1..100) }
|
||||
end
|
||||
end
|
||||
|
||||
29
spec/models/vouchers/percentage_rate_spec.rb
Normal file
29
spec/models/vouchers/percentage_rate_spec.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Vouchers::PercentageRate do
|
||||
let(:enterprise) { build(:enterprise) }
|
||||
|
||||
describe 'validations' do
|
||||
subject { build(:voucher_percentage_rate, code: 'new_code', enterprise: enterprise) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:amount) }
|
||||
it do
|
||||
is_expected.to validate_numericality_of(:amount)
|
||||
.is_greater_than(0)
|
||||
.is_less_than_or_equal_to(100)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#compute_amount' do
|
||||
subject do
|
||||
create(:voucher_percentage_rate, code: 'new_code', enterprise: enterprise, amount: 10)
|
||||
end
|
||||
let(:order) { create(:order_with_totals) }
|
||||
|
||||
it 'returns percentage of the order total' do
|
||||
expect(subject.compute_amount(order)).to eq(-order.pre_discount_total * 0.1)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -160,7 +160,7 @@ describe "As a consumer, I want to see adjustment breakdown" do
|
||||
|
||||
describe "moving between summary to summary via edit cart" do
|
||||
let!(:voucher) do
|
||||
create(:voucher_percentage, code: 'good_code', enterprise: distributor, amount: 20)
|
||||
create(:voucher_percentage_rate, code: 'good_code', enterprise: distributor, amount: 20)
|
||||
end
|
||||
|
||||
it "recalculate the tax component properly" do
|
||||
@@ -171,7 +171,6 @@ describe "As a consumer, I want to see adjustment breakdown" do
|
||||
fill_in "Enter voucher code", with: voucher.code
|
||||
click_button("Apply")
|
||||
|
||||
#pause
|
||||
proceed_to_summary
|
||||
assert_db_voucher_adjustment(-2.00, -0.26)
|
||||
|
||||
@@ -200,7 +199,7 @@ describe "As a consumer, I want to see adjustment breakdown" do
|
||||
proceed_to_payment
|
||||
|
||||
# Check voucher is still there
|
||||
expect(page).to have_content("20.0 % Voucher")
|
||||
expect(page).to have_content("20.00% Voucher")
|
||||
|
||||
# Go to summary
|
||||
proceed_to_summary
|
||||
|
||||
Reference in New Issue
Block a user