plugins/elixir-phoenix/skills/phoenix-contexts/SKILL.md
Phoenix context design — creating/splitting contexts, Scope (1.8+), Ecto.Multi, PubSub, routers, plugs, controllers. Use when editing contexts, routers, or designing boundaries.
npx skillsauth add oliver-kriska/claude-elixir-phoenix phoenix-contextsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Reference for designing and implementing Phoenix contexts (bounded contexts).
Ecto.Multi for transactions with side effectslib/my_app/
├── accounts/ # Context directory
│ ├── user.ex # Schema
│ ├── scope.ex # Scope struct (Phoenix 1.8+)
├── accounts.ex # Context module (public API)
All context functions MUST accept scope as first parameter:
def list_posts(%Scope{} = scope) do
from(p in Post, where: p.user_id == ^scope.user.id)
|> Repo.all()
end
def create_post(%Scope{} = scope, attrs) do
%Post{user_id: scope.user.id}
|> Post.changeset(attrs)
|> Repo.insert()
|> broadcast(scope, :created)
end
# ✅ Reference by ID, convert at boundary
def create_order(%Scope{} = scope, user_id, product_ids) do
with {:ok, user} <- Accounts.fetch_user(scope, user_id) do
do_create_order(scope, user.id, product_ids)
end
end
# ❌ Reaching into other context's internals
alias MyApp.Accounts.User # Don't do this
Repo.all(from o in Order, join: u in User, ...) # Don't query other schemas
| Wrong | Right |
|-------|-------|
| Service objects (UserCreationService) | Context functions (Accounts.create_user/2) |
| Repository pattern wrapping Repo | Repo IS the repository |
| Direct Repo calls in controllers | Delegate to context |
| Schema callbacks with side effects | Use Ecto.Multi |
%Scope{} struct for authorization context${CLAUDE_SKILL_DIR}/references/scopes-auth.md "Pre-Scopes Patterns")For detailed patterns, see:
${CLAUDE_SKILL_DIR}/references/context-patterns.md - Full context module, PubSub, Multi, cross-boundary${CLAUDE_SKILL_DIR}/references/scopes-auth.md - Scope struct, multi-tenant, authorization, plugs${CLAUDE_SKILL_DIR}/references/routing-patterns.md - Verified routes, pipelines, API auth${CLAUDE_SKILL_DIR}/references/plug-patterns.md - Function/module plugs, placement, guards${CLAUDE_SKILL_DIR}/references/json-api-patterns.md - JSON controllers, FallbackController, API authdevelopment
Verify Elixir/Phoenix changes — compile, format, and test in one loop. Use after implementation, before PRs, or after fixing bugs.
development
OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task, Registry, pattern matching, with chains, pipes. Use when designing processes or debugging BEAM issues.
tools
Self-improving loop for plugin skills. Reads program.md, proposes one mutation per iteration, evaluates against deterministic scorer, keeps improvements via git, reverts failures. Targets weakest skill+dimension. Use with /loop for overnight runs.
development
Project health audit and health check — architecture, performance, tests, dependencies, code quality. Use when assessing overall project health, before releases, or after refactors.