Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ bin/rails g
## Generators

* [Core Setup](./lib/generators/rolemodel/core_setup)
* [Agent Docs](./lib/generators/rolemodel/agent_docs)
* [Github](./lib/generators/rolemodel/github)
* [Heroku](./lib/generators/rolemodel/heroku)
* [Readme](./lib/generators/rolemodel/readme)
Expand Down Expand Up @@ -129,7 +130,7 @@ Generators that leave behind conventions a coding agent needs to follow also ins
| `rolemodel:turbo:modals` | `turbo-modals` |
| `rolemodel:turbo:form` | `dynamic-forms` |
| `rolemodel:soft_destroyable` | `soft-destroyable` |
| `rolemodel:core_setup` | `polymorphic-parent-resources`, `rake-tasks` |
| `rolemodel:core_setup` | `controller-patterns`, `polymorphic-parent-resources`, `rake-tasks` |

## Utilities

Expand Down
1 change: 1 addition & 0 deletions lib/generators/rolemodel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## What you get

* [Agent Docs](./agent_docs)
* [Editors](./editors)
* [Github](./github)
* [GoodJob](./good_job)
Expand Down
14 changes: 14 additions & 0 deletions lib/generators/rolemodel/agent_docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Agent Docs Generator

[USAGE](./USAGE)

The agent instructions and conventions a new RoleModel app starts with. `rolemodel:core_setup` runs it.

## What you get

- `AGENTS.md`, with `CLAUDE.md` pointing at it. Fill in its first sentence: what the app is, and the one fact that changes how every task in it is approached.
- `docs/CONVENTIONS.md`, an index of the files in `docs/conventions/`. Each entry carries a `<!-- paths: -->` glob naming the files it applies to.
- `.github/instructions/conventions.instructions.md`, which points Copilot (including code review) at the same conventions.
- `.claude/hooks/surface_conventions.rb`, registered in `.claude/settings.json`. Before Claude Code edits a file, the hook lists the conventions whose globs match it. It's downloaded from the [`scaffold-docs`](https://github.com/RoleModel/skills/tree/main/skills/scaffold-docs) skill, so there's one copy to maintain, and it needs Ruby on the `PATH`.

