command_tower 0.17.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -8
  3. data/app/controllers/command_tower/me/inbox_controller.rb +16 -0
  4. data/app/deserializers/command_tower/deserializers/messaging/inbox.rb +85 -0
  5. data/app/deserializers/command_tower/deserializers/messaging/preferences/update_deserializer.rb +10 -1
  6. data/app/errors/command_tower/errors/account/push_test_destination_error.rb +21 -0
  7. data/app/jobs/command_tower/messaging/communications/produce_recipient_job.rb +2 -0
  8. data/app/models/command_tower/messaging/communication.rb +31 -0
  9. data/app/models/command_tower/messaging/inbox_item.rb +6 -0
  10. data/app/serializers/command_tower/serializers/messaging/inbox.rb +44 -1
  11. data/app/serializers/command_tower/serializers/messaging/preferences/notification_serializer.rb +2 -0
  12. data/app/services/command_tower/messaging/accept/coordinator.rb +16 -1
  13. data/app/services/command_tower/messaging/accept/persister.rb +13 -0
  14. data/app/services/command_tower/messaging/contract/mappers/communication_mapper.rb +7 -0
  15. data/app/services/command_tower/messaging/contract/results/communication_result.rb +1 -0
  16. data/app/services/command_tower/messaging/execution/adapter_result.rb +11 -1
  17. data/app/services/command_tower/messaging/execution/adapters/expo/adapter.rb +146 -25
  18. data/app/services/command_tower/messaging/execution/adapters/expo/configuration.rb +5 -0
  19. data/app/services/command_tower/messaging/execution/adapters/expo/endpoint_ticket_history.rb +86 -0
  20. data/app/services/command_tower/messaging/execution/adapters/expo/interruption_level.rb +41 -0
  21. data/app/services/command_tower/messaging/execution/attempt_ledger.rb +3 -0
  22. data/app/services/command_tower/messaging/execution/expo_receipt_collection.rb +224 -0
  23. data/app/services/command_tower/messaging/execution/operation_logger.rb +55 -5
  24. data/app/services/command_tower/messaging/execution/recovery.rb +8 -0
  25. data/app/services/command_tower/messaging/inbox/conversation_result.rb +31 -0
  26. data/app/services/command_tower/messaging/inbox/entry_result.rb +36 -0
  27. data/app/services/command_tower/messaging/inbox/reader.rb +246 -20
  28. data/app/services/command_tower/messaging/inbox/unread_count_result.rb +3 -2
  29. data/app/services/command_tower/messaging/inbox.rb +4 -0
  30. data/app/services/command_tower/messaging/notification_types/declaration.rb +19 -1
  31. data/app/services/command_tower/messaging/notification_types/declaration_validator.rb +62 -0
  32. data/app/services/command_tower/messaging/notification_types/permitted_choice.rb +55 -0
  33. data/app/services/command_tower/messaging/notification_types/registry.rb +25 -0
  34. data/app/services/command_tower/messaging/notification_types.rb +3 -0
  35. data/app/services/command_tower/messaging/preferences/catalog.rb +7 -0
  36. data/app/services/command_tower/messaging/preferences/catalog_notification_result.rb +26 -1
  37. data/app/services/command_tower/messaging/preferences/effective_choice.rb +92 -0
  38. data/app/services/command_tower/messaging/preferences/preference_state.rb +24 -4
  39. data/app/services/command_tower/messaging/preferences/store.rb +10 -0
  40. data/app/services/command_tower/messaging/preferences/update.rb +36 -4
  41. data/app/services/command_tower/messaging/rendering/channel_renderer.rb +63 -3
  42. data/app/services/command_tower/messaging/rendering/inbox_document.rb +27 -0
  43. data/app/services/command_tower/messaging/rendering/inbox_document_renderer.rb +53 -36
  44. data/app/services/command_tower/messaging/rendering/inbox_presentation_resolver.rb +52 -0
  45. data/app/services/command_tower/messaging/rendering/inbox_presentation_snapshot.rb +64 -0
  46. data/app/services/command_tower/messaging/rendering/push_presentation.rb +90 -0
  47. data/app/services/command_tower/messaging/rendering/rendered_push_payload.rb +54 -6
  48. data/app/services/command_tower/messaging/sound_catalog.rb +85 -0
  49. data/app/services/command_tower/messaging.rb +5 -1
  50. data/app/services/command_tower/services/account/push/send_self_test.rb +7 -1
  51. data/app/services/command_tower/services/messaging/communications/produce.rb +31 -0
  52. data/app/services/command_tower/services/messaging/communications/produce_many.rb +6 -0
  53. data/app/services/command_tower/services/messaging/inbox.rb +52 -2
  54. data/app/workflows/command_tower/workflows/me/error_mapping.rb +1 -0
  55. data/app/workflows/command_tower/workflows/messaging/communications/produce_recipient_workflow.rb +5 -1
  56. data/app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb +5 -1
  57. data/app/workflows/command_tower/workflows/messaging/inbox.rb +15 -1
  58. data/config/routes.rb +1 -0
  59. data/db/migrate/20261010000001_add_inbox_presentation_snapshot_to_messaging_communications.rb +7 -0
  60. data/db/migrate/20261010000002_add_conversation_identity_to_messaging_communications.rb +10 -0
  61. data/db/migrate/20261012000001_add_provider_metadata_to_messaging_delivery_attempts.rb +7 -0
  62. data/db/migrate/20261012000002_add_finished_at_index_to_messaging_delivery_attempts.rb +8 -0
  63. data/docs/authorization.md +1 -1
  64. data/docs/bootstrap/00-ownership.md +77 -0
  65. data/docs/bootstrap/01-docker-make-compose.md +267 -0
  66. data/docs/bootstrap/02-create-the-rails-app.md +75 -0
  67. data/docs/bootstrap/03-pin-the-gem.md +53 -0
  68. data/docs/bootstrap/04-secrets-and-env.md +59 -0
  69. data/docs/bootstrap/05-install-migrate-doctor.md +74 -0
  70. data/docs/bootstrap/06-mount-and-health.md +53 -0
  71. data/docs/bootstrap/07-execution-bases.md +38 -0
  72. data/docs/bootstrap/08-initializer.md +90 -0
  73. data/docs/bootstrap/09-rbac.md +83 -0
  74. data/docs/bootstrap/10-roles-and-gates.md +62 -0
  75. data/docs/bootstrap/11-auth-client-path.md +57 -0
  76. data/docs/bootstrap/12-smoke-check.md +81 -0
  77. data/docs/bootstrap/13-optional.md +50 -0
  78. data/docs/bootstrap/14-sanity-checks.md +44 -0
  79. data/docs/bootstrap/README.md +90 -0
  80. data/docs/cookie_authentication_guide.md +11 -0
  81. data/docs/extending.md +3 -3
  82. data/docs/host_integration_guide.md +3 -281
  83. data/docs/initializing.md +37 -28
  84. data/docs/messaging_integration_guide.md +1 -1
  85. data/docs/principal_capabilities.md +1 -1
  86. data/docs/upgrades/0.10.0.md +5 -5
  87. data/docs/upgrades/0.11.0.md +5 -5
  88. data/docs/upgrades/0.18.0.md +35 -0
  89. data/docs/upgrades/0.19.0.md +40 -0
  90. data/docs/upgrades/README.md +3 -1
  91. data/lib/command_tower/authorization/default.yml +1 -0
  92. data/lib/command_tower/configuration/messaging/config.rb +10 -0
  93. data/lib/command_tower/configuration/messaging/expo.rb +61 -0
  94. data/lib/command_tower/install/baseline.rb +4 -0
  95. data/lib/command_tower/version.rb +1 -1
  96. metadata +36 -2
