Files
openfoodnetwork/app/models/voucher.rb
Gaetan Craig-Riou 9b680e1a92 Add CalculatedAdjustments to Voucher
Vouchers are only flat rate of 10 for now, so we add a FlatRate
calculator in a before_validation callback
2023-05-15 13:42:37 +10:00

29 lines
552 B
Ruby

# frozen_string_literal: false
class Voucher < ApplicationRecord
include CalculatedAdjustments
belongs_to :enterprise
has_many :adjustments, as: :originator, class_name: 'Spree::Adjustment'
validates :code, presence: true, uniqueness: { scope: :enterprise_id }
before_validation :add_calculator
def value
10
end
def display_value
Spree::Money.new(value)
end
private
# For now voucher are only flat rate of 10
def add_calculator
self.calculator = Calculator::FlatRate.new(preferred_amount: -value)
end
end