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: Expose Trust#show with link to Payout#new #23

Open
wants to merge 8 commits into
base: tobias/issuing-payouts
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion app/furniture/tobias.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
class Tobias
class Tobias < Furniture
has_many :trusts, inverse_of: :tobias
default_scope { where(furniture_kind: "tobias") }
location(parent: :room)
end

# TOBIAS is hard to pluralize... Rails presumed TOBIAS was plural...
# So this tells Rails that TOBIAS is the singular form, and that TOBIASes
# is the plural.
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural(/^(tobias)$/i, '\1\2es')
inflect.singular(/^(tobias)es/i, '\1')
end
6 changes: 6 additions & 0 deletions app/furniture/tobias/record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Tobias
class Record < ApplicationRecord
self.abstract_class = true
extend StripsNamespaceFromModelName
end
end
11 changes: 11 additions & 0 deletions app/furniture/tobias/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Tobias
class Routes
def self.append_routes(router)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we call router.resources, we are connecting the URL path to the application.

/spaces/:space_id/rooms/:room_id, nested under the "Section" the TOBIAS is in.

So, resources :tobiases creates an entry point into our application at the /spaces/:space_id/rooms/:room_id/tobiases path. When we nest resources calls within a do...end block, like we do with router.resources :trusts this nests them within that path , a-la /spaces/:space_id/rooms/:room_id/tobiases/:tobias_id/trusts

What path do you think the line with router.resources :payouts creates?

router.resources :tobiases, module: "tobias" do
router.resources :trusts do
router.resources :payouts
end
end
end
end
end
6 changes: 5 additions & 1 deletion app/furniture/tobias/trust.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class Tobias
class Trust < ApplicationRecord
class Trust < Record
self.table_name = "tobias_trusts"

belongs_to :tobias, inverse_of: :trusts

location(parent: :tobias)

has_many :beneficiaries, inverse_of: :trust, dependent: :destroy
has_many :payouts, inverse_of: :trust, dependent: :destroy
end
Expand Down
7 changes: 7 additions & 0 deletions app/furniture/tobias/trust_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Tobias
class TrustPolicy < ApplicationPolicy
def show?
true
end
end
end
3 changes: 3 additions & 0 deletions app/furniture/tobias/trusts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Trust <%= trust.id %></h1>

<%= link_to("New Payout", trust.location(:new, child: :payout)) %>
9 changes: 9 additions & 0 deletions app/furniture/tobias/trusts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Tobias
class TrustsController < FurnitureController
expose :trust, model: Trust

def show
authorize trust
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The authorize method comes from Pundit, and is how we link the code in TrustPolicy, which describes who is allowed to see or change the Trust model, to the action being taken by the user.

end
end
end
1 change: 1 addition & 0 deletions app/models/furniture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def self.registry
marketplace: ::Marketplace::Marketplace,
livestream: ::Livestream,
section_navigation: SectionNavigation::SectionNavigation,
tobias: Tobias,
embedded_form: EmbeddedForm
}
end
Expand Down
1 change: 1 addition & 0 deletions db/migrate/20240127063826_create_tobias_payouts.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class CreateTobiasPayouts < ActiveRecord::Migration[7.1]
def change
create_table :tobias_trusts, id: :uuid do |t|
t.references :tobias, type: :uuid, foreign_key: {to_table: :furnitures}
t.timestamps
end

Expand Down
3 changes: 3 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,10 @@
end

create_table "tobias_trusts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "tobias_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["tobias_id"], name: "index_tobias_trusts_on_tobias_id"
end

create_table "utility_hookups", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
Expand Down Expand Up @@ -404,4 +406,5 @@
add_foreign_key "tobias_payments", "tobias_beneficiaries", column: "beneficiary_id"
add_foreign_key "tobias_payments", "tobias_payouts", column: "payout_id"
add_foreign_key "tobias_payouts", "tobias_trusts", column: "trust_id"
add_foreign_key "tobias_trusts", "furnitures", column: "tobias_id"
end
5 changes: 5 additions & 0 deletions spec/tobias/factories/tobias_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :tobias do
room
end
end
3 changes: 3 additions & 0 deletions spec/tobias/factories/trust_factory.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require_relative "tobias_factory"

FactoryBot.define do
factory :tobias_trust, class: "Tobias::Trust" do
tobias
end
end
Loading