Skip to content

Commit

Permalink
Add voucher_type to voucher
Browse files Browse the repository at this point in the history
And update related specs
voucher_type doesn't do anything for now.
  • Loading branch information
rioug committed Jul 17, 2023
1 parent 4faee3e commit cb54397
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 9 deletions.
15 changes: 14 additions & 1 deletion app/models/voucher.rb
Expand Up @@ -11,7 +11,20 @@ class Voucher < ApplicationRecord
dependent: :nullify

validates :code, presence: true, uniqueness: { scope: :enterprise_id }
validates :amount, presence: true, numericality: { greater_than: 0 }
validates :amount,
presence: true,
numericality: { greater_than: 0 },
if: ->(v) { v.voucher_type == FLAT_RATE }
validates :amount,
presence: true,
numericality: { greater_than: 0, less_than_or_equal_to: 100 },
if: ->(v) { v.voucher_type == PERCENTAGE_RATE }

FLAT_RATE = 'flat'.freeze
PERCENTAGE_RATE = 'percentage'.freeze
TYPES = [FLAT_RATE, PERCENTAGE_RATE].freeze

validates :voucher_type, inclusion: TYPES

def display_value
Spree::Money.new(amount)
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230508035306_add_type_to_vouchers.rb
@@ -0,0 +1,5 @@
class AddTypeToVouchers < ActiveRecord::Migration[7.0]
def change
add_column :vouchers, :voucher_type, :string, limit: 255
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Expand Up @@ -1185,6 +1185,7 @@
t.bigint "enterprise_id"
t.datetime "deleted_at", precision: nil
t.decimal "amount", precision: 10, scale: 2, default: "0.0", null: false
t.string "voucher_type", limit: 255
t.index ["code", "enterprise_id"], name: "index_vouchers_on_code_and_enterprise_id", unique: true
t.index ["deleted_at"], name: "index_vouchers_on_deleted_at"
t.index ["enterprise_id"], name: "index_vouchers_on_enterprise_id"
Expand Down
9 changes: 8 additions & 1 deletion spec/factories/voucher_factory.rb
@@ -1,8 +1,15 @@
# frozen_string_literal: true

FactoryBot.define do
factory :voucher do
factory :voucher_flat_rate, class: Voucher do
enterprise { build(:distributor_enterprise) }
voucher_type { Voucher::FLAT_RATE }
amount { 15 }
end

factory :voucher_percentage, class: Voucher do
enterprise { build(:distributor_enterprise) }
voucher_type { Voucher::PERCENTAGE_RATE }
amount { rand(1..100) }
end
end
2 changes: 1 addition & 1 deletion spec/models/spree/order_spec.rb
Expand Up @@ -1449,7 +1449,7 @@ def advance_to_delivery_state(order)
describe "#voucher_adjustments" do
let(:distributor) { create(:distributor_enterprise) }
let(:order) { create(:order, user: user, distributor: distributor) }
let(:voucher) { create(:voucher, code: 'new_code', enterprise: order.distributor) }
let(:voucher) { create(:voucher_flat_rate, code: 'new_code', enterprise: order.distributor) }

context "when no voucher adjustment" do
it 'returns an empty array' do
Expand Down
24 changes: 21 additions & 3 deletions spec/models/voucher_spec.rb
Expand Up @@ -11,15 +11,31 @@
end

describe 'validations' do
subject { build(:voucher, code: 'new_code', enterprise: enterprise) }
subject { build(:voucher_flat_rate, code: 'new_code', enterprise: enterprise) }

it { is_expected.to validate_presence_of(:code) }
it { is_expected.to validate_uniqueness_of(:code).scoped_to(:enterprise_id) }
it { is_expected.to validate_presence_of(:amount) }
it { is_expected.to validate_numericality_of(:amount).is_greater_than(0) }
it { is_expected.to validate_inclusion_of(:voucher_type).in_array(Voucher::TYPES) }

context "when voucher_type is flat rate" do
it { is_expected.to validate_numericality_of(:amount).is_greater_than(0) }
end

context "when voucher_type is percentage rate" do
subject { build(:voucher_percentage, code: 'new_code', enterprise: enterprise) }

it do
is_expected.to validate_numericality_of(:amount)
.is_greater_than(0)
.is_less_than_or_equal_to(100)
end
end
end

describe '#compute_amount' do
subject { create(:voucher_flat_rate, code: 'new_code', enterprise: enterprise, amount: 10) }

let(:order) { create(:order_with_totals) }

context 'when order total is more than the voucher' do
Expand All @@ -42,7 +58,9 @@
describe '#create_adjustment' do
subject(:adjustment) { voucher.create_adjustment(voucher.code, order) }

let(:voucher) { create(:voucher, code: 'new_code', enterprise: enterprise, amount: 25) }
let(:voucher) do
create(:voucher_flat_rate, code: 'new_code', enterprise: enterprise, amount: 25)
end
let(:order) { create(:order_with_line_items, line_items_count: 3, distributor: enterprise) }

it 'includes an amount of 0' do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/voucher_adjustments_spec.rb
Expand Up @@ -19,7 +19,7 @@
)
end
let(:shipping_method) { distributor.shipping_methods.first }
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) }
let(:voucher) { create(:voucher_flat_rate, code: 'some_code', enterprise: distributor) }

before do
order.update!(created_by: user)
Expand Down
6 changes: 4 additions & 2 deletions spec/system/consumer/split_checkout_spec.rb
Expand Up @@ -721,7 +721,7 @@

context "with voucher available" do
let!(:voucher) do
create(:voucher, code: 'some_code', enterprise: distributor, amount: 15)
create(:voucher_flat_rate, code: 'some_code', enterprise: distributor, amount: 15)
end

before do
Expand Down Expand Up @@ -1114,7 +1114,9 @@
end

describe "vouchers" do
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor, amount: 6) }
let(:voucher) do
create(:voucher_flat_rate, code: 'some_code', enterprise: distributor, amount: 6)
end

before do
voucher.create_adjustment(voucher.code, order)
Expand Down

0 comments on commit cb54397

Please sign in to comment.