Skip to content

Commit

Permalink
🧹 Tobias: Refactor Payout#issue and supporting code
Browse files Browse the repository at this point in the history
There was a number of things I didn't love about my implementation of
`Payout#issue`:

- I had added a `Tobias::Record` far earlier than necessary
- There were linter errors
- `Payout#payout_amount` felt redundant

This remediates those nose-wrinkles, and sets us up nicely for the next
test.
  • Loading branch information
zspencer committed Jan 29, 2024
1 parent 68c8111 commit eec626a
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/furniture/tobias/beneficiary.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Tobias
class Beneficiary < Record
class Beneficiary < ApplicationRecord
self.table_name = "tobias_beneficiaries"

belongs_to :trust
Expand Down
2 changes: 1 addition & 1 deletion app/furniture/tobias/payment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Tobias
class Payment < Record
class Payment < ApplicationRecord
self.table_name = "tobias_payments"

monetize :amount_cents
Expand Down
5 changes: 2 additions & 3 deletions app/furniture/tobias/payout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ class Payout < ApplicationRecord
has_many :beneficiaries, through: :trust
has_many :payments

monetize :payout_amount_cents
monetize :amount_cents

def issue
per_beneficiary_amount = (payout_amount / beneficiaries.count)
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)
end
end
Expand Down
9 changes: 0 additions & 9 deletions app/furniture/tobias/record.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/furniture/tobias/trust.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Tobias
class Trust < Record
class Trust < ApplicationRecord
self.table_name = "tobias_trusts"

has_many :beneficiaries

end
end
2 changes: 1 addition & 1 deletion db/migrate/20240127063826_create_tobias_payouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def change
end

create_table :tobias_payouts, id: :uuid do |t|
t.monetize :payout_amount
t.monetize :amount
t.references :trust, type: :uuid, foreign_key: {to_table: :tobias_trusts}
t.timestamps
end
Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@
end

create_table "tobias_payouts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.integer "payout_amount_cents", default: 0, null: false
t.string "payout_amount_currency", default: "USD", null: false
t.integer "amount_cents", default: 0, null: false
t.string "amount_currency", default: "USD", null: false
t.uuid "trust_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
Expand Down
1 change: 0 additions & 1 deletion spec/tobias/factories/beneficiary_factory.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FactoryBot.define do
factory :tobias_beneficiary, class: "Tobias::Beneficiary" do

end
end
1 change: 0 additions & 1 deletion spec/tobias/factories/trust_factory.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FactoryBot.define do
factory :tobias_trust, class: "Tobias::Trust" do

end
end
4 changes: 2 additions & 2 deletions spec/tobias/payout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

RSpec.describe Tobias::Payout do
describe "#issue" do
it "issues a Payment to each Beneficiary for their share of the #payout_amount" do
payout = create(:tobias_payout, payout_amount_cents: 150_00)
it "issues a Payment to each Beneficiary for their share of the #amount" do
payout = create(:tobias_payout, amount_cents: 150_00)

beneficiaries = create_list(:tobias_beneficiary, 10, trust: payout.trust)

Expand Down

0 comments on commit eec626a

Please sign in to comment.