@@ -34,7 +34,7 @@ module CommandTower
34
34
  def call
35
35
  with_recipient do |recipient_id|
36
36
  result = CommandTower::Messaging::Inbox.list(recipient_id:, limit:, offset:, scope:)
37
- context.items = result.items.map { |item| Inbox.item(item) }
37
+ context.items = result.items.map { |entry| Inbox.entry(entry) }
38
38
  context.pagination = { limit: result.limit, offset: result.offset, total_count: result.total_count }
39
39
  end
40
40
  end
@@ -80,12 +80,47 @@ module CommandTower
80
80
  end
81
81
  end
82
82
 
83
+ class Conversation < CommandTower::Services::ApplicationService
84
+ include FailureMapping
85
+ validate :user, is_a: User, required: true
86
+ validate :key, is_a: String, required: true
87
+ validate :scope, is_a: String, required: false, default: "inbox"
88
+ validate :around, is_a: [String, Integer], required: false
89
+ validate :before_id, is_a: Integer, required: false
90
+ validate :after_id, is_a: Integer, required: false
91
+ validate :limit, is_a: Integer, required: false
92
+
93
+ def call
94
+ with_recipient do |recipient_id|
95
+ found = CommandTower::Messaging::Inbox.conversation(
96
+ recipient_id:, key:, scope:, around:, before_id:, after_id:, limit:,
97
+ )
98
+ messages = found.message_ids.map do |inbox_item_id|
99
+ Inbox.detail(recipient_id:, inbox_item_id:)
100
+ end
101
+ context.conversation = {
102
+ scope: found.scope,
103
+ subject: found.subject,
104
+ message_count: found.message_count,
105
+ unread_count: found.unread_count,
106
+ inbox_item_ids: found.inbox_item_ids,
107
+ messages:,
108
+ window: found.window,
109
+ }
110
+ end
111
+ end
112
+ end
113
+
83
114
  class UnreadCount < CommandTower::Services::ApplicationService
