Skip to main content

Overview

This is a Ruby on Rails 7.1+ application following the Model-View-Controller (MVC) architectural pattern. The codebase is organized into standard Rails directories, each with a specific purpose. This guide helps you navigate the code and understand where different pieces of functionality live.

Directory Structure

App Directory (Main Code)

The app/ directory contains the core application logic, organized by responsibility:

Models (app/models/)

Models represent your data and business logic. Each model typically corresponds to a database table. Core Models: Supporting Models: Key Concepts:
Finding code:
  • Look here for data validation rules
  • Database queries and scopes
  • Relationships between entities
  • Business logic calculations

Controllers (app/controllers/)

Controllers handle HTTP requests and coordinate between models and views. Main Controllers: Special Controllers: API Controllers (app/controllers/api/): Controller Pattern:
Finding code:
  • Look here for request handling logic
  • Form processing and validation
  • Redirects and responses
  • Before/after action filters

Views (app/views/)

Views contain the HTML templates that render the user interface. Rails uses ERB (Embedded Ruby) for templating. View Organization:
View Pattern:
Key Concepts:
  • <% %> - Execute Ruby code (no output)
  • <%= %> - Execute Ruby code and output result
  • Partials (_filename.html.erb) - Reusable components
  • Helpers - Ruby methods available in views
Finding code:
  • Look here for HTML structure
  • User interface elements
  • Form markup
  • Display logic

Jobs (app/jobs/)

Background jobs handle asynchronous tasks that don’t need to complete immediately. Key Jobs: Job Pattern:
Finding code:
  • Look here for scheduled tasks
  • Email sending logic
  • API integrations that run periodically
  • Long-running operations

Mailers (app/mailers/)

Mailers handle email generation and sending. Key Mailers: Email Templates: app/views/person_mailer/
  • welcome_email.html.erb - Welcome email template
  • renewal_reminder.html.erb - Renewal reminder template
Mailer Pattern:

Policies (app/policies/)

Policies define authorization rules using Pundit. They determine what actions users can perform. Key Policies: Policy Pattern:
Permission Levels:
  • read - View data
  • write - Edit data
  • permit - Manage permissions
  • verify_members - Verify membership status

Helpers (app/helpers/)

Helpers contain utility methods available in views. Key Helpers: Helper Pattern:

Assets (app/assets/ and app/javascript/)

Assets include stylesheets, JavaScript, and images. Stylesheets (app/assets/stylesheets/):
  • application.css - Main stylesheet manifest
  • Custom CSS files for specific features
JavaScript (app/javascript/):
  • application.js - JavaScript entry point
  • Stimulus controllers for interactive features
  • Turbo for dynamic page updates
Images (app/assets/images/):
  • Static images and icons

Configuration (config/)

Configuration files control how the application behaves. Key Files:
  • routes.rb - URL routing definitions
  • database.yml - Database connection settings
  • environments/ - Environment-specific settings
    • development.rb - Development settings
    • production.rb - Production settings
    • test.rb - Test settings
Routes Pattern:
URL Structure:
  • /peoplePeopleController#index
  • /people/123PeopleController#show
  • /people/123/editPeopleController#edit
  • /api/peopleApi::PeopleController#index
Other Configuration:

Database (db/)

Database schema, migrations, and seed data. Key Files:
  • schema.rb - Current database structure (auto-generated)
  • seeds.rb - Initial data for new databases
  • migrate/ - Migration files that modify the schema
Migration Pattern:
Key Concepts:
  • Never edit schema.rb directly - it’s auto-generated
  • Create migrations to change the database structure
  • Run rails db:migrate to apply migrations
  • Migrations are timestamped and run in order

Library Code (lib/)

Custom code that doesn’t fit into the standard Rails structure. Key Files: Rake Task Pattern:

Tests (test/)

Comprehensive test suite covering all application functionality. Test Organization:
Test Pattern:
Test Types:
  • Model tests - Business logic and validations
  • Controller tests - Request handling and responses
  • System tests - Full user workflows in browser
  • Integration tests - Multiple components working together

