# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "Client Layer"
        Browser[Web Browser]
        MobileApp[Mobile App]
        APIClient[API Client]
    end

    subgraph "Web Server Layer"
        Puma[Puma/Unicorn]
        Rack[Rack Interface]
    end

    subgraph "Rails Framework"
        subgraph "Middleware Stack"
            RequestId[RequestId]
            RemoteIp[RemoteIp]
            Cookies[Cookies]
            Session[Session]
            Flash[Flash]
            SSL[SSL]
        end

        subgraph "Action Pack"
            Routing[ActionDispatch::Routing]
            Controller[ActionController]
            API[ActionController::API]
        end

        subgraph "Action View"
            Templates[ERB/Builder Templates]
            Helpers[View Helpers]
            Partials[Partials]
        end

        subgraph "Active Record"
            Models[Models]
            Associations[Associations]
            Validations[Validations]
            Callbacks[Callbacks]
            Migrations[Migrations]
        end

        subgraph "Active Model"
            Attributes[Attributes]
            Dirty[Dirty Tracking]
            Serialization[Serialization]
        end

        subgraph "Active Support"
            CoreExt[Core Extensions]
            Caching[Caching]
            Notifications[Instrumentation]
            I18n[Internationalization]
        end
    end

    subgraph "Background Processing"
        ActiveJob[ActiveJob]
        SolidQueue[Solid Queue]
        Sidekiq[Sidekiq]
    end

    subgraph "Real-time Communication"
        ActionCable[ActionCable]
        Channels[Channels]
        Connections[Connections]
    end

    subgraph "Storage & Email"
        ActiveStorage[ActiveStorage]
        ActionMailer[ActionMailer]
        ActionMailbox[ActionMailbox]
        ActionText[ActionText]
    end

    subgraph "Data Layer"
        PostgreSQL[(PostgreSQL)]
        MySQL[(MySQL)]
        SQLite[(SQLite)]
        Redis[(Redis)]
        Memcached[(Memcached)]
        S3[(AWS S3)]
    end

    Browser --> Puma
    MobileApp --> Puma
    APIClient --> Puma
    Puma --> Rack
    Rack --> RequestId
    RequestId --> RemoteIp
    RemoteIp --> Cookies
    Cookies --> Session
    Session --> Flash
    Flash --> SSL
    SSL --> Routing
    Routing --> Controller
    Routing --> API
    Controller --> Templates
    Controller --> Models
    API --> Models
    Templates --> Helpers
    Templates --> Partials
    Models --> Associations
    Models --> Validations
    Models --> Callbacks
    Models --> Attributes
    Models --> PostgreSQL
    Models --> MySQL
    Models --> SQLite
    Controller --> ActiveJob
    ActiveJob --> SolidQueue
    ActiveJob --> Sidekiq
    SolidQueue --> Redis
    Sidekiq --> Redis
    Controller --> ActionCable
    ActionCable --> Channels
    ActionCable --> Connections
    ActionCable --> Redis
    Models --> ActiveStorage
    ActiveStorage --> S3
    Controller --> ActionMailer
    ActionMailer --> ActiveJob
    Models --> ActionText
    Caching --> Redis
    Caching --> Memcached