84
115
  include FailureMapping
85
116
  validate :user, is_a: User, required: true
86
117
 
87
118
  def call
88
- with_recipient { |recipient_id| context.count = CommandTower::Messaging::Inbox.unread_count(recipient_id:).count }
119
+ with_recipient do |recipient_id|
120
+ result = CommandTower::Messaging::Inbox.unread_count(recipient_id:)
121
+ context.count = result.count
122
+ context.badge_count = result.badge_count
123
+ end
89
124
  end
90
125
  end
91
126
 
@@ -115,6 +150,21 @@ module CommandTower
115
150
  { id: item.id, title: item.title, status: item.status, viewed_at: item.viewed_at, created_at: item.created_at, updated_at: item.updated_at }
116
151
  end
117
152
 
153
+ def entry(entry)
154
+ {
155
+ kind: entry.kind,
156
+ id: entry.id,
157
+ title: entry.title,
158
+ preview: entry.preview,
159
+ activity_at: entry.activity_at,
160
+ created_at: entry.created_at,
161
+ message_count: entry.message_count,
162
+ unread_count: entry.unread_count,
163
+ read: entry.read,
164
+ inbox_item_ids: entry.inbox_item_ids,
165
+ }
166
+ end
167
+
118
168
  def detail(recipient_id:, inbox_item_id:)
119
169
  item = CommandTower::Messaging::Inbox.show(recipient_id:, inbox_item_id:)
120
170
  communication = CommandTower::Messaging::Contract::Communications.find(
@@ -26,6 +26,7 @@ module CommandTower
26
26
  CommandTower::Errors::Account::PushoverVerificationFailedError,
27
27
  CommandTower::Errors::Account::PushEndpointNotFoundError,
28
28
  CommandTower::Errors::Account::PushTestNoEndpointError,
29
+ CommandTower::Errors::Account::PushTestDestinationError,
29
30
  CommandTower::Errors::ValidationError
30
31
  :unprocessable_entity
31
32
  when CommandTower::Errors::Account::PhoneVerificationThrottledError,
@@ -15,7 +15,9 @@ module CommandTower
15
15
  title:,
16
16
  body:,
17
17
  platform_enabled_channels:,
18
- metadata: nil
18
+ metadata: nil,
19
+ conversation_key: nil,
20
+ conversation_title: nil
19
21
  )
20
22
  user = User.find_by(id: user_id)
21
23
  unless user
@@ -34,6 +36,8 @@ module CommandTower
34
36
  body:,
35
37
  metadata:,
36
38
  platform_enabled_channels:,
39
+ conversation_key:,
40
+ conversation_title:,
37
41
  )
38
42
 
39
43
  unless result.success?
@@ -154,7 +154,7 @@ module CommandTower
154
154
 
155
155
  def load_delivery(channel_delivery_id)
156
156
  CommandTower::Messaging::ChannelDelivery.includes(
157
- communication: %i[user destination_plan],
157
+ communication: %i[user destination_plan inbox_item],
158
158
  ).find_by(id: channel_delivery_id)
159
159
  end
160
160
 
@@ -172,6 +172,7 @@ module CommandTower
172
172
  communication:,
173
173
  channel_key: delivery.channel_key,
174
174
  recipient_address:,
175
+ channel_delivery: delivery,
175
176
  )
176
177
 
