# masutaka/trial-rails8 architecture

> Architecture diagram of the GitHub repository masutaka/trial-rails8, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/masutaka/trial-rails8
- Repository: https://github.com/masutaka/trial-rails8

## Overview

##### Project Type and Purpose
- This is a monolithic full-stack web application built with Ruby on Rails 8.
- It provides a blog platform (posts, comments, scheduling), social features (follows, notifications), product management (rich text descriptions, image uploads), real-time chat via WebSocket, and user authentication.
- It uses Hotwire (Turbo Drive, Turbo Frames, Turbo Streams, Stimulus), Active Job with Sidekiq, Active Storage, Action Text, and Action Cable (Solid Cable adapter).

##### Key Architectural Components
a. Clients
- Web browser (HTML views enhanced with Turbo & Stimulus).
b. Rails Application Server
- HTTP request routing (config/routes.rb) → Controllers → Models → Views.
- MVC pattern: app/controllers, app/models, app/views.
- Turbo Drive for navigation, Turbo Frames/Streams for partial updates, Stimulus controllers for frontend behavior.
c. Database
- MySQL (config/database.yml) storing users, posts, comments, follows, notifications, products.
- Polymorphic associations for notifications, self-referential for follows.
d. Background Jobs
- Active Job configured with Sidekiq (config/sidekiq.yml & initializer).
- Redis as Sidekiq backend.
- Jobs: scheduled post publication, email sending, notifications.
e. WebSockets
- Action Cable (Solid Cable adapter configured in config/cable.yml).
- ChatChannel for real-time messaging.
f. File Storage & Rich Text
- Active Storage for file uploads (product images) using local/disk service (config/storage.yml).
- Action Text for rich-text descriptions.
g. Email
- Action Mailer sending password reset and product in-stock emails.
h. Infrastructure & DevOps
- Docker/Docker Compose for MySQL and Redis containers.
- CI/CD pipelines (GitHub Actions).
- Kamal hooks for deployment.
- Sidekiq Web UI for job monitoring.
i. Caching & Monitoring
- Bullet gem for N+1 detection.
- Rails cache store (config/cache.yml).

##### Relationships and Data Flows
- Browser → (HTTP/Turbo Drive) → Rails Router → Controller → Model → DB → View → Browser.
- Browser → (Turbo Streams/WebSocket) → Action Cable → ChatChannel → Broadcast → Browser.
- Rails → (Active Job enqueued) → Redis queue → Sidekiq worker → perform job (e.g., publish_post_job) → DB changes or email dispatch.
- Controllers interact with models and enqueue jobs or broadcast streams.
- Active Storage uploads files to storage service; URLs served by Rails.

