home / skills / kaakati / rails-enterprise-dev / rails-context-verification

This skill verifies Rails codebase context before generation to prevent assumption bugs and ensure correct namespace, routes, and helpers.

npx playbooks add skill kaakati/rails-enterprise-dev --skill rails-context-verification

Review the files below or copy the command above to add this skill to your agents.

Files (4)
SKILL.md
5.6 KB
---
name: Rails Context Verification
description: "Systematic verification of codebase context before code generation to prevent assumption bugs. Use when: (1) Working in unfamiliar namespace, (2) Using authentication helpers, (3) Copying patterns across namespaces, (4) Before any code generation. Trigger keywords: context, assumptions, helpers, authentication, current_user, verify, validate context"
version: 1.2.0
category: implementation
---

# Rails Context Verification

Prevent "assumption bugs" by verifying codebase context before generating code.

## Context Verification Decision Tree

```
What are you generating?
│
├─ View with authentication
│   └─ Verify: rg "def current_" app/controllers/
│
├─ Controller with auth check
│   └─ Verify: rg "devise_for" config/routes.rb
│
├─ Route helpers (link_to, redirect_to)
│   └─ Verify: rails routes | grep namespace
│
├─ Instance variables in view
│   └─ Verify: rg "@variable\s*=" controller_file
│
├─ Custom helper method
│   └─ Verify: rg "def helper_name" app/helpers/
│
└─ Copying pattern from other namespace
    └─ STOP! Search target namespace for actual patterns
```

---

## NEVER Do This

**NEVER** assume authentication helper names:
```ruby
# WRONG - Assuming current_admin exists
<%= current_admin.email %>

# RIGHT - Verify first: rg "def current_" app/controllers/
# Found: current_administrator
<%= current_administrator.email %>
```

**NEVER** assume Devise scope matches model name:
```ruby
# WRONG - Model is Administrator, assuming admin scope
before_action :authenticate_admin!

# RIGHT - Check routes.rb: devise_for :administrators
before_action :authenticate_administrator!
```

**NEVER** assume route prefixes:
```erb
<%# WRONG - Assuming singular prefix %>
<%= link_to "Dashboard", admin_dashboard_path %>

<%# RIGHT - Verify: rails routes | grep admin → shows admins_ (plural) %>
<%= link_to "Dashboard", admins_dashboard_path %>
```

**NEVER** copy patterns across namespaces without verification:
```ruby
# WRONG - Copying client pattern to admin
# Client uses: before_action :set_account
# Admin controller:
before_action :set_account  # FAILS - admins don't have accounts!

# RIGHT - Verify what admin actually has
# rg "before_action" app/controllers/admins/base_controller.rb
before_action :authenticate_administrator!
```

**NEVER** use instance variables without verifying they're set:
```erb
<%# WRONG - Using @current_account without verification %>
<%= @current_account.name %>  # NIL ERROR if not set!

<%# RIGHT - Check controller first %>
<%# rg "@current_account\s*=" controller_file %>
<%# Not found? Don't use it. %>
```

---

## Core Verification Protocol

### Before ANY Code Generation

1. **Identify Namespace:** Admin? Client? API? Public?
2. **Search for Patterns:** Don't invent - discover
3. **Use Verified Names:** Only what search confirms exists
4. **Document Context:** Record verified helpers for team

### Quick Verification Commands

| What | Command |
|------|---------|
| Authentication helper | `rg "def current_" app/controllers/` |
| Signed-in? helper | `rg "signed_in\?" app/views/namespace/` |
| Devise scope | `rg "devise_for" config/routes.rb` |
| Route helpers | `rails routes \| grep namespace` |
| View helpers | `rg "def helper_name" app/helpers/` |
| Instance variables | `rg "@variable\s*=" controller_file` |
| Before actions | `rg "before_action" base_controller.rb` |
| Model methods | `rg "def method_name" app/models/` |
| Factories | `rg "factory :name" spec/factories/` |

---

## Verification Checklist

### For Views
- [ ] Authentication helper verified (`current_*`)
- [ ] Signed-in helper verified (`*_signed_in?`)
- [ ] Instance variables set in controller
- [ ] View helpers exist
- [ ] Route helpers correct