The conventions assume the stack `core_setup` installs (Slim, SimpleForm, Optics, Turbo, RSpec with Playwright) and the [`controller-patterns`](https://github.com/RoleModel/skills/tree/main/skills/controller-patterns) skill. For an existing app, use the `scaffold-docs` and `generate-conventions` skills instead.
10 changes: 10 additions & 0 deletions lib/generators/rolemodel/agent_docs/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Description:
Adds AGENTS.md and RoleModel's starting conventions for a new app

Writes AGENTS.md (with CLAUDE.md pointing at it), docs/CONVENTIONS.md and
the convention files it indexes, and Copilot instructions that point at
them. It also installs the surface_conventions hook from RoleModel/skills,
which shows Claude Code the conventions that apply to each file it edits.

Example:
rails generate rolemodel:agent_docs
22 changes: 22 additions & 0 deletions lib/generators/rolemodel/agent_docs/agent_docs_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Rolemodel
class AgentDocsGenerator < GeneratorBase
HOOK_URL = 'https://raw.githubusercontent.com/RoleModel/skills/main/skills/scaffold-docs/assets/surface_conventions.rb'.freeze

source_root File.expand_path('templates', __dir__)

def install_docs
directory '.', '.'
end

def install_hook
get HOOK_URL, '.claude/hooks/surface_conventions.rb'
chmod '.claude/hooks/surface_conventions.rb', 0o755
end

private

def project_name
Rails.application.class.module_parent_name.titleize
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hooks": {
"PreToolUse": [
{
"matcher": "^(Edit|Write)$",
"hooks": [
{
"type": "command",
"command": "\"${CLAUDE_PROJECT_DIR}/.claude/hooks/surface_conventions.rb\""
}
]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
applyTo: "app/**/*,spec/**/*,db/migrate/**/*,lib/**/*,config/**/*"
---

# Conventions

`docs/CONVENTIONS.md` is the source of truth for this repo's conventions. Each entry carries a `<!-- paths: ... -->` comment naming the files it applies to. Read the convention files whose paths match what you're changing, and follow what they say — not the one-line index summary.

The branch name or PR title carries a ticket ID like `ABC-123`. Whenever the `linear` MCP server is connected, fetch that issue with `linear-get_issue` and read its description and acceptance criteria before writing or reviewing code — every time, not only when something looks off. Flag changes that contradict the ticket and requirements the diff doesn't address. No ID, or no server: work against these conventions alone and don't block on it.
26 changes: 26 additions & 0 deletions lib/generators/rolemodel/agent_docs/templates/AGENTS.md.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# <%= project_name %>

<One sentence: what this app is, plus the one fact that changes how every task in it is approached.>

## How We Build

- Deliver the smallest change that adds value. Speculative complexity makes the next change harder, so never pre-solve a hypothetical case; flag real uncertainty in one line and move on.
- Make the change easy, then make the easy change. When extending something, ask whether it's still the right shape, and reshape it first if not.
- Think like a Rubyist: prefer polymorphism to conditionals, and Rails conventions to custom machinery.
- Fix root causes, never symptoms. When reusing a method exposes a bug, fix that method rather than writing a near-copy around it.
- Testability is a code virtue: write code that looks test-first even when it wasn't. Test behavior, not implementation, and don't over-constrain.
- Prefer a clearer name or an extracted method to a comment.

## Before Editing

- Read `docs/CONVENTIONS.md`, then the convention files relevant to the task.
- No new gems, top-level directories, base classes, or JS libraries without asking. If no existing pattern fits, stop and say so.
- Keep changes minimal and focused; preserve unrelated work in a dirty tree.
- Write a focused failing test before implementing feature behavior.

## Verification

- `bin/setup` once per checkout; `bin/dev` to run the app.
- `bundle exec rspec spec/path/to_spec.rb` for the specs your change touches. CI runs the full suite; don't run it locally.
- `bundle exec rubocop -a`
- `yarn eslint`
1 change: 1 addition & 0 deletions lib/generators/rolemodel/agent_docs/templates/CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@AGENTS.md
18 changes: 18 additions & 0 deletions lib/generators/rolemodel/agent_docs/templates/docs/CONVENTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Conventions

Read the entries below, then the files relevant to the task.

- [Controllers follow the controller-patterns skill](conventions/controllers.md) — load the skill before writing a controller or route; no custom actions. <!-- paths: app/controllers/**/*.rb, config/routes.rb -->
- [Logic lives in models](conventions/logic-in-models.md) — models, POROs and form objects; app/services only for external boundaries; jobs call one method. <!-- paths: app/controllers/**/*.rb, app/jobs/**/*.rb, app/models/**/*.rb, app/forms/**/*.rb, app/services/**/*.rb -->
- [Callbacks stay guarded](conventions/callbacks.md) — the record, its siblings, or a Turbo refresh; guarded state-transition jobs; never mail or API calls. <!-- paths: app/models/**/*.rb -->
- [String-backed enums, referenced by key](conventions/string-enums.md) — explicit string values, never a magic string. <!-- paths: app/models/**/*.rb, db/migrate/*.rb, spec/factories/**/*.rb -->
- [Queries live in model scopes](conventions/reuse-scopes.md) — named scopes, no order/where/group_by in templates. <!-- paths: app/controllers/**/*.rb, app/jobs/**/*.rb, app/models/**/*.rb, app/views/**/*.slim -->
- [No SQL strings in queries](conventions/no-sql-strings.md) — hashes, ranges, association joins, in_order_of; Arel only as a last resort. <!-- paths: app/**/*.rb, lib/**/*.rb, spec/**/*.rb -->
- [Eager load in the query that feeds the view](conventions/eager-loading.md) — includes up front; includes plus joins becomes eager_load. <!-- paths: app/controllers/**/*.rb, app/models/**/*.rb, app/jobs/**/*.rb, app/views/**/*.slim -->
- [Views are Slim; forms are simple_form](conventions/views-and-forms.md) — Slim, simple_form and app/inputs, render records by to_partial_path, shared partials in app/views/application, Optics. <!-- paths: app/views/**/*.slim, app/inputs/**/*.rb, app/helpers/**/*.rb, app/assets/stylesheets/**/*.css -->
- [Partials declare their locals](conventions/partial-locals.md) — locals on the first line; never an instance variable. <!-- paths: app/views/**/_*.slim -->
- [Reach for Turbo before Stimulus](conventions/stimulus.md) — TurboForm and Turbo refreshes first; Stimulus only for browser-only interactions. <!-- paths: app/javascript/**/*.js, app/views/**/*.slim -->
- [Generate migrations; never renumber one](conventions/migrations.md) — generator timestamps, data__ migrations with their own models, schema.rb column order. <!-- paths: db/migrate/*.rb, db/schema.rb -->
- [Test layers](conventions/test-layers.md) — system specs for flows, request specs for page-less endpoints, no controller specs. <!-- paths: spec/**/*.rb -->
- [A fix ships with the spec that proves it](conventions/reproduce-the-bug.md) — failing-first specs with factories; don't stub the app's own models. <!-- paths: spec/**/*.rb -->
- [System specs assert what a user sees](conventions/system-spec-assertions.md) — labels and text for targets, data_test for scoping, flash for success. <!-- paths: spec/system/**/*.rb, app/views/**/*.slim -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Callbacks stay guarded

A callback derives the record's own attributes, keeps its siblings consistent, or broadcasts a Turbo refresh. It may enqueue a job for a specific state transition, but only behind a guard like `if: -> { saved_change_to_status?(to: 'closed') }`. A callback never sends mail or calls an external API directly.

An unguarded side effect runs on every save path, including factories, imports, and the console.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Controllers follow the controller-patterns skill

Load the `controller-patterns` skill before writing or reshaping a controller or route, and match one of its shapes. There are no custom actions: an action that doesn't fit is a resource of its own.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Eager load in the query that feeds the view

Load the associations a view or job walks with `includes` in the query that fetches the records, not with per-row lookups.

`includes` silently turns into one large joined `eager_load` when the same association is also joined or filtered on.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Logic lives in models

Put a business rule or state transition in a named model method, or in a PORO under `app/models/`. Put input spanning several records in a form object in `app/forms/` that includes `ActiveModel::Model` and `ActiveModel::Attributes`.

`app/services/` is for boundaries with the outside world (third-party APIs, uploads, webhook consumers). Don't add a service for domain logic.

A controller holds control flow only (see the controller-patterns skill). A job loads its records and calls one model method.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generate migrations; never renumber one

Create a migration with `bin/rails generate migration`, never by hand-writing the timestamp: round `...0000` versions collide. The `schema_migrations` table tracks applied migrations by their timestamp prefix alone, so never change a committed migration's timestamp.

Generate a migration that changes data as `bin/rails generate migration data__<short_description>`, so data migrations stand apart from schema changes in `db/migrate/`. It declares its own models, so later changes to the app's models can't change what it does. Name each after its table and inherit from `ActiveRecord::Base`: `class Issue < ActiveRecord::Base; end`. A table with a `type` column also needs `self.inheritance_column = nil`. Back-fill with one `update_all` per distinct value rather than one per row.

`db/schema.rb` dumps columns in the local database's physical order. Before committing it, check that no column moved without a migration causing it; order follows the order migrations landed on the default branch.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# No SQL strings in queries

Build queries from ActiveRecord, never from SQL string fragments. That rules out `select('...')`, `order('...')`, `pluck('...')`, `joins('...')`, `group('...')`, `where('start_date < ?', date)`, string `update_all`, and `Arel.sql(...)`.

Use instead:

- Hash conditions and open-ended ranges: `where(start_date: ...date)`, `where(archived_at: nil)`.
- Association joins and nested hashes: `joins(:team).order(teams: { name: :asc })`.
- `in_order_of(:status, %w[active pending archived])` for a custom sort order.

If a query seems to need raw SQL (a JSONB `->>` lookup, Postgres `DISTINCT ON`, `NULLS LAST`), redesign it: store the value in a real column, or narrow the rows with a scope and pick in Ruby. An Arel node is the last resort, and worth a note in the PR.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Partials declare their locals

A partial declares its locals on the first line with `-# locals: (widget:)`. It never reads an instance variable. When a partial isn't a record's, render it with the shorthand `render 'widgets/fields', f:`, not `render partial:`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# A fix ships with the spec that proves it

A bug fix includes a spec that fails without the fix. Build real records with factories, and don't stub the app's own models.

A spec that stubs the collaborator holding the bug passes whether or not the fix is there.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Queries live in model scopes

Chain the model's existing scopes before writing a query, and add a missing query as a named scope on the model, not inline in a controller, job, or template. Templates render what they're given: no `order`, `where`, or `group_by` in Slim.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Reach for Turbo before Stimulus

Dynamic page behavior is server-rendered: a dependent or conditional form uses TurboForm (the `dynamic-forms` skill), and a page that changes after an action redirects and morphs with a Turbo refresh. Stimulus is for interactions only the browser can do (drag-and-drop, Tom Select), using the controllers the gem's generators install. No inline JavaScript in templates. A new Stimulus controller is a question to raise in the PR.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# String-backed enums, referenced by key

Declare enums with explicit string values on a string column, like `enum :status, { active: 'active', archived: 'archived' }`. Reference values by key, predicate, scope, or factory trait, never as a raw string.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# System specs assert what a user sees

Select by what the user sees, never by CSS class or DOM structure:

- Click targets and form fields by their visible text or label: `click_on "Yes, I'm Sure"`, `fill_in 'Name', with: 'Bob'`. A field without a label uses its name attribute: `fill_in 'user[name]', with: 'Bob'`.
- Scope to one part of the page with a `data-testid` from `dom_id`, and select it with the `data_test` helper:

```slim
tr data-testid=dom_id(widget)
td = link_to 'Edit', edit_widget_path(widget)
```

```ruby
within(data_test(widget)) { click_on 'Edit' }
```

End every interaction with an assertion on its visible result. For a create, update or destroy, that's usually the flash message: `expect(page).to have_content 'Successfully Created Widget'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test layers

Cover a user flow with a system spec on the default `rack_test` driver, and add `js: true` (Playwright) only when the behavior needs JavaScript. Cover an endpoint with no page of its own (a webhook, a turbo_stream or JSON endpoint) with a request spec in `spec/requests/`. Don't write controller specs. Unit-test logic in the model that holds it, and policies in `spec/policies/`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Views are Slim; forms are simple_form

Templates are Slim. Build every form with `simple_form_for`, `simple_fields_for` for nested attributes, and `f.association` for `belongs_to` selects. Reach for the custom inputs in `app/inputs/` before adding one.

Every input option earns its place: drop options that restate a default. A `label_method` repeated across forms becomes `to_label` on the model; an option every input of a type needs becomes a custom input or wrapper.

Render records with `render @widget` or `render @widgets`, which find the partial through `to_partial_path`. Name a partial only when no record owns it.

Shared partials live in `app/views/application/`, the fallback lookup path for a bare `render`, never in `app/views/shared/`. Check there and in `app/helpers/` for an existing partial or helper before writing a new one. Style with Optics classes and tokens (the `optics-context` skill), not one-off CSS.
1 change: 1 addition & 0 deletions lib/generators/rolemodel/all_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def run_all_the_generators
generate 'rolemodel:saas:all'
generate 'rolemodel:mailers'
generate 'rolemodel:linters:all'
generate 'rolemodel:agent_docs'
generate 'rolemodel:ui_components:all'
generate 'rolemodel:source_map'
generate 'rolemodel:good_job'
Expand Down
1 change: 1 addition & 0 deletions lib/generators/rolemodel/core_setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ app you can push straight to Heroku right after generation.
- [GitHub](../github) — standard GitHub configuration
- [Readme](../readme) — standard project README
- [Linters](../linters) — Rubocop and ESLint
- [Agent Docs](../agent_docs) — `AGENTS.md`, conventions, and the hook that surfaces them
- [Heroku](../heroku) — standard Heroku deployment configuration
- [Sentry](../sentry) — error monitoring for Ruby and JavaScript
- [Lograge](../lograge) — condensed request logging
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/rolemodel/core_setup/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Description:
Runs the core generators every new Rails app should have

Sets up Slim, Webpack, Optics, SimpleForm, Tom Select, testing (RSpec &
friends), Turbo, flash messages, GitHub config, README, linters, Heroku
deployment, Sentry, and Lograge. It also installs the agent skills for
friends), Turbo, flash messages, GitHub config, README, linters, agent
docs and conventions, Heroku deployment, Sentry, and Lograge. It also installs the agent skills for
controller patterns, resource_for, and TaskTools. The result is an app
that is ready to push directly to Heroku.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def run_the_core_generators
generate 'rolemodel:github'
generate 'rolemodel:readme'
generate 'rolemodel:linters:all'
generate 'rolemodel:agent_docs'
return if options.skip_deployable?

generate 'rolemodel:heroku'
Expand Down
32 changes: 32 additions & 0 deletions spec/generators/rolemodel/agent_docs_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
RSpec.describe Rolemodel::AgentDocsGenerator, type: :generator do
before do
hook = StringIO.new("#!/usr/bin/env ruby\n")
allow(URI).to receive(:open).with(described_class::HOOK_URL, {}).and_yield(hook)
end

it 'adds AGENTS.md, the conventions index, and Copilot instructions' do
run_generators

assert_file 'AGENTS.md', /\A# Example Rails Current$/
assert_file 'CLAUDE.md', "@AGENTS.md\n"
assert_file 'docs/CONVENTIONS.md', %r{\(conventions/controllers\.md\)}
assert_file 'docs/conventions/controllers.md'
assert_file '.github/instructions/conventions.instructions.md'
end

it 'installs the surface_conventions hook from RoleModel/skills and registers it' do
run_generators

assert_file '.claude/hooks/surface_conventions.rb', "#!/usr/bin/env ruby\n"
expect(File.executable?(File.join(destination_root, '.claude/hooks/surface_conventions.rb'))).to be true
assert_file '.claude/settings.json', %r{\.claude/hooks/surface_conventions\.rb}
end

it 'indexes every convention file it installs' do
run_generators

index = File.read(File.join(destination_root, 'docs/CONVENTIONS.md'))
installed = Dir.children(File.join(destination_root, 'docs/conventions')).sort
expect(index.scan(%r{\(conventions/([\w-]+\.md)\)}).flatten.sort).to eq installed
end
end