Extending ModelRails
Adding a workspace-scoped feature
Most features you build are workspace-scoped: data a tenant owns that must never leak across workspaces. The framework keeps that explicit — there is no magic default_scope — so you opt in deliberately at each step. Here is the full path for a new model (say, a Milestone).
1. Generate the model and run the migration
rails generate model Milestone name:string workspace:references
rails db:migrate
rails generate model only writes the migration; rails db:migrate applies it. Skipping the second command is the most common first mistake.
2. Decide how it is tenant-scoped
Two shapes — picking the wrong one is the most common design mistake:
- A workspace-level root (a top-level thing a workspace owns, like
Project) →include Tenanted, which addsbelongs_to :workspaceand afor_current_workspacescope. - A child of something already tenant-scoped (e.g. a
Commenton aProject) → justbelongs_to :project. Do not addTenantedor aworkspace_id; it inherits its tenant transitively through the parent. This is exactly whyResourceandDocumentcarry noworkspace_id— they reach the workspace viaresource → project → workspace.
# app/models/milestone.rb — a workspace-level root
class Milestone < ApplicationRecord
include Tenanted # adds belongs_to :workspace + the for_current_workspace scope
belongs_to :created_by, class_name: "User"
validates :name, presence: true
end
Scoping is explicit, not automatic.
Tenanteddeliberately installs nodefault_scope. You scope every query yourself (step 3). That avoidsdefault_scope’s hidden filter, which changes queries far from the line that declared it, but it means you are responsible for never loading a tenant model unscoped.
3. Controller — scope through the workspace, and authorize
Include WorkspaceScoped (it resolves @workspace from the URL slug and sets Current.workspace), then query through the association — never Milestone.all:
# app/controllers/workspaces/milestones_controller.rb
class Workspaces::MilestonesController < ApplicationController
include WorkspaceScoped
def index
authorize Milestone
@milestones = @workspace.milestones.kept # scoped via the association
end
def create
authorize Milestone
@milestone = @workspace.milestones.build(milestone_params)
@milestone.created_by = Current.user
# ...
end
end
@workspace.milestones is what keeps one workspace from loading another’s records. Current.workspace (set by WorkspaceScoped) is a second check behind it, which policies and for_current_workspace rely on.
The moment a create involves a second row — a membership, a join record, anything that must exist for the first row to be usable — move the assembly onto the tenant root as a verb:
Workspace#create_milestone(attrs, creator:)in theWorkspace#create_projectshape (one transaction,lock!+ guard, both writes, returns the possibly-invalid record for form re-render). Committing the first row and then writing the second outside the transaction is the orphaned-record bug class #660 and #676 closed — a raise between the writes strands a committed record its own creator can’t see. See the creation-verb shape under Concurrency in Architecture.
Only the seven actions
Every routed action is index, show, new, create, edit, update, or destroy, with no cited exceptions. A member do ... end block with a verb in it is a resource that has not been named yet: archive and unarchive are create and destroy on a nested singular archival; resending an invitation is create on a nested resend; a two-step ceremony is two creates on two nouns (a passkey’s challenge, then its credential); a static page is show on pages, keyed by name. The trade is one more small controller for a URL that names the domain, and base took it everywhere (its own bin/rails routes, filtered to non-REST actions, prints nothing). The ModelRails/RestfulActions cop (lib/rubocop) fails a public controller method with any other name on commit, so a helper that belongs below private fails too.
4. Authorize with a Pundit policy
Every controller action calls authorize. Add a policy that extends ApplicationPolicy, which provides membership (the current user’s membership in Current.workspace) and can?("permission") (reads that member’s role-permission flags):
# app/policies/milestone_policy.rb
class MilestonePolicy < ApplicationPolicy
def index?
membership.present? # any member of the workspace
end
def create?
can?("manage_projects") # gated on a role permission
end
def update?
create?
end
def destroy?
record.created_by == user || can?("manage_workspace")
end
end
The permission keys (manage_projects, manage_members, manage_workspace, …) live on each role; see Workspace Administration for the full list.
5. Opt into shared behavior (optional)
Mix in the same concerns the built-in models use, only as needed:
| Concern | Gives you | Requirement |
|---|---|---|
Discardable |
Soft delete (discard!, .kept scope) |
— |
Trackable |
Activity-log entries when the record changes | — |
Broadcastable |
Turbo Stream broadcasts on change | define a private broadcast_target (e.g. workspace or the parent record) |
Project includes all three; Resource broadcasts to its project. Copy whichever match your model.
If your Trackable model has a column that must stay secret — an API key, a webhook signing secret — add it to Trackable::SENSITIVE_ATTRIBUTES. That list is the only thing standing between a changed column and the operations activity ledger, which renders changes verbatim, and activity_logs.metadata is plain JSON, so an unlisted secret is stored unencrypted for the whole retention window. Being encrypted on your own model does not exempt a column: encrypted-at-rest and secret are different questions, which is why the list is declared by hand rather than derived.
Per-model traits
A concern that belongs to one model is a trait, and it lives with its model, not in app/models/concerns/: app/models/user/avatar.rb reopens the class and nests the module —
class User < ApplicationRecord
module Avatar
extend ActiveSupport::Concern
# ...
end
end
— and the model includes it by its bare name (include Avatar). The nested form is required, not stylistic: the compact module User::Avatar does not open User‘s lexical scope, so the trait cannot see the model’s constants. app/models/concerns/ is for concerns a second model also includes (Discardable, Trackable, Broadcastable). What earns a trait is a domain name a user would recognize plus a reason to change that differs from the model’s; a slice by Rails artifact type (“validations”, “callbacks”) does not, and a method that overrides a shared concern’s hook or calls super stays in the class body, where include order cannot shadow it. Include order is callback registration order, so a trait that registers callbacks is included where its callbacks belonged. Base’s own traits under app/models/{user,invitation,workspace,membership}/ are the exemplars. The ModelRails/ModelConcernNamespace cop (lib/rubocop) fails a compact-form file under app/models/<model>/ on commit.
6. Outside the request cycle (jobs, rake tasks, machine clients)
Controllers establish Current.workspace for you (WorkspaceScoped resolves it from the URL slug); nothing does that automatically anywhere else. A job, rake task, or any future non-browser entry point doing tenant-scoped work must set it explicitly — and should read it back with Current.workspace! (note the bang), which raises Current::NoWorkspaceError when context was never established. The plain Current.workspace returns nil in that situation, and a nil inside a where clause silently widens the query across tenants — the exact failure the explicit-scoping design exists to prevent.
class DigestJob < ApplicationJob
def perform(workspace_id)
Current.workspace = Workspace.find(workspace_id) # establish explicitly
Current.workspace!.projects.find_each { |p| ... } # read with the bang
end
end
A job that emails a workspace’s members goes through a notifier: see A job that emails a workspace’s members.
Cross-workspace queries
Everything above scopes to one workspace. Some code legitimately needs the
opposite — a query that spans every workspace on purpose: a maintenance
sweep, a retention job, an operator fixing a single account from the command
line. Tenanted installs no default_scope, so nothing stops a query from
crossing workspaces; the question is where it’s safe to write one.
Where it’s safe
spec/code_smells/no_unscoped_tenant_loads_spec.rb only scans
app/controllers/, app/helpers/, and app/views/ — request-context code,
where Current.workspace is ambient and a stray class-level
Project.find(params[:id]) can hand a signed-in user’s role in their
workspace the authority to act on someone else’s record
(ApplicationPolicy#record_in_current_workspace? is the runtime backstop for
that case, but the unscoped load itself is the smell the spec fails on).
Jobs, rake tasks, and the Rails console are outside that scan — there’s
no ambient Current.workspace to leak and no signed-in user’s permissions to
misapply. That’s the boundary: request-context code always scopes through
the workspace (see Adding a workspace-scoped
feature above); background code whose
whole job is touching many or all workspaces queries the model directly.
Pattern 1 — iterate every workspace explicitly
WorkspaceCapacitySweepJob walks every kept workspace and reads each one’s
own associations — never Current.workspace, never for_current_workspace:
# app/jobs/workspace_capacity_sweep_job.rb
Workspace.kept.find_each do |workspace|
sweep_members_metric(workspace) # workspace.memberships.kept.count, etc.
end
Reach for this shape when the job genuinely means “every workspace” — quota checks, per-tenant digests, anything that needs each workspace’s own scoped data one at a time.
Trap: don’t call
for_current_workspacefrom code like this expecting “every record.” It reads ambientCurrent.workspace, which isniloutside a request, so the scope silently becomeswhere(workspace: nil)— zero rows, not all of them. Query through the workspace association instead (workspace.memberships.kept), as above.
Pattern 2 — a global condition, not a tenant identity
Most of the scheduled sweeps in app/jobs/ (registered in
config/recurring.yml) don’t iterate workspaces at all. They query the model
directly on a condition that has nothing to do with tenancy — age, status —
so touching every workspace is just what “in batches” naturally does:
# ActivityLogRetentionSweepJob — 12-month retention window, no workspace filter
ActivityLog.where(created_at: ...RETENTION_WINDOW.ago)
.where.not(action: ActivityLog::SECURITY_ACTIONS)
.in_batches(of: 100, &:delete_all)
# WorkspaceInvitationExpiringSweepJob — every workspace's expiring invitations at once
Invitation.where(accepted_at: nil, declined_at: nil)
.where("expires_at BETWEEN ? AND ?", Time.current, 24.hours.from_now)
.find_each { |invitation| ... }
# DigestMailerJob — User isn't even Tenanted, but the same shape applies
User.not_suspended
.joins(:preferences)
.where("user_preferences.digest_next_due_at <= ?", Time.current)
.find_each { |user| ... }
This is the same class-level-finder shape the request-context spec forbids
in a controller — legitimate here for the same reason as Pattern 1: no
ambient Current.workspace to misapply, and the condition doing the scoping
is global by design rather than standing in for “the current workspace.”
Pattern 3 — operator tools resolve one record by its own identifier
lib/tasks/admin.rake and lib/tasks/tenancy.rake run outside any request
too, so an operator can resolve a single record with a class-level finder —
something the code-smell spec would flag inside a controller:
# lib/tasks/admin.rake
user = User.find_by!(email_address: args[:email])
workspace = Workspace.find_by!(slug: args[:slug])
It’s safe here because there’s no signed-in user whose permissions could misfire against the result — the operator names the target directly on the command line, and the task acts on exactly that record, not on “whatever the current workspace happens to be.”
Pattern 4 — the operations area resolves through the actor’s reach
Operations:: controllers (/operations) run inside a request — the
signed-in-user case Pattern 3 is careful to rule out — but deliberately with
no workspace context: they never include WorkspaceScoped and never set
Current.workspace, because an operator administers the instance from above
the workspaces, not from inside one. A workspace is reached only through the
signed-in operator’s own reach relation:
# app/models/user.rb
def operated_workspaces(include_discarded: false)
return Workspace.none unless operator?
include_discarded ? Workspace.all : Workspace.kept
end
# app/controllers/operations/workspaces_controller.rb
@workspace = operated_workspaces.find_by!(slug: params[:slug])
Workspace isn’t Tenanted, so no_unscoped_tenant_loads_spec.rb wouldn’t
flag a bare Workspace.find_by! here — but the relation form is not there to get past that spec;
it is the point. operated_workspaces is every kept workspace today (the activity ledger
alone passes include_discarded: true); it’s
also the one seam a future scoped-operator model changes, and every
operations controller reading through it (rather than each reinventing “all
workspaces”) is what makes that a one-method change instead of an audit.
Anything inside the resolved workspace still loads through that workspace’s
associations (@workspace.memberships, @workspace.activity_logs), exactly
like Patterns 1 and 2. See Instance operations for what the
area does with this.
Rule of thumb
- Request-context code (controllers, helpers, views): always scope
through the workspace association (
@workspace.projects.find_by!(...)). Never a class-level finder on aTenantedmodel — the code-smell spec enforces this. - Background code that deliberately spans workspaces (jobs, rake tasks,
console): query the model directly — either by iterating
Workspace.keptand reading each workspace’s own associations (Pattern 1), or by a business condition unrelated to tenant identity (Pattern 2). Never reach forfor_current_workspacethere;Current.workspaceisn’t set, and the scope will silently return nothing instead of everything. - Background code that means to act on one workspace (the
DigestJobexample in step 6 above) setsCurrent.workspaceexplicitly and reads it back withCurrent.workspace!— the opposite of this section, and documented there.
Adding a New Resource Type
The Resource registry uses a polymorphic pattern. To add a new type (e.g., Slideshow):
1. Create the model
rails generate model Slideshow
rails db:migrate # generate writes the migration; this applies it
# app/models/slideshow.rb
class Slideshow < ApplicationRecord
has_one :resource, as: :resourceable, dependent: :destroy
has_many :slides, dependent: :destroy
end
A resource type is reached through resource → project → workspace, so it needs no workspace_id and does not include Tenanted — see Adding a workspace-scoped feature for when a model does.
2. Register the type
In app/models/resource.rb, add to the allowlist:
ALLOWED_RESOURCEABLE_TYPES = %w[Document Slideshow].freeze
3. Create view partials
app/views/workspaces/projects/resources/types/_slideshow.html.erb
app/views/workspaces/projects/resources/types/_slideshow_form.html.erb
The resource new, edit, and show views render the matching partial by resourceable_type.
4. Add strong parameters
In ResourcesController#resourceable_params, add a case:
when "Slideshow"
params.fetch(:slideshow, {}).permit(:title, slides_attributes: [:image, :caption, :position])
Customizing the Site Logo
Brand has three fork-owned files: the words (config/locales/en/brand.en.yml), the colors (app/assets/tailwind/tokens/_brand.css), and the mark — app/views/shared/_site_mark.html.erb, a file whose entire body is one <svg>. Replace it wholesale with your artwork. It is merge=ours, so upstream never conflicts with it again. Three things on the root <svg> are the contract with the template, and a view spec asserts them:
fill="currentColor"— the mark takes its color from the link around it, which is what re-lights it in dark mode; on the header that is--color-chrome-mark(below).aria-hidden="true"— the mark is decorative; the brand name next to it is what assistive tech reads.- a
viewBox, and nowidth,height, orclass— the caller sizes it.
The lockup around it, app/views/shared/_site_logo.html.erb, is template-owned: it renders the mark and the product name as siblings inside whatever link the caller provides. The caller’s link is the flex box and sizes the mark with a child selector; the one local, name_class, lets a compact variant keep the name for screen readers only:
<%= link_to root_path, class: "flex items-center gap-2 min-h-11 focus-ring rounded [&>svg]:h-6" do %>
<%= render "shared/site_logo" %>
<% end %>
<%= link_to root_path, class: "flex items-center min-h-11 focus-ring rounded [&>svg]:h-5" do %>
<%= render "shared/site_logo", name_class: "sr-only" %>
<% end %>
Use h-* on the child, not size-*: a wordmark is wider than tall, and a square box letterboxes it. The favicon and PWA icons in public/ are the same artwork again, in the sizes listed below; public/icon.svg colors itself with prefers-color-scheme rather than the app’s .dark class because a favicon renders outside the document.
Favicon and PWA Icons
The app ships with a multi-format favicon setup in public/:
| File | Purpose |
|---|---|
favicon.ico |
Legacy browsers (any size) |
icon.svg |
Modern browsers (scalable, crisp at all sizes) |
apple-touch-icon.png |
iOS home screen bookmark |
icon-192.png |
PWA icon (192x192) |
icon-512.png |
PWA icon and splash screen (512x512) |
manifest.webmanifest |
PWA manifest (app name, icons, theme) |
The <link> tags that load them are in app/views/shared/_layout_head.html.erb and the docs layout, app/views/layouts/markdowndocs/application.html.erb.
To replace with your own branding, swap the files in public/ keeping the same filenames and sizes. Update name and short_name in manifest.webmanifest to match your app name.
Branding the Header Bar
The header bar paints from a family of chrome tokens, so a fork recolors it from _brand.css without touching app/views/shared/_header.html.erb. Each token defaults to the semantic token the bar used before (app/assets/tailwind/tokens/_semantic.css), so an unbranded fork looks exactly as it did:
| Token | Paints | Default |
|---|---|---|
--color-chrome |
the bar | --color-surface-raised |
--color-chrome-border, --chrome-border-width |
the rule under it | --color-border, 1px |
--color-on-chrome |
icons and controls on the bar | --color-text-body |
--color-on-chrome-strong |
the product name | --color-text-heading |
--color-on-chrome-muted |
the account chevron | --color-text-muted |
--color-on-chrome-hover |
a control under the pointer | --color-interactive-hover |
--color-chrome-mark |
the site mark | --color-on-chrome-strong |
--color-chrome-focus |
the focus outline, via focus-ring-chrome |
--color-interactive-focus |
Brand the whole family or none of it: a bar color with the default text tokens is a dark-on-dark header. Set them in OKLCH; one :root value covers both themes, and a second, inside .dark, overrides the dark one:
:root {
--color-chrome: oklch(27.1% 0.080 251.6);
--color-chrome-border: oklch(86.3% 0.176 89.8);
--chrome-border-width: 4px;
--color-on-chrome: oklch(100% 0 0);
--color-on-chrome-strong: oklch(100% 0 0);
--color-on-chrome-muted: oklch(86.9% 0.020 252.9);
--color-on-chrome-hover: oklch(86.3% 0.176 89.8);
--color-chrome-mark: oklch(86.3% 0.176 89.8);
--color-chrome-focus: oklch(86.3% 0.176 89.8);
}
spec/code_smells/chrome_contrast_spec.rb measures whatever _brand.css sets: every on-chrome token at 7:1 on --color-chrome and the mark and focus colors at 3:1, in both themes, and it fails when a branded chrome leaves a token unset. A control a fork adds to the bar uses text-on-chrome and focus-ring-chrome; the account menu and the mobile menu panel open onto bg-surface-raised and keep the ordinary tokens, so they stay readable under any bar color.
Cookie Consent (GDPR)
The app includes a GDPR cookie consent banner via biscuit-rails, overridden at app/views/biscuit/banner/_banner.html.erb to fix three gaps in the gem’s defaults (#500): Reject non-essential is the emphasized default action on first visit (not Accept — rejecting must be at least as easy as accepting), the banner is server-rendered hidden when consent already exists so it never flashes, and reopening the preferences panel (footer link) shows the visitor’s actual saved choices instead of stale checkboxes. It renders at the bottom of every page and manages consent across 4 categories:
| Category | Required | Purpose |
|---|---|---|
necessary |
Yes | Session, CSRF, theme preference |
analytics |
No | Usage tracking (Google Analytics, etc.) |
preferences |
No | Non-essential preference cookies |
marketing |
No | Advertising and retargeting pixels |
Configuration is in config/initializers/biscuit.rb. The engine is mounted at /biscuit.
Guarding third-party scripts
Wrap any non-essential scripts with the biscuit_allowed? helper:
<% if biscuit_allowed?(:analytics) %>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXX"></script>
<% end %>
<% if biscuit_allowed?(:marketing) %>
<!-- Retargeting pixel -->
<% end %>
In controllers:
Biscuit::Consent.new(cookies).allowed?(:analytics)
Disabling the banner
If your deployment only uses functional cookies (session, theme, CSRF), you can remove the banner by deleting <%= biscuit_banner %> from app/views/shared/_layout_tail.html.erb (the tail the app’s layouts share) and from app/views/layouts/markdowndocs/application.html.erb.
Invitation Types
The invitation system supports two modes:
- Email invitations — enter email addresses, system sends invitation emails with 7-day expiry tokens
- Magic link invitations — generate a shareable URL (no email needed), useful for posting in Slack or team docs
Both types create the same Invitation record. The difference is whether email is present. See Workspace Administration for full details.
Adding Custom Workspace Roles
Seed a new role with custom permissions:
# db/seeds.rb
Role.find_or_create_by!(slug: "billing_admin", workspace_id: nil) do |r|
r.name = "Billing Admin"
r.permissions = { manage_settings: true, manage_billing: true }
end
Then check the permission in policies:
def manage_billing?
can?("manage_billing")
end
Keep Owner a permission superset. A role can only be granted by someone who already holds every permission it confers (
ApplicationPolicy#may_grant?— this is what blocks Admin→Owner escalation). So every permission you introduce —manage_billingabove — must also be added to the Owner role, or no one, not even an Owner, can assign the role that uses it and it silently disappears from every role picker. Add the key to Owner in the same seed:owner = Role.find_by!(slug: "owner", workspace_id: nil) owner.update!(permissions: owner.permissions.merge("manage_billing" => true))Editing
seeds.rbdoes not touch Owner rows already persisted in production/staging — backfill those with a data migration.
Upgrading Project Roles to Role Model
If you need custom project roles beyond creator/editor/viewer:
- Add
role_idtoproject_memberships:rails generate migration AddRoleIdToProjectMemberships role:references - Seed project-specific roles with a
contextcolumn on Role - Update
ProjectMembershipPolicyto usecan?instead of enum checks - Migrate existing data: map enum values to Role records
Adding Per-Resource Permissions
For fine-grained access (e.g., “can view Document A but not Document B”):
- Create a
ResourceSharemodel:user_id,resource_id,permission(read/write) - Update
ResourcePolicyto check both project membership AND resource shares - Resources without shares fall back to project-level permissions
Project Tools registry
Each project carries a set of tools (tabs in the project navigation). The base template ships :docs only. Forks add tools by registering them in config/initializers/project_tools.rb after building the tool’s surface (model + controller + routes + views):
# config/initializers/project_tools.rb
Rails.application.config.to_prepare do
ProjectTools::Registry.reset!
# Built-in tool — keep this.
ProjectTools::Registry.register(
key: :docs,
path_helper: :workspace_project_resources_path,
default_enabled: true
)
# Your tool — register it here.
ProjectTools::Registry.register(
key: :messages,
path_helper: :workspace_project_messages_path,
default_enabled: true
)
end
path_helper is a project-scoped route helper the project tab bar calls as helper(workspace, project).
Gate a tool’s controller so its routes redirect back to project home when the tool is disabled for that project:
class Workspaces::Projects::MessagesController < ApplicationController
include WorkspaceScoped
include ProjectScoped # resolves @project; must come BEFORE enforces_tool
include EnforcesProjectTool
enforces_tool :messages # redirects if tool_enabled?(:messages) is false
# …
end
The EnforcesProjectTool concern reads @project.tool_enabled?(key), and enforces_tool registers its guard where it is declared, so ProjectScoped must be included first; a guard that finds no @project lets the request through. See Project Tools for the full how-to.
Clientside (external-client area)
Clientside shares a read-only project view with external clients without giving them workspace membership or a seat in workspace policies. Clientside covers the ClientAccess record, who may turn it on and invite, the Clientside:: controllers, and where a fork adds client-area pages.
AI tooling: bring your own
The template ships no AI-agent configuration — no CLAUDE.md, no .cursorrules, no agent-os/, nothing. This is a policy, not an omission: AI tooling is a per-developer choice layered onto a fork, not something a fork inherits. An agent-config file committed to the template becomes unexplainable cruft — or worse, doctrine — in every downstream fork, whose own test suite would then be enforcing another developer’s tooling choices.
The boundary is enforced by a template invariant in spec/code_smells/template_invariants_spec.rb: the suite fails if any AI-agent configuration file (CLAUDE.md, AGENTS.md, .claude/, .cursorrules, .aider, and similar) becomes tracked in git. If you use AI assistance, keep your agent’s configuration local via .git/info/exclude, which ignores files without touching the tracked .gitignore.
In your own fork you set the policy. If you want agent configuration to ship with your app, extend (or delete) the pattern in that invariant — with a comment saying why — and commit your setup like any other file.
Next steps
- Architecture — the request flow, tenancy model, and key directories your new code plugs into.
- Deployment — ship it with Kamal once your feature is built.
- Browse the full docs index for feature-specific references (workspaces, notifications, identity, background jobs).