### For Controllers
- [ ] Authentication method verified (`authenticate_*!`)
- [ ] Authorization pattern discovered
- [ ] Before actions exist in base controller
- [ ] Service objects exist before calling

### For Tests
- [ ] Factory exists for model
- [ ] Shared examples exist if used
- [ ] Test helpers verified

---

## Namespace-Specific Patterns

| Namespace | Authentication | Route Prefix | Base Controller |
|-----------|---------------|--------------|-----------------|
| Admin | `current_administrator`, `administrator_signed_in?` | `admins_` | `Admins::BaseController` |
| Client | `current_user`, `user_signed_in?` | `clients_` | `Clients::BaseController` |
| API | Token-based | `api_v1_` | `Api::BaseController` |
| Public | None or optional | Default | `ApplicationController` |

**Warning:** These are examples. Your codebase may differ. Always verify!

---

## Context Documentation Format

When delegating to specialists, include verified context:

```markdown
**Verified Context:**
- Namespace: admin
- Auth helper: `current_administrator` (verified: app/controllers/application_controller.rb:42)
- Route prefix: `admins_` (verified: rails routes)
- Authorization: `require_super_admin` (verified: base_controller.rb:8)

CRITICAL: Use ONLY these verified helpers. Do not assume others exist.
```

---

## Remember

1. **Never assume** - always verify helper names, routes, methods
2. **Search first** - discover patterns before applying them
3. **Namespace matters** - admin ≠ client ≠ api (different patterns)
4. **Devise scope matters** - :users ≠ :admins ≠ :administrators
5. **2 minutes to verify** saves hours of debugging

---

## References

Detailed examples and commands in `references/`:
- `verification-commands.md` - Complete search command reference
- `pattern-discovery-examples.md` - Real-world verification workflows
- `workflow-integration.md` - Implementation workflow integration, beads tracking

Overview

This skill enforces a systematic verification of Rails codebase context before any code generation to prevent assumption bugs. It formalizes quick discovery steps for helpers, routes, instance variables, and namespace patterns so generated code matches the real project. Use it as a lightweight pre-flight check that saves debugging time and ensures safe, accurate changes.

How this skill works

The skill runs targeted searches and commands to confirm names and patterns used by the codebase (authentication helpers, devise scopes, route prefixes, instance variables, helpers, before_actions). Based on a decision tree it selects the right verification step for views, controllers, routes, or copied patterns and returns the exact items to use. It produces a short verified-context block to hand off to implementers or automated agents.

When to use it

  • Before generating views, controllers, routes, or tests in an unfamiliar namespace
  • When using authentication helpers (current_*, *_signed_in?, authenticate_*! )
  • When copying patterns across namespaces (admin vs client vs api)
  • Before any automated code generation step or multi-agent task
  • When writing code that references instance variables, helpers, or route helpers

Best practices

  • Always run focused searches (rg or rails routes) to confirm exact helper and scope names
  • Record a Verified Context block listing namespace, auth helper, route prefix, and source locations
  • Never hardcode assumptions—use only names confirmed by repository searches
  • If a pattern is missing in the target namespace, STOP and adapt rather than copying blindly
  • Keep verification steps under a couple minutes; treating verification as mandatory reduces rework

Example use cases

  • Generating an admin view that displays the current user email—verify current_administrator exists before using it
  • Adding a redirect in a controller—confirm route prefixes with rails routes | grep namespace to use correct path helpers
  • Porting a before_action from client to admins—search the admins namespace to find equivalent base controller actions
  • Writing tests that reference factories—rg spec/factories to ensure the factory exists before scaffolding tests
  • Creating a helper method—search app/helpers to avoid naming collisions or duplicates

FAQ

What commands should I run quickly to verify auth and routes?

Use ripgrep: rg "def current_" app/controllers/ and rg "devise_for" config/routes.rb, and run rails routes | grep namespace to confirm route prefixes.

What if the verification finds no matching helper or variable?

Stop and adapt: either implement the missing helper in the correct namespace, change the design to use confirmed patterns, or document the new verified context before generating code.