##### Architectural Patterns & Principles
- MVC to separate concerns.
- Self-referential association for follow graph.
- Polymorphic association for notification extensibility.
- Event-driven design via background jobs and WebSocket broadcasting.
- Hotwire approach to minimize custom JS and SPA complexity.
- Docker-based local development replicates production dependencies.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend
    subgraph "Frontend (Browser)" 
        direction TB
        Browser["Web Browser"]:::frontend
        subgraph "Hotwire Stack"
            direction TB
            Turbo["Turbo (Drive/Frames/Streams)"]:::frontend
            Stimulus["Stimulus Controllers"]:::frontend
        end
    end

    %% Rails Application Core
    subgraph "Rails Application" 
        direction TB
        Router["Rails Router"]:::backend
        Controllers["Controllers"]:::backend
        Models["Models"]:::backend
        Views["Views"]:::backend

        subgraph "WebSocket Subsystem"
            direction TB
            CableConfig["Action Cable Config"]:::backend
            ChatChannel["ChatChannel"]:::backend
        end

        subgraph "Background Processing"
            direction TB
            ActiveJob["Active Job"]:::backend
            Sidekiq["Sidekiq Workers"]:::backend
        end

        subgraph "File & Text Services"
            direction TB
            ActiveStorage["Active Storage"]:::data
            ActionText["Action Text"]:::data
        end

        Mailer["Action Mailer"]:::backend
    end

    %% Data Stores
    subgraph "Data Layer"
        direction TB
        MySQL["MySQL Database"]:::data
        Redis["Redis"]:::data
    end

    %% Infrastructure
    subgraph "Infrastructure & DevOps"
        direction TB
        Docker["Docker & Compose"]:::infra
        CICD["CI/CD Pipelines"]:::infra
        Kamal["Kamal Hooks"]:::infra
        SidekiqUI["Sidekiq Web UI"]:::infra
    end

    %% Flows
    Browser -->|"HTTP/Turbo Drive"| Router
    Router --> Controllers
    Controllers --> Models
    Models --> MySQL
    Controllers --> Views
    Views --> Browser
    Browser -->|"Turbo Streams/WebSocket"| ChatChannel
    ChatChannel --> Browser
    Controllers -->|"enqueue job"| ActiveJob
    ActiveJob -->|"to Redis queue"| Redis
    Redis -->|"processed by"| Sidekiq
    Sidekiq -->|"perform Job"| Models
    Sidekiq --> Mailer
    Controllers --> ActiveStorage
    ActiveStorage -->|"stores files"| MySQL
    Controllers --> ActionText
    Mailer -->|"SMTP"| Browser

    %% Hotwire interactions
    Browser --> Turbo
    Browser --> Stimulus
    Turbo --> Controllers
    Stimulus --> Controllers

    %% Infrastructure links
    Docker --> MySQL
    Docker --> Redis
    Docker --> Sidekiq
    CICD --> Router
    Kamal --> Router
    SidekiqUI --> Redis

    %% Click events
    click Router "https://github.com/masutaka/trial-rails8/blob/main/config/routes.rb"
    click Controllers "https://github.com/masutaka/trial-rails8/tree/main/app/controllers/"
    click Models "https://github.com/masutaka/trial-rails8/tree/main/app/models/"
    click Views "https://github.com/masutaka/trial-rails8/tree/main/app/views/"
    click Stimulus "https://github.com/masutaka/trial-rails8/tree/main/app/javascript/controllers/"
    click Turbo "https://github.com/masutaka/trial-rails8/tree/main/app/assets/"
    click CableConfig "https://github.com/masutaka/trial-rails8/blob/main/config/cable.yml"
    click ChatChannel "https://github.com/masutaka/trial-rails8/blob/main/app/channels/chat_channel.rb"
    click ActiveJob "https://github.com/masutaka/trial-rails8/blob/main/config/initializers/sidekiq.rb"
    click Sidekiq "https://github.com/masutaka/trial-rails8/blob/main/config/sidekiq.yml"
    click Redis "https://github.com/masutaka/trial-rails8/blob/main/compose.yml"
    click ActiveStorage "https://github.com/masutaka/trial-rails8/blob/main/config/storage.yml"
    click ActionText "https://github.com/masutaka/trial-rails8/blob/main/db/migrate/*_create_action_text_tables.action_text.rb"
    click Mailer "https://github.com/masutaka/trial-rails8/blob/main/app/mailers/passwords_mailer.rb"
    click MySQL "https://github.com/masutaka/trial-rails8/blob/main/config/database.yml"
    click Docker "https://github.com/masutaka/trial-rails8/blob/main/compose.yml"
    click CICD "https://github.com/masutaka/trial-rails8/tree/main/.github/workflows/"
    click Kamal "https://github.com/masutaka/trial-rails8/tree/main/.kamal/hooks/"

    %% Styles
    classDef frontend fill:#cfe2ff,stroke:#000,color:#000
    classDef backend fill:#d1e7dd,stroke:#000,color:#000
    classDef data fill:#ffe5b4,stroke:#000,color:#000
    classDef infra fill:#e2e3e5,stroke:#000,color:#000
```