```

## コンポーネント依存関係図

```mermaid
graph TD
    subgraph "Meta Gem"
        Rails[rails]
    end

    subgraph "Application Bootstrap"
        Railties[railties]
    end

    subgraph "Web Layer"
        ActionPack[actionpack]
        ActionView[actionview]
        ActionDispatch[action_dispatch]
        ActionController[action_controller]
    end

    subgraph "Model Layer"
        ActiveRecord[activerecord]
        ActiveModel[activemodel]
        Arel[arel]
    end

    subgraph "Background & Communication"
        ActiveJob[activejob]
        ActionCable[actioncable]
        ActionMailer[actionmailer]
        ActionMailbox[actionmailbox]
    end

    subgraph "Content & Storage"
        ActiveStorage[activestorage]
        ActionText[actiontext]
    end

    subgraph "Foundation"
        ActiveSupport[activesupport]
    end

    subgraph "External Dependencies"
        Rack[rack]
        GlobalID[globalid]
        Zeitwerk[zeitwerk]
    end

    Rails --> Railties
    Rails --> ActionPack
    Rails --> ActionView
    Rails --> ActiveRecord
    Rails --> ActiveModel
    Rails --> ActiveJob
    Rails --> ActionCable
    Rails --> ActionMailer
    Rails --> ActionMailbox
    Rails --> ActiveStorage
    Rails --> ActionText
    Rails --> ActiveSupport

    Railties --> ActiveSupport
    Railties --> ActionPack

    ActionPack --> ActionView
    ActionPack --> ActiveSupport
    ActionPack --> Rack

    ActionView --> ActiveSupport

    ActiveRecord --> ActiveModel
    ActiveRecord --> ActiveSupport
    ActiveRecord --> Arel

    ActiveModel --> ActiveSupport

    ActiveJob --> ActiveSupport
    ActiveJob --> GlobalID

    ActionCable --> ActiveSupport
    ActionCable --> ActionPack
    ActionCable --> Zeitwerk

    ActionMailer --> ActionView
    ActionMailer --> ActiveJob

    ActionMailbox --> ActiveRecord
    ActionMailbox --> ActionMailer
    ActionMailbox --> ActiveStorage

    ActiveStorage --> ActiveRecord
    ActiveStorage --> ActiveJob

    ActionText --> ActiveRecord
    ActionText --> ActiveStorage
```

## MVCリクエストフロー図

```mermaid
sequenceDiagram
    participant Client
    participant Rack as Rack Server
    participant MW as Middleware Stack
    participant Router as ActionDispatch::Routing
    participant Controller as ActionController
    participant Model as ActiveRecord
    participant View as ActionView
    participant DB as Database

    Client->>Rack: HTTP Request
    Rack->>MW: Process Request
    MW->>MW: RequestId
    MW->>MW: RemoteIp
    MW->>MW: Cookies
    MW->>MW: Session
    MW->>MW: Flash
    MW->>Router: Route Request

    Router->>Controller: Dispatch to Action

    Note over Controller: Before Filters

    Controller->>Model: Query/Create/Update/Delete
    Model->>DB: SQL Query
    DB-->>Model: Result Set
    Model-->>Controller: Model Objects

    Note over Controller: Action Logic

    Controller->>View: Render Template
    View->>View: Process ERB/Partials
    View-->>Controller: HTML/JSON Response

    Note over Controller: After Filters

    Controller-->>MW: Response
    MW-->>Rack: Process Response
    Rack-->>Client: HTTP Response
```

## ActiveJob非同期処理フロー図

```mermaid
sequenceDiagram
    participant App as Rails Application
    participant AJ as ActiveJob
    participant Adapter as Queue Adapter
    participant Queue as Job Queue
    participant Worker as Background Worker
    participant Job as Job Class

    App->>AJ: MyJob.perform_later(args)
    AJ->>AJ: Serialize Arguments
    AJ->>Adapter: Enqueue Job
    Adapter->>Queue: Push to Queue

    Note over Queue: Job Persisted

    Worker->>Queue: Poll for Jobs
    Queue-->>Worker: Job Data
    Worker->>AJ: Execute Job
    AJ->>AJ: Deserialize Arguments
    AJ->>Job: perform(args)

    alt Success
        Job-->>AJ: Completed
        AJ-->>Worker: Success
    else Failure
        Job-->>AJ: Exception
        AJ->>AJ: Retry Logic
        AJ->>Queue: Re-enqueue (if retries remaining)
    end
