Rails Multitenancy boilerplate. Users-Memberships-Organizations. Row-based. Set tenant based on URL params by default
Teams should be an MVP feature!
Yes, this can generate an “long” url like /organizations/344/projects/4532/tasks/24342342/edit
, but it preserves the logical hierarchy.
resources :organizations do
resources :memberships
resources :projects do
resources :tasks do
end
end
end
I tried using OrganizationMiddlewhare
like JumpstartPro does, but it felt too much of an unconventional approach.
For example in Trello, you can have 2 unrelated boards open in 2 tabs.
git clone [email protected]:yshmarov/moneygun.git your_new_project_name
cd your_new_project_name
bundle install
rails db:create db:migrate
bin/dev
🚫 Bad
# models/project.rb
belongs_to :organization
belongs_to :user
✅ Good
# models/project.rb
belongs_to :organization
belongs_to :membership
I recommend scoping downstream models to organization
too. This way you can query them more easily.
# models/task.rb
belongs_to :organization # <- THIS
belongs_to :project
Inbox
is an example of a well-integrated resource scoped to an organization
. With pundit authorization and tests. Use it as an inspiration.
To quickly generate nested resources you can use gem nested_scaffold
rails generate nested_scaffold organization/project name
Generate a pundit policy:
rails g pundit:policy project
I did not focus on system tests, because the frontend can evolve a lot.
There are quite a lot of tests covering authentication, authorization, and multitenancy.
# run all tests
rails test:all
bundle exec erb_lint --lint-all -a
bundle exec rubocop -A
Feel free to raise an Issue or open a Pull Request!