yshmarov / moneygun

Rails Multitenancy boilerplate. Users-Memberships-Organizations. Row-based. Set tenant based on URL params by default
https://moneygun-e5b81fbfbbd9.herokuapp.com
MIT License
58 stars 3 forks source link

[DEMO] example using acts_as_tenant #110

Closed yshmarov closed 2 weeks ago

yshmarov commented 3 weeks ago

https://github.com/ErwinM/acts_as_tenant

For setting Current Tenant to work, we can't set ants_as_tenant on AccountUser model. It has to be accessible without scoping to a Tenant.

class AccountUser < ApplicationRecord
  belongs_to :account
  # acts_as_tenant :account

assert_difference does not work well with ActsAsTenant: https://jumpstartrails.com/discussions/testing-with-config-require_tenant-true

With require_tenant = true we can query acts_as_tenant models only if ActsAsTenant.current_tenant is set:

askvote(dev)> Inbox.count
(askvote):1:in `<main>': ActsAsTenant::Errors::NoTenantSet (ActsAsTenant::Errors::NoTenantSet)
askvote(dev)> Inbox.all
(askvote):2:in `<main>': ActsAsTenant::Errors::NoTenantSet (ActsAsTenant::Errors::NoTenantSet)
askvote(dev)> Current.account = Account.first
(askvote):3:in `<main>': uninitialized constant Current (NameError)

Current.account = Account.first
^^^^^^^
askvote(dev)> Inbox.all
(askvote):4:in `<main>': ActsAsTenant::Errors::NoTenantSet (ActsAsTenant::Errors::NoTenantSet)
askvote(dev)> ActsAsTenant.current_tenant = Account.first
  Account Load (0.6ms)  SELECT "accounts".* FROM "accounts" ORDER BY "accounts"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> #<Account:0x000000012161f478 id: 4, name: "Yaro", created_at: "2024-10-18 23:31:47.899202000 +0000", updated_at: "2024-10-19 07:57:00.340731000 +0000">
askvote(dev)> Inbox.all
  Inbox Load (1.3ms)  SELECT "inboxes".* FROM "inboxes" WHERE "inboxes"."account_id" = $1 /* loading for pp */ LIMIT $2  [["account_id", 4], ["LIMIT", 11]]
=> [#<Inbox:0x0000000127913fc0 id: 6, name: "b11", account_id: 4, created_at: "2024-10-19 08:22:57.549142000 +0000", updated_at: "2024-10-19 08:22:57.549142000 +0000">]
askvote(dev)> ActsAsTenant.current_tenant = Account.second
  Account Load (1.1ms)  SELECT "accounts".* FROM "accounts" ORDER BY "accounts"."id" ASC LIMIT $1 OFFSET $2  [["LIMIT", 1], ["OFFSET", 1]]
=> #<Account:0x0000000120ad2e58 id: 5, name: "second acc", created_at: "2024-10-19 08:17:04.817521000 +0000", updated_at: "2024-10-19 08:17:04.817521000 +0000">
askvote(dev)> Inbox.all
  Inbox Load (0.7ms)  SELECT "inboxes".* FROM "inboxes" WHERE "inboxes"."account_id" = $1 /* loading for pp */ LIMIT $2  [["account_id", 5], ["LIMIT", 11]]
=>
[#<Inbox:0x00000001212f1558 id: 4, name: "i1", account_id: 5, created_at: "2024-10-19 08:22:20.305598000 +0000", updated_at: "2024-10-19 08:22:20.305598000 +0000">,
 #<Inbox:0x0000000120ad5dd8 id: 5, name: "i2", account_id: 5, created_at: "2024-10-19 08:22:24.831796000 +0000", updated_at: "2024-10-19 08:22:24.831796000 +0000">]
yshmarov commented 2 weeks ago

outdated in favour of https://github.com/yshmarov/moneygun/pull/120