# rails/activerecord-session_store architecture

> Architecture diagram of the GitHub repository rails/activerecord-session_store, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rails/activerecord-session_store
- Repository: https://github.com/rails/activerecord-session_store

## Overview

##### Project type and purpose
- This is a Ruby gem (library) that provides a session store for Rails apps using Active Record.
- Its main role is to hook into Rails’ middleware, serialize session data, and persist it in a database table.

##### File structure
- Top‐level: gemspec, Gemfile, Rakefile, CI config, docs (README, CONTRIBUTING, CHANGELOG).
- lib/…: the core code, split into two main namespaces:
- action_dispatch/session/active_record_store.rb – the middleware implementation
- active_record/session_store – the Session model, Railtie, SQLBypass variant, version file
- lib/generators/…: Rails generator for creating the sessions table migration
- lib/tasks/database.rake: Rake tasks for trimming and upgrading session data
- test/…: unit and integration tests for all components
- gemfiles/: compatibility matrix against different Rails versions

##### README for insights
- Describes how to install and configure the gem in a Rails app
- Lists configurable options (table name, serializer, secure_session_only flag)
- Mentions two provided session‐class implementations: the default AR model and the SQLBypass example
- Documents two Rake tasks: db:sessions:trim and db:sessions:upgrade

##### Detail without overthinking
- How they connect at runtime (data flow) and at build time (generator, rake)
- Keep styling consistent so a new contributor or user of this gem can instantly see where to hook in or debug

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Host Application
    subgraph "Rails Host Application"
        HostApp["Rails Application"]:::external
    end

    %% Gem Components
    subgraph "Session Store Gem"
        subgraph "Core Functionality"
            Middleware["ActionDispatch::Session::ActiveRecordStore"]:::core
            SessionModel["ActiveRecord::SessionStore::Session"]:::core
            SQLBypass["ActiveRecord::SessionStore::SQLBypass"]:::optional
        end
        subgraph "Integration Hooks"
            Railtie["Railtie"]:::hook
            Version["Version"]:::hook
            Entry["Gem Entry Point"]:::hook
        end
        subgraph "Generators"
            Generator["Migration Generator"]:::hook
            Template["Migration Template"]:::hook
        end
        subgraph "Rake Tasks"
            RakeTasks["db:sessions:trim / db:sessions:upgrade"]:::hook
        end
        Gemfile["Gemspec"]:::hook
    end

    %% Database
    DB["Sessions Table\n(PostgreSQL/MySQL/SQLite)"]:::db

    %% Relationships
    HostApp -->|boot| Entry
    Entry -->|requires| Railtie
    Railtie -->|registers middleware| Middleware
    Railtie -->|registers generator| Generator
    Railtie -->|registers tasks| RakeTasks
    Railtie -->|loads version| Version
    HostApp -->|HTTP request| Middleware
    Middleware -->|invokes| SessionModel
    SessionModel -->|executes SQL| DB
    SQLBypass -.->|alternate strategy| SessionModel
    Generator -->|uses template| Template
    Template -->|generates migration→creates table| DB
    RakeTasks -->|runs commands| SessionModel

    %% Click Events
    click Middleware "https://github.com/rails/activerecord-session_store/blob/master/lib/action_dispatch/session/active_record_store.rb"
    click SessionModel "https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/session.rb"
    click SQLBypass "https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/sql_bypass.rb"
    click Railtie "https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/railtie.rb"
    click Version "https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/version.rb"
    click Entry "https://github.com/rails/activerecord-session_store/blob/master/lib/activerecord/session_store.rb"
    click Generator "https://github.com/rails/activerecord-session_store/blob/master/lib/generators/active_record/session_migration_generator.rb"
    click Template "https://github.com/rails/activerecord-session_store/blob/master/lib/generators/active_record/templates/migration.rb"
    click RakeTasks "https://github.com/rails/activerecord-session_store/blob/master/lib/tasks/database.rake"
    click Gemfile "https://github.com/rails/activerecord-session_store/blob/master/activerecord-session_store.gemspec"

    %% Styles
    classDef core fill:#D0E8FF,stroke:#1E70BF,color:#000;
    classDef hook fill:#E2F8D0,stroke:#2C7A0B,color:#000;
    classDef optional fill:#FFF4C1,stroke:#B88A00,color:#000;
    classDef db fill:#C8F0FF,stroke:#0084A8,color:#000;
    classDef external fill:#F0F0F0,stroke:#888888,color:#000;
```
