generated from block/oss-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Replace CoPlan::UserModel concern with engine-owned CoPlan::User model #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
db/migrate/20260226200001_create_coplan_users_and_migrate.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| class CreateCoplanUsersAndMigrate < ActiveRecord::Migration[8.1] | ||
| def up | ||
| create_table :coplan_users, id: { type: :string, limit: 36 } do |t| | ||
| t.string :external_id, null: false | ||
| t.string :name, null: false | ||
| t.boolean :admin, default: false, null: false | ||
| t.json :metadata, default: {} | ||
| t.timestamps | ||
| end | ||
|
|
||
| add_index :coplan_users, :external_id, unique: true | ||
|
|
||
| # Migrate existing users — reuse the same id so FK columns stay valid | ||
| execute <<~SQL | ||
| INSERT INTO coplan_users (id, external_id, name, admin, metadata, created_at, updated_at) | ||
| SELECT id, id, name, (role = 'admin'), '{}', created_at, updated_at | ||
| FROM users | ||
| SQL | ||
|
|
||
| # Update foreign keys to point to coplan_users instead of users | ||
| remove_foreign_key :coplan_api_tokens, column: :user_id | ||
| remove_foreign_key :coplan_comment_threads, column: :created_by_user_id | ||
| remove_foreign_key :coplan_comment_threads, column: :resolved_by_user_id | ||
| remove_foreign_key :coplan_plan_collaborators, column: :user_id | ||
| remove_foreign_key :coplan_plan_collaborators, column: :added_by_user_id | ||
| remove_foreign_key :coplan_plans, column: :created_by_user_id | ||
|
|
||
| add_foreign_key :coplan_api_tokens, :coplan_users, column: :user_id | ||
| add_foreign_key :coplan_comment_threads, :coplan_users, column: :created_by_user_id | ||
| add_foreign_key :coplan_comment_threads, :coplan_users, column: :resolved_by_user_id | ||
| add_foreign_key :coplan_plan_collaborators, :coplan_users, column: :user_id | ||
| add_foreign_key :coplan_plan_collaborators, :coplan_users, column: :added_by_user_id | ||
| add_foreign_key :coplan_plans, :coplan_users, column: :created_by_user_id | ||
| end | ||
|
|
||
| def down | ||
| remove_foreign_key :coplan_api_tokens, column: :user_id | ||
| remove_foreign_key :coplan_comment_threads, column: :created_by_user_id | ||
| remove_foreign_key :coplan_comment_threads, column: :resolved_by_user_id | ||
| remove_foreign_key :coplan_plan_collaborators, column: :user_id | ||
| remove_foreign_key :coplan_plan_collaborators, column: :added_by_user_id | ||
| remove_foreign_key :coplan_plans, column: :created_by_user_id | ||
|
|
||
| add_foreign_key :coplan_api_tokens, :users, column: :user_id | ||
| add_foreign_key :coplan_comment_threads, :users, column: :created_by_user_id | ||
| add_foreign_key :coplan_comment_threads, :users, column: :resolved_by_user_id | ||
| add_foreign_key :coplan_plan_collaborators, :users, column: :user_id | ||
| add_foreign_key :coplan_plan_collaborators, :users, column: :added_by_user_id | ||
| add_foreign_key :coplan_plans, :users, column: :created_by_user_id | ||
|
|
||
| drop_table :coplan_users | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| # CoPlan Host App Integration Guide | ||
|
|
||
| ## Overview | ||
|
|
||
| CoPlan is a Rails engine that manages collaborative planning documents. It owns its own `CoPlan::User` model and handles authentication internally via a callback you configure. | ||
|
|
||
| ## Setup | ||
|
|
||
| ### 1. Mount the engine | ||
|
|
||
| ```ruby | ||
| # config/routes.rb | ||
| mount CoPlan::Engine, at: "/coplan" | ||
| ``` | ||
|
|
||
| ### 2. Install migrations | ||
|
|
||
| ```bash | ||
| bin/rails coplan:install:migrations | ||
| bin/rails db:migrate | ||
| ``` | ||
|
|
||
| This creates the engine's tables (`coplan_users`, `coplan_plans`, etc.) in your database. | ||
|
|
||
| ### 3. Configure authentication | ||
|
|
||
| Provide an `authenticate` callback that receives a Rack request and returns user identity attributes (or `nil` if unauthenticated): | ||
|
|
||
| ```ruby | ||
| # config/initializers/coplan.rb | ||
| CoPlan.configure do |config| | ||
| config.authenticate = ->(request) { | ||
| # Example: session-based auth | ||
| user_id = request.session[:user_id] | ||
| return nil unless user_id | ||
|
|
||
| user = User.find_by(id: user_id) | ||
| return nil unless user | ||
|
|
||
| { | ||
| external_id: user.id.to_s, # required — unique ID from your auth system | ||
| name: user.name, # required — display name | ||
| admin: user.admin?, # optional — can manage CoPlan settings (default: false) | ||
| metadata: {} # optional — arbitrary data (default: {}) | ||
| } | ||
| } | ||
|
|
||
| # Optional: AI provider configuration | ||
| config.ai_api_key = ENV["OPENAI_API_KEY"] | ||
| config.ai_model = "gpt-4o" | ||
| end | ||
| ``` | ||
|
|
||
| The callback is called on every CoPlan request. The engine automatically finds or creates a `CoPlan::User` from the returned attributes, keeping the name and admin flag in sync. | ||
|
|
||
| ### Callback return values | ||
|
|
||
| | Key | Type | Required | Description | | ||
| |---------------|---------|----------|-------------| | ||
| | `external_id` | String | Yes | Unique identifier from your auth system | | ||
| | `name` | String | Yes | Display name | | ||
| | `admin` | Boolean | No | Can manage reviewers, settings (default: `false`) | | ||
| | `metadata` | Hash | No | Arbitrary data stored as JSON (default: `{}`) | | ||
|
|
||
| Return `nil` to indicate the user is not authenticated (the engine will respond with `401 Unauthorized`). | ||
|
|
||
| ## Authentication examples | ||
|
|
||
| ### Trogdor (Square internal) | ||
|
|
||
| ```ruby | ||
| config.authenticate = ->(request) { | ||
| trogdor = Rails::Auth.credentials(request.env)["trogdor"] | ||
| return nil unless trogdor | ||
|
|
||
| { | ||
| external_id: trogdor.uid.to_s, | ||
| name: trogdor.username, | ||
| admin: trogdor.capabilities.include?("owners") | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Devise | ||
|
|
||
| ```ruby | ||
| config.authenticate = ->(request) { | ||
| env = request.env | ||
| warden = env["warden"] | ||
| user = warden&.user | ||
| return nil unless user | ||
|
|
||
| { | ||
| external_id: user.id.to_s, | ||
| name: user.name, | ||
| admin: user.admin? | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## CoPlan::User model | ||
|
|
||
| The engine manages a `coplan_users` table with these columns: | ||
|
|
||
| | Column | Type | Description | | ||
| |---------------|---------|-------------| | ||
| | `id` | String | UUIDv7 primary key (auto-assigned) | | ||
| | `external_id` | String | Unique ID from your auth system | | ||
| | `name` | String | Display name | | ||
| | `admin` | Boolean | Admin flag | | ||
| | `metadata` | JSON | Extensible data bag | | ||
|
|
||
| `CoPlan::User` is a normal ActiveRecord model. Host apps can reference it directly: | ||
|
|
||
| ```ruby | ||
| class Notification < ApplicationRecord | ||
| belongs_to :user, class_name: "CoPlan::User" | ||
| end | ||
| ``` | ||
|
|
||
| ## API tokens | ||
|
|
||
| CoPlan provides a REST API for programmatic access. Users create API tokens in the Settings UI. API requests authenticate via `Authorization: Bearer <token>` headers — no session or callback required. | ||
|
|
||
| ## Layout and sign-out | ||
|
|
||
| CoPlan inherits from your `::ApplicationController` for layout and middleware. The engine's nav bar will render a "Sign out" link if your app defines a `sign_out_path` route helper. | ||
|
|
||
| ## Configuration reference | ||
|
|
||
| ```ruby | ||
| CoPlan.configure do |config| | ||
| # Required | ||
| config.authenticate = ->(request) { ... } | ||
|
|
||
| # AI provider (optional) | ||
| config.ai_base_url = "https://api.openai.com/v1" # default | ||
| config.ai_api_key = nil | ||
| config.ai_model = "gpt-4o" # default | ||
|
|
||
| # Error reporting (optional) | ||
| config.error_reporter = ->(exception, context) { | ||
| Rails.error.report(exception, context: context) # default | ||
| } | ||
|
|
||
| # Notifications (optional) | ||
| config.notification_handler = ->(event, payload) { ... } | ||
| end | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
downpath re-adds FKs tousersimmediately, but afterupruns, newly provisionedcoplan_usersno longer share IDs with hostusers(onlyexternal_idmatches). If any post-migration data references those newcoplan_users.idvalues (e.g.,coplan_plans.created_by_user_id),add_foreign_key ... :usersfails with FK violations and blocks rollback during an incident. The rollback needs a data remap from CoPlan IDs back to host user IDs (viaexternal_id) or should be marked irreversible.Useful? React with 👍 / 👎.