From 71c9b6991d93688ddffbe0dbc5caac949cb2354a Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Mon, 29 Jan 2024 15:15:14 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=97=E2=9C=A8`Tobias`:=20`Payout#issue`?= =?UTF-8?q?=20won't=20create=20`Payment`=20for=20new=20`Beneficiary`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This one actually needed adjusting! Turns out, `Payout#issue` would happily create a new `Payment` for a new `Beneficiary`, which is less than ideal. Now we have a guard clause that will prevent `Payout#issue` from making additional `Payments` when there are already `Payments` present. --- app/furniture/tobias/payout.rb | 2 ++ spec/tobias/payout_spec.rb | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/furniture/tobias/payout.rb b/app/furniture/tobias/payout.rb index 1d37b0e28..0541fcac5 100644 --- a/app/furniture/tobias/payout.rb +++ b/app/furniture/tobias/payout.rb @@ -9,6 +9,8 @@ class Payout < ApplicationRecord monetize :amount_cents def issue + return if payments.present? + per_beneficiary_amount = (amount / beneficiaries.count) beneficiaries.each do |beneficiary| payments.create_with(amount: per_beneficiary_amount).find_or_create_by(beneficiary_id: beneficiary.id) diff --git a/spec/tobias/payout_spec.rb b/spec/tobias/payout_spec.rb index 9a616f426..29e18003c 100644 --- a/spec/tobias/payout_spec.rb +++ b/spec/tobias/payout_spec.rb @@ -31,13 +31,19 @@ end context "when running twice" do - it "does not issue multiple payouts" do + it "does not issue multiple payouts, even when beneficiaries are added" do payout = create(:tobias_payout, amount_cents: 100_00) create_list(:tobias_beneficiary, 2, trust: payout.trust) payout.issue + create(:tobias_beneficiary, trust: payout.trust) + + # ActiveRecord appears to be caching the `payout.beneficiaries` results + # Reload busts that cache. + payout.reload + expect { payout.issue }.not_to(change(payout.payments, :count)) end end