Public Files (public/)

Static files served directly without Rails processing.
Files in public/ are served at the root URL:
  • public/robots.txt/robots.txt
  • public/favicon.ico/favicon.ico

Finding Your Way Around

I need to add a new feature…

  1. Start with routes (config/routes.rb) - Define the URL
  2. Create/modify controller (app/controllers/) - Handle the request
  3. Create/modify model (app/models/) - Add business logic
  4. Create/modify views (app/views/) - Build the UI
  5. Add policy (app/policies/) - Control access
  6. Write tests (test/) - Verify it works

I need to understand how X works…

Authentication:
  • app/controllers/sessions_controller.rb - Login/logout
  • app/controllers/application_controller.rb - authorize_admin method
  • app/models/admin.rb - Admin model with permissions
Membership Management:
  • app/models/person.rb - Member data and status
  • app/models/membership.rb - Membership periods
  • app/controllers/memberships_controller.rb - Renewal flow
  • app/jobs/renewal_reminders_job.rb - Automated reminders
Google Integration:
  • app/controllers/google_controller.rb - OAuth and sync operations
  • app/jobs/calendar_sync_job.rb - Calendar synchronization
  • lib/calendar_aggregator.rb - Event aggregation logic
Email System:
  • app/mailers/person_mailer.rb - Email definitions
  • app/views/person_mailer/ - Email templates
  • config/environments/production.rb - SMTP settings
API:
  • app/controllers/api/ - API endpoints
  • app/models/api_key.rb - Authentication
  • config/routes.rb - API routes (under /api)

I need to modify the database…

  1. Generate migration: rails generate migration DescriptionOfChange
  2. Edit migration file in db/migrate/
  3. Run migration: rails db:migrate
  4. Update model in app/models/ if needed
  5. Update tests to reflect changes

I need to debug an issue…

Check these places in order:
  1. Server logs - docker compose logs app
  2. Controller - Where the request is handled
  3. Model - Where business logic lives
  4. View - What’s being rendered
  5. Routes - How URLs map to controllers
  6. Policy - Authorization rules
  7. Database - Data issues via Rails console

Common Patterns

Creating a New Model

Creating a New Controller

Adding a Background Job

Request Flow

Understanding how a request flows through the application:
Example: Viewing a member profile

Key Rails Conventions

Naming Conventions

  • Models: Singular, CamelCase (Person, ApiKey)
  • Controllers: Plural, CamelCase (PeopleController, ApiKeysController)
  • Tables: Plural, snake_case (people, api_keys)
  • Files: snake_case (person.rb, api_key.rb)

Directory Mapping

RESTful Actions

Standard controller actions follow REST conventions:
  • index - List all records (GET /people)
  • show - Display one record (GET /people/123)
  • new - Show form for new record (GET /people/new)
  • create - Save new record (POST /people)
  • edit - Show form for editing (GET /people/123/edit)
  • update - Save changes (PATCH /people/123)
  • destroy - Delete record (DELETE /people/123)

Summary

The Rails application structure is organized by responsibility:
  • Models (app/models/) - Data and business logic
  • Controllers (app/controllers/) - Request handling
  • Views (app/views/) - User interface
  • Jobs (app/jobs/) - Background tasks
  • Policies (app/policies/) - Authorization
  • Tests (test/) - Quality assurance
  • Config (config/) - Application settings
  • Database (db/) - Schema and migrations
Key principles:
  • Convention over Configuration - Follow Rails naming conventions
  • DRY (Don’t Repeat Yourself) - Reuse code via helpers, partials, and inheritance
  • Separation of Concerns - Keep models, views, and controllers focused
  • RESTful Design - Use standard CRUD actions where possible
When you need to find something, think about what it does:
  • Data/Logic → Models
  • User Interface → Views
  • Request Handling → Controllers
  • Background Processing → Jobs
  • Access Control → Policies
  • URL Routing → Config/Routes