Skip to content

Commit

Permalink
🥗✨Tobias: Payout#issue won't create Payment for new Beneficiary
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zspencer committed Jan 29, 2024
1 parent aa61795 commit 71c9b69
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/furniture/tobias/payout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion spec/tobias/payout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 71c9b69

Please sign in to comment.