177
178
  CommandTower::Messaging::Execution::OperationLogger.render_succeeded(
@@ -204,18 +205,21 @@ module CommandTower
204
205
  CommandTower::Messaging::Execution::OperationLogger.adapter_accepted(
205
206
  channel_delivery: delivery,
206
207
  delivery_attempt: attempt,
208
+ provider_metadata: result.provider_metadata,
207
209
  )
208
210
  elsif result.retryable_failure?
209
211
  CommandTower::Messaging::Execution::OperationLogger.adapter_retryable(
210
212
  channel_delivery: delivery,
211
213
  delivery_attempt: attempt,
212
214
  error_code: result.error_code,
215
+ provider_metadata: result.provider_metadata,
213
216
  )
214
217
  else
215
218
  CommandTower::Messaging::Execution::OperationLogger.adapter_terminal(
216
219
  channel_delivery: delivery,
217
220
  delivery_attempt: attempt,
218
221
  error_code: result.error_code,
222
+ provider_metadata: result.provider_metadata,
219
223
  )
220
224
  end
221
225
  end
@@ -26,12 +26,26 @@ module CommandTower
26
26
  result = CommandTower::Services::Messaging::Inbox::List.call(user:, limit:, offset:, scope:)
27
27
  return result_or_failure(result) unless result.success?
28
28
 
29
- payload = result.data[:items].map { |item| CommandTower::Serializers::Messaging::Inbox::ItemSerializer.serialize(item) }
29
+ payload = result.data[:items].map { |item| CommandTower::Serializers::Messaging::Inbox::EntrySerializer.serialize(item) }
30
30
  meta = CommandTower::Serializers::Messaging::Inbox::PaginationMetaSerializer.serialize(result.data[:pagination])
31
31
  success(payload:, meta:, http_status: :ok)
32
32
  end
33
33
  end
34
34
 
35
+ class ConversationWorkflow < BaseWorkflow
36
+ def call(user:, key:, scope:, around: nil, before_id: nil, after_id: nil, limit: nil)
37
+ result = CommandTower::Services::Messaging::Inbox::Conversation.call(
38
+ user:, key:, scope:, around:, before_id:, after_id:, limit:,
39
+ )
40
+ return result_or_failure(result) unless result.success?
41
+
42
+ success(
43
+ payload: CommandTower::Serializers::Messaging::Inbox::ConversationSerializer.serialize(result.data[:conversation]),
44
+ http_status: :ok,
45
+ )
46
+ end
47
+ end
48
+
35
49
  class ShowWorkflow < BaseWorkflow
36
50
  def call(user:, inbox_item_id:)
37
51
  result = CommandTower::Services::Messaging::Inbox::Show.call(user:, inbox_item_id:)
data/config/routes.rb CHANGED
@@ -64,6 +64,7 @@ CommandTower::Engine.routes.draw do
64
64
 
65
65
  resources :inbox, only: [:index, :show, :destroy], controller: "inbox" do
66
66
  collection do
67
+ get :conversation
67
68
  get :unread_count, path: "unread-count"
68
69
  post "bulk/read", action: :bulk_read
69
70
  post "bulk/unread", action: :bulk_unread
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddInboxPresentationSnapshotToMessagingCommunications < ActiveRecord::Migration[7.2]
4
+ def change
5
+ add_column :messaging_communications, :inbox_presentation_snapshot, :json
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddConversationIdentityToMessagingCommunications < ActiveRecord::Migration[7.2]
4
+ def change
5
+ add_column :messaging_communications, :conversation_key, :string, limit: 128
6
+ add_column :messaging_communications, :conversation_title, :string
7
+ add_index :messaging_communications, %i[user_id conversation_key],
8
+ name: "index_messaging_communications_on_user_and_conversation_key"
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddProviderMetadataToMessagingDeliveryAttempts < ActiveRecord::Migration[7.2]
4
+ def change
5
+ add_column :messaging_delivery_attempts, :provider_metadata, :json
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddFinishedAtIndexToMessagingDeliveryAttempts < ActiveRecord::Migration[7.2]
4
+ def change
5
+ add_index :messaging_delivery_attempts, :finished_at,
6
+ name: "index_messaging_delivery_attempts_on_finished_at"
7
+ end
8
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Authorization establishes **permission** after authentication. Failed authorization returns `403`.
4
4
 
5
- Host `rbac_groups.yml` is a **required integration step** (Step 4) — see [Host integration](host_integration_guide.md#step-4--host-rbac-required). CommandTower ships CT-owned entity definitions. Hosts grant those names to product roles; they must not copy CT controller mappings. Without a host role that grants Me/Auth entities, authenticated calls return **403**.
5
+ Host `rbac_groups.yml` is a **required integration step** — see [Bootstrap — RBAC](bootstrap/09-rbac.md). CommandTower ships CT-owned entity definitions. Hosts grant those names to product roles; they must not copy CT controller mappings. Without a host role that grants Me/Auth entities, authenticated calls return **403**.
6
6
 
7
7
  ## Quick usage (host provisional)
8
8
 
@@ -0,0 +1,77 @@
1
+ # 00 — Ownership
2
+
3
+ ## Purpose
4
+
5
+ Know what CommandTower owns vs what the host owns **before** you generate a Rails app. Wrong ownership here produces forked platform code and duplicate schema.
6
+
7
+ ## Prerequisites
8
+
9
+ Read [`README.md`](README.md). This step is conceptual; no files yet.
10
+
11
+ ## Engine vs host
12
+
13
+ CommandTower is a **mountable Rails engine**. The host is a Rails API application that:
14
+
15
+ - Depends on the `command_tower` gem
16
+ - Runs install/migrate/doctor **inside Compose**
17
+ - Mounts the engine
18
+ - Configures secrets, RBAC composition, and feature gates
19
+ - Adds **product** routes, workflows, and schema
20
+
21
+ The engine owns:
22
+
23
+ - CommandTower-owned schema (users, sessions, JWT-related tables, Me/Auth, messaging platform tables, and the rest of the engine `db/migrate`)
24
+ - Platform HTTP under the mount
25
+ - Workflow / service / serializer framework
26
+ - Default entity catalog (`lib/command_tower/authorization/default.yml`)
27
+ - Shared FactoryBot definitions
28
+
29
+ The host owns:
30
+
31
+ - `Dockerfile`, `docker-compose.yml`, `Makefile`
32
+ - Product schema, product routes, product workflows
33
+ - `config/initializers/command_tower.rb` after generate
34
+ - `config/rbac_groups.yml` (grants of **CT entity names** plus host-owned entities)
35
+ - Secrets in Compose / env
36
+ - Feature-gate choices
37
+ - Messaging catalogs and adapter credentials (optional)
38
+
39
+ ## Schema (AD-SCH-01)
40
+
41
+ CommandTower is the **sole authoring authority** for CommandTower-owned schema. Hosts must **not** manually write, edit, or copy-paste CommandTower schema migrations.
42
+
43
+ Hosts install copies via `command_tower:install` / `command_tower:install:migrations`, then `db:migrate`. Installed `*.command_tower.rb` files are an **execution context**, not a second authoring home.
44
+
45
+ Contract: [`../initializing.md`](../initializing.md).
46
+
47
+ ## Workflow layer
48
+
49
+ Meaningful business behavior enters through a **workflow**. Controllers, jobs, and scheduled tasks do not call services as a full business action. Workflows do not call other workflows (extract a shared sequence). See [`../architecture.md`](../architecture.md).
50
+
51
+ Do not invent a parallel “manager” layer.
52
+
53
+ ## Dummy `rails_app` is not a product template
54
+
55
+ [`../../rails_app/`](../../rails_app/) is the **in-engine dummy host** used to develop and test the gem. Later steps cite its initializer and `rbac_groups.yml` for **shape**.
56
+
57
+ Do not copy `rails_app/` as a new product. Do not copy this gem’s [`docker-compose.yaml`](../../docker-compose.yaml) (`mysql:latest`, engine ports) — that file is for **engine development**, not a product host.
58
+
59
+ This tree writes **generic** Makefile / Compose templates in [`01-docker-make-compose.md`](01-docker-make-compose.md).
60
+
61
+ ## Docker-only
62
+
63
+ No Ruby or Rails on the host OS. See the README **Docker-only** section. Step 01 exists so `rails new` never runs on the Mac.
64
+
65
+ ## What a platform-proof host is
66
+
67
+ Enough to:
68
+
69
+ 1. Boot in Compose
70
+ 2. Pass `make doctor`
71
+ 3. Register, log in, and receive **200** from `GET /me` as a `member`
72
+
73
+ Product domain, Solid Queue workers, SMS/Pushover adapters, and admin RBAC bundles are **not** required to finish this manual.
74
+
75
+ ## Stop
76
+
77
+ You can explain engine vs host, AD-SCH-01, and why Docker exists before `rails new`. Next: [`01-docker-make-compose.md`](01-docker-make-compose.md).
@@ -0,0 +1,267 @@
1
+ # 01 — Docker, Make, Compose
2
+
3
+ ## Purpose
4
+
5
+ Author the local toolchain **before** any Rails app exists. After this step you can `make help` and `make build` on an empty repository. Ruby is only inside the image.
6
+
7
+ ## Prerequisites
8
+
9
+ - Docker Desktop (or equivalent) with the Compose plugin
10
+ - Make
11
+ - Git
12
+ - Empty (or nearly empty) host repository
13
+
14
+ Do **not** install Ruby, Bundler, or Rails on the Mac.
15
+
16
+ ## Files the host creates
17
+
18
+ | File | Role |
19
+ |------|------|
20
+ | `Dockerfile` | Ruby image: Bundler, MariaDB client, Node (asset/JS tooling if needed) |
21
+ | `docker-compose.yml` | `api`, MariaDB **10.11+**, Redis |
22
+ | `Makefile` | Operator surface: `build`, `rails-new`, `rails`, `migrate`, `doctor`, … |
23
+ | `.dockerignore` | Keep the build context small |
24
+ | `.env` (gitignored) | Local secrets later ([`04-secrets-and-env.md`](04-secrets-and-env.md)); create a stub if Compose interpolates vars |
25
+
26
+ The database in **this recipe** is MariaDB 10.11+ with `utf8mb4` / `utf8mb4_unicode_ci`. That is a host choice documented here. Do not copy the gem’s engine-dev compose (`mysql:latest`).
27
+
28
+ Redis is required for CommandTower cache/features that expect it. Optional `worker` is only if the host uses Solid Queue (add after the app exists).
29
+
30
+ ## Procedure
31
+
32
+ ### 1. `.dockerignore`
33
+
34
+ ```
35
+ .git
36
+ log
37
+ tmp
38
+ .env
39
+ coverage
40
+ vendor/bundle
41
+ node_modules
42
+ .DS_Store
43
+ ```
44
+
45
+ ### 2. `Dockerfile`
46
+
47
+ Use an MRI image CommandTower’s doctor accepts (Rails **>= 7 and < 9**). A current floor that matches engine CI is Ruby **4.0.x** and Rails **8.1**. Do not freeze an older patch than the gem you will pin in step 03.
48
+
49
+ The image must **bind Puma on `0.0.0.0`**. Binding `127.0.0.1` inside the container is not reachable from the Mac.
50
+
51
+ ```dockerfile
52
+ FROM ruby:4.0.1
53
+
54
+ ENV RAILS_LOG_TO_STDOUT=1
55
+ ENV BUNDLE_PATH=/usr/local/bundle
56
+ ENV BUNDLE_JOBS=4
57
+
58
+ RUN apt-get update -qq && apt-get install --no-install-recommends -y \
59
+ build-essential \
60
+ git \
61
+ default-libmysqlclient-dev \
62
+ default-mysql-client \
63
+ libyaml-dev \
64
+ pkg-config \
65
+ redis-tools \
66
+ && rm -rf /var/lib/apt/lists/*
67
+
68
+ WORKDIR /app
69
+
70
+ # Gemfile does not exist until `make rails-new`. Copy the context; install gems when present.
71
+ COPY . .
72
+ RUN if [ -f Gemfile ]; then bundle install; fi
73
+
74
+ EXPOSE 3000
75
+ CMD ["bundle", "exec", "bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]
76
+ ```
77
+
78
+ Until [`02-create-the-rails-app.md`](02-create-the-rails-app.md) runs, there is **no** `Gemfile`. That is expected. `make build` still produces an image with Ruby, Bundler, and the MariaDB client. `make rails-new` uses that image.
79
+
80
+ After `rails new` you will have a Gemfile; rebuild so `bundle install` during image build caches gems. Day-to-day `make bundle` still refreshes gems in the running volume.
81
+
82
+ ### 3. `docker-compose.yml`
83
+
84
+ ```yaml
85
+ services:
86
+ api:
87
+ build: .
88
+ working_dir: /app
89
+ command: bundle exec bin/rails server -b 0.0.0.0 -p 3000
90
+ volumes:
91
+ - .:/app
92
+ - bundle_cache:/usr/local/bundle
93
+ ports:
94
+ - "3000:3000"
95
+ environment:
96
+ RAILS_ENV: development
97
+ DATABASE_URL: mysql2://app:app@db:3306/app_development
98
+ REDIS_URL: redis://redis:6379/0
99
+ SECRET_KEY_BASE: ${SECRET_KEY_BASE:-dev-secret-key-base-change-me}
100
+ SIGNUP_SESSION_JWT_SECRET: ${SIGNUP_SESSION_JWT_SECRET:-dev-signup-session-change-me}
101
+ PASSWORD_RECOVERY_SESSION_JWT_SECRET: ${PASSWORD_RECOVERY_SESSION_JWT_SECRET:-dev-password-recovery-change-me}
102
+ CORS_ALLOWED_ORIGINS: ${CORS_ALLOWED_ORIGINS:-http://localhost:8081,http://localhost:8082,http://localhost:19006}
103
+ depends_on:
104
+ db:
105
+ condition: service_healthy
106
+ redis:
107
+ condition: service_started
108
+ stdin_open: true
109
+ tty: true
110
+
111
+ db:
112
+ image: mariadb:10.11
113
+ environment:
114
+ MARIADB_ROOT_PASSWORD: root
115
+ MARIADB_DATABASE: app_development
116
+ MARIADB_USER: app
117
+ MARIADB_PASSWORD: app
118
+ ports:
119
+ - "3306:3306"
120
+ volumes:
121
+ - mariadb_data:/var/lib/mysql
122
+ command:
123
+ - --character-set-server=utf8mb4
124
+ - --collation-server=utf8mb4_unicode_ci
125
+ healthcheck:
126
+ test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
127
+ interval: 5s
128
+ timeout: 5s
129
+ retries: 20
130
+
131
+ redis:
132
+ image: redis:7-alpine
133
+ ports:
134
+ - "6379:6379"
135
+
136
+ volumes:
137
+ bundle_cache:
138
+ mariadb_data:
139
+ ```
140
+
141
+ Rename `app_development` / user / password to product names when you have them. Keep utf8mb4.
142
+
143
+ `command:` (and the Dockerfile `CMD`) must use `-b 0.0.0.0`. Without it, `make server` is not reachable from the Mac or from an Expo app on the host OS.
144
+
145
+ Published port **3000** is this recipe’s default for a single local API. The frontend `HostConfig` API origin must use the **same** host-published port. A second Command Tower API on this machine publishes a different host port. Engine-dev compose in this gem uses different ports; ignore it.
146
+
147
+ `CORS_ALLOWED_ORIGINS` in the sample (`8081`, `8082`, `19006`) is one web product’s origins. A second local web app uses its own Expo port. Change that origin, this API’s published port, and `config.application.url` (step 08) together. The list is consumed in [`11-auth-client-path.md`](11-auth-client-path.md). `curl` on the Mac does not need CORS.
148
+
149
+ ### 4. `Makefile`
150
+
151
+ Generic operator surface. `COMPOSE` is the only Compose invocation.
152
+
153
+ ```makefile
154
+ .PHONY: help build bundle setup fresh rebuild rails-new rails db-prepare migrate doctor \
155
+ bash console runner server s halt down down-v rspec test spec rubocop
156
+
157
+ COMPOSE := docker compose
158
+ API := $(COMPOSE) run --rm api
159
+ API_T := $(COMPOSE) run --rm -T api
160
+
161
+ help:
162
+ @echo "build Build the api image"
163
+ @echo "bundle bundle install in the container"
164
+ @echo "setup build + db-prepare"
165
+ @echo "fresh down-v + setup"
166
+ @echo "rebuild build --no-cache + bundle"
167
+ @echo "rails-new rails new (API) inside the container"
168
+ @echo "rails Forward to bin/rails (ARGS=...)"
169
+ @echo "db-prepare db:prepare"
170
+ @echo "migrate db:migrate"
171
+ @echo "doctor command_tower:doctor"
172
+ @echo "bash Shell in api"
173
+ @echo "console rails console"
174
+ @echo "runner rails runner CMD='...'"
175
+ @echo "server / s Foreground api server"
176
+ @echo "halt Stop compose"
177
+ @echo "down compose down"
178
+ @echo "down-v compose down -v (destroys DB volume)"
179
+ @echo "rspec/test rspec"
180
+ @echo "spec rspec SPEC=path"
181
+ @echo "rubocop rubocop"
182
+
183
+ build:
184
+ $(COMPOSE) build api
185
+
186
+ bundle:
187
+ $(API) bundle install
188
+
189
+ setup: build db-prepare
190
+
191
+ fresh: down-v setup
192
+
193
+ rebuild:
194
+ $(COMPOSE) build --no-cache api
195
+ $(MAKE) bundle
196
+
197
+ # Chicken-and-egg: image has Ruby; Gemfile may not exist yet.
198
+ # Doctor accepts Rails >= 7 and < 9. Prefer 8.1 to match current engine CI.
199
+ RAILS_NEW_VERSION ?= ~> 8.1.0
200
+ rails-new:
201
+ $(API) bash -lc 'gem install rails -v "$(RAILS_NEW_VERSION)" --no-document && rails new . --api --database=mysql --skip-git --force'
202
+
203
+ rails:
204
+ $(COMPOSE) run --rm -e SKIP_MOUNT -e SKIP_CONFIGURE -e FORCE api bundle exec bin/rails $(ARGS)
205
+
206
+ db-prepare:
207
+ $(API) bundle exec bin/rails db:prepare
208
+
209
+ migrate:
210
+ $(API) bundle exec bin/rails db:migrate
211
+
212
+ doctor:
213
+ $(API) bundle exec bin/rails command_tower:doctor
214
+
215
+ bash:
216
+ $(COMPOSE) run --rm api bash
217
+
218
+ console:
219
+ $(API) bundle exec bin/rails console
220
+
221
+ runner:
222
+ $(API) bundle exec bin/rails runner "$(CMD)"
223
+
224
+ server s:
225
+ $(COMPOSE) up api
226
+
227
+ halt:
228
+ $(COMPOSE) stop
229
+
230
+ down:
231
+ $(COMPOSE) down
232
+
233
+ down-v:
234
+ $(COMPOSE) down -v
235
+
236
+ rspec test:
237
+ $(API_T) bundle exec rspec
238
+
239
+ spec:
240
+ $(API_T) bundle exec rspec $(SPEC)
241
+
242
+ rubocop:
243
+ $(API_T) bundle exec rubocop
244
+ ```
245
+
246
+ `rails-new` installs the Rails **gem inside the ephemeral container** and writes the app onto the mounted `/app`. That is still Docker-only: the Mac never runs `gem` or `rails`.
247
+
248
+ After the app exists, prefer `make rails ARGS='…'` over re-running `rails-new`.
249
+
250
+ Flags such as `SKIP_MOUNT=1` belong on the Make line. The `rails` recipe forwards `SKIP_MOUNT`, `SKIP_CONFIGURE`, and `FORCE` into the container:
251
+
252
+ ```bash
253
+ SKIP_MOUNT=1 make rails ARGS='command_tower:install'
254
+ ```
255
+
256
+ ### 5. Build
257
+
258
+ ```bash
259
+ make help
260
+ make build
261
+ ```
262
+
263
+ `make build` may warn that `Gemfile` is missing. That is expected until step 02.
264
+
265
+ ## Stop
266
+
267
+ `make help` prints the operator surface. `make build` produces an `api` image with Ruby. There is still no Rails application. Next: [`02-create-the-rails-app.md`](02-create-the-rails-app.md).
@@ -0,0 +1,75 @@
1
+ # 02 — Create the Rails app
2
+
3
+ ## Purpose
4
+
5
+ Generate a Rails **8.1 API** application **inside the Compose image**. The host OS never receives a `rails` binary.
6
+
7
+ ## Prerequisites
8
+
9
+ - [`01-docker-make-compose.md`](01-docker-make-compose.md) complete: `make build` works
10
+ - Working directory is the host repository (already contains Dockerfile / Compose / Makefile)
11
+
12
+ ## Files this step creates (inside the container, onto the mount)
13
+
14
+ Rails’ `rails new .` writes `Gemfile`, `config/`, `app/`, `bin/`, and the rest of a standard API app into the current directory.
15
+
16
+ `--force` is required because Dockerfile, Compose, and Makefile already exist.
17
+
18
+ ## Procedure
19
+
20
+ ```bash
21
+ make rails-new
22
+ ```
23
+
24
+ That target (from step 01) runs in the `api` service:
25
+
26
+ ```text
27
+ gem install rails -v "~> 8.1.0" --no-document
28
+ rails new . --api --database=mysql --skip-git --force
29
+ ```
30
+
31
+ Both commands run **in the container**. `--skip-git` avoids nesting a repo; you already have Git on the host. `--database=mysql` matches MariaDB via the `mysql2` adapter.
32
+
33
+ If `rails new` overwrites `Dockerfile`, `docker-compose.yml`, or `Makefile`, **restore the files from step 01**. Rails generators are not the source of the host toolchain. Confirm `command:` / `CMD` still bind `-b 0.0.0.0`.
34
+
35
+ Then add host gems CommandTower does not replace. In `Gemfile` (keep `mysql2` and `puma` from `rails new`):
36
+
37
+ ```ruby
38
+ gem "rack-cors"
39
+ ```
40
+
41
+ `command_tower` (step 03) already depends on `redis`. You do not need a second Redis gem unless the host uses Redis for its own cache.
42
+
43
+ Set `config/database.yml` so development points at the `db` service, not `localhost` on the Mac:
44
+
45
+ ```yaml
46
+ development:
47
+ adapter: mysql2
48
+ encoding: utf8mb4
49
+ collation: utf8mb4_unicode_ci
50
+ url: <%= ENV.fetch("DATABASE_URL") %>
51
+ ```
52
+
53
+ Leave test until you add a test database; `db:prepare` can create it later.
54
+
55
+ Local mail for verify/reset codes (Compose-mounted `tmp/` is visible on the Mac):
56
+
57
+ ```ruby
58
+ # config/environments/development.rb
59
+ config.action_mailer.perform_deliveries = true
60
+ config.action_mailer.delivery_method = :file
61
+ config.action_mailer.file_settings = { location: Rails.root.join("tmp/mails") }
62
+ ```
63
+
64
+ Then:
65
+
66
+ ```bash
67
+ make build
68
+ make bundle
69
+ ```
70
+
71
+ Do not run `rails new` on the Mac. Do not `brew install ruby` to make this step “easier.”
72
+
73
+ ## Stop
74
+
75
+ `Gemfile` exists, `bin/rails` exists **in the repo** (executed only via `make rails`). Dockerfile/Compose from step 01 are still the operator toolchain. `make bundle` succeeds. Next: [`03-pin-the-gem.md`](03-pin-the-gem.md).