Files
openfoodnetwork/app/models/voucher.rb
Gaetan Craig-Riou d29119f5c5 Remove non need belongs_to associations from Adjustments
It turns out the "tax_rate" association isn't used and wasn't working.
Same for the "voucher" one, which I added to be consistent with existing
code.
Both of these weren't caught by the specs because you can't test associations
with a custome relation with 'shouda-matchers' see: https://github.com/thoughtbot/shoulda-matchers/issues/981
2023-05-15 13:42:40 +10:00

50 lines
1.3 KiB
Ruby

# frozen_string_literal: false
class Voucher < ApplicationRecord
acts_as_paranoid
belongs_to :enterprise
has_many :adjustments,
as: :originator,
class_name: 'Spree::Adjustment',
dependent: :nullify
validates :code, presence: true, uniqueness: { scope: :enterprise_id }
def value
10
end
def display_value
Spree::Money.new(value)
end
# Ideally we would use `include CalculatedAdjustments` to be consistent with other adjustments,
# but vouchers have complicated calculation so we can't easily use Spree::Calculator. We keep
# the same method to stay as consistent as possible.
#
# Creates a new voucher adjustment for the given order
def create_adjustment(label, order)
amount = compute_amount(order)
adjustment_attributes = {
amount: amount,
originator: self,
order: order,
label: label,
mandatory: false,
state: "open",
tax_category: nil
}
order.adjustments.create(adjustment_attributes)
end
# We limit adjustment to the maximum amount needed to cover the order, ie if the voucher
# covers more than the order.total we only need to create an adjustment covering the order.total
def compute_amount(order)
-value.clamp(0, order.total)
end
end