|
| 1 | +[Conventions](/guides/conventions.md) / [File Structure](/guides/conventions/file-structure.md) / |
| 2 | + |
| 3 | +# Controllers |
| 4 | + |
| 5 | +We organise our controllers within our namespaces. |
| 6 | + |
| 7 | +We should follow resourceful controller conventions. |
| 8 | + |
| 9 | +``` |
| 10 | +. |
| 11 | +└ app |
| 12 | + └─ controllers |
| 13 | + ├─ [namespace] (Namespace-specific controllers) |
| 14 | + │ ├─ [controller_name]_controller.rb |
| 15 | + │ └─ application_controller.rb |
| 16 | + └─ application_controller.rb |
| 17 | +``` |
| 18 | + |
| 19 | +Each namespace has its own `ApplicationController` as a base for all controllers within that namespace. |
| 20 | + |
| 21 | +```ruby |
| 22 | +# Top-level application controller. |
| 23 | +# Shared functionality across all namespaces. |
| 24 | +class ApplicationController < ActionController::Base |
| 25 | + include Pundit::Authorization |
| 26 | +end |
| 27 | + |
| 28 | +# Find application controller. |
| 29 | +# Base controller for all Find controllers. |
| 30 | +class Find::ApplicationController < ApplicationController |
| 31 | +end |
| 32 | + |
| 33 | +# The API application controller inherits from ActionController::API. |
| 34 | +# Base controller for all API controllers. |
| 35 | +class API::ApplicationController < ActionController::API |
| 36 | +end |
| 37 | + |
| 38 | +# The base controller for all v1 API controllers. |
| 39 | +class API::V1::ApplicationController < API::ApplicationController |
| 40 | +end |
| 41 | +``` |
| 42 | + |
| 43 | +This allows us to set base functionality across groups of controllers. |
| 44 | + |
| 45 | +The pattern described in [ADR#6 Controller Structure](/guides/adr/0006-controller-structure.md) details a scalable pattern to managing nested resourceful controllers. |
| 46 | + |
| 47 | +```ruby |
| 48 | +class Find::RecruitmentCycles::CoursesController < Find::ApplicationController |
| 49 | + # GET /recruitment_cycles/:recruitment_cycle_id/courses |
| 50 | + def index |
| 51 | + @courses = recruitment_cycle.courses |
| 52 | + end |
| 53 | + |
| 54 | + # GET /recruitment_cycles/:recruitment_cycle_id/courses/:id |
| 55 | + def show |
| 56 | + @course = recruitment_cycle.courses.find(params[:id]) |
| 57 | + end |
| 58 | + |
| 59 | + private |
| 60 | + |
| 61 | + def recruitment_cycle |
| 62 | + @recruitment_cycle ||= RecruitmentCycle.find(params[:recruitment_cycle_id]) |
| 63 | + end |
| 64 | +end |
| 65 | +``` |
| 66 | + |
| 67 | +The `params[:id]` should always be the ID of the resource being acted upon. Parent resources should always be prefixed, e.g. `params[:recruitment_cycle_id]`. |
0 commit comments