Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Tobias: Refactor Payout#issue and supporting code #14

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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