```

## ActionCable WebSocket通信図

```mermaid
sequenceDiagram
    participant Browser
    participant Cable as ActionCable Server
    participant Connection as Connection
    participant Channel as Channel
    participant Redis as Redis PubSub
    participant Other as Other Servers

    Browser->>Cable: WebSocket Connect
    Cable->>Connection: Establish Connection
    Connection->>Connection: Authenticate
    Connection-->>Browser: Welcome Message

    Browser->>Channel: Subscribe to Channel
    Channel->>Channel: subscribed callback
    Channel->>Redis: Subscribe to Stream
    Channel-->>Browser: Subscription Confirmed

    Note over Browser,Other: Real-time Communication

    Browser->>Channel: Perform Action
    Channel->>Channel: Action Method
    Channel->>Redis: Broadcast
    Redis->>Other: Publish to Other Servers
    Other->>Browser: Broadcast to Clients
    Channel->>Browser: Broadcast to Local Clients

    Browser->>Channel: Unsubscribe
    Channel->>Channel: unsubscribed callback
    Channel->>Redis: Unsubscribe from Stream
```

## ActiveStorage ファイルアップロードフロー図

```mermaid
sequenceDiagram
    participant Client
    participant Controller
    participant Model as ActiveRecord Model
    participant AS as ActiveStorage
    participant Service as Storage Service
    participant S3 as AWS S3/GCS

    alt Direct Upload
        Client->>AS: Request Direct Upload URL
        AS->>Service: Create Blob
        Service-->>AS: Signed URL
        AS-->>Client: Upload URL
        Client->>S3: Upload File Directly
        S3-->>Client: Upload Complete
        Client->>Controller: Submit Form with Blob ID
    else Standard Upload
        Client->>Controller: Upload File
        Controller->>Model: Attach File
        Model->>AS: Create Attachment
        AS->>Service: Upload to Service
        Service->>S3: Store File
        S3-->>Service: Success
        Service-->>AS: Blob Created
    end

    AS->>AS: Analyze Job (async)
    AS-->>Model: Attachment Complete
```

## レイヤー構成図

```mermaid
graph TB
    subgraph "Presentation Layer"
        ERB[ERB Templates]
        JSON[JSON Builders]
        HTML[HTML Helpers]
    end

    subgraph "Controller Layer"
        Base[ActionController::Base]
        API[ActionController::API]
        Metal[ActionController::Metal]
    end

    subgraph "Service Layer"
        Concerns[Controller Concerns]
        Services[Service Objects]
        Presenters[Presenters]
    end

    subgraph "Domain Layer"
        Models[ActiveRecord Models]
        ValueObjects[Value Objects]
        DomainLogic[Domain Logic]
    end

    subgraph "Data Access Layer"
        Adapters[Connection Adapters]
        QueryInterface[Query Interface]
        Migrations[Migrations]
    end

    subgraph "Infrastructure Layer"
        DB[(Database)]
        Cache[(Cache)]
        Storage[(File Storage)]
        Queue[(Job Queue)]
    end

    ERB --> Base
    JSON --> API
    HTML --> Base
    Base --> Concerns
    API --> Concerns
    Metal --> Services
    Concerns --> Models
    Services --> Models
    Presenters --> Models
    Models --> Adapters
    Models --> QueryInterface
    Adapters --> DB
    QueryInterface --> DB
    Models --> Cache
    Models --> Storage
    Models --> Queue
```

## 設定・起動フロー図

```mermaid
flowchart TD
    A[require config/boot.rb] --> B[Set up load paths]
    B --> C[require railties and engines]
    C --> D[Define Rails.application]
    D --> E[Run before_configuration callbacks]
    E --> F[Load config/environments/ENV.rb]
    F --> G[Run before_initialize callbacks]
    G --> H[Run Railtie initializers]
    H --> I[Run engine initializers]
    I --> J[Run application initializers]
    J --> K[Load config/initializers/*]
    K --> L[Build middleware stack]
    L --> M[Run to_prepare callbacks]
    M --> N{eager_load?}
    N -->|Yes| O[Run eager_load!]
    N -->|No| P[Skip eager loading]
    O --> Q[Run after_initialize callbacks]
    P --> Q
    Q --> R[Application Ready]
```
