Add ActiveRecord, customer dashboard, separate DB architecture

- Re-enable ActiveRecord pointing at Neon Postgres
- Add Customer model with stripe_customer_id, email, slug, status
- Add dashboard controller and Apple ID-style device view
- Redirect checkout success to /dashboard
- Webhook now calls /instances, parses response, creates Customer in Rails DB
- Add MIT License

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Andrew Gundersen 2026-02-26 23:51:50 -05:00
commit fd2d498141
16 changed files with 199 additions and 89 deletions

View file

@ -19,7 +19,7 @@ FROM base as build
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git pkg-config
apt-get install --no-install-recommends -y build-essential git pkg-config libpq-dev
# Install application gems
COPY Gemfile Gemfile.lock ./

7
LICENSE Normal file
View file

@ -0,0 +1,7 @@
Copyright (c) 2026 Andrew Gundersen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -5,7 +5,7 @@ class CheckoutController < ApplicationController
session = Stripe::Checkout::Session.create(
mode: "subscription",
line_items: [{ price: ENV.fetch("STRIPE_PRICE_ID"), quantity: 1 }],
success_url: "#{ENV.fetch("BASE_URL")}/success?session_id={CHECKOUT_SESSION_ID}",
success_url: "#{ENV.fetch("BASE_URL")}/dashboard?session_id={CHECKOUT_SESSION_ID}",
cancel_url: "#{ENV.fetch("BASE_URL")}/",
)
render json: { url: session.url }
@ -22,17 +22,26 @@ class CheckoutController < ApplicationController
)
if event["type"] == "checkout.session.completed"
session = event["data"]["object"]
uri = URI("#{ENV.fetch("INFRA_SERVICE_URL")}/customers")
sess = event["data"]["object"]
uri = URI("#{ENV.fetch("INFRA_SERVICE_URL")}/instances")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
req = Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
req = Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
req.body = {
stripe_customer_id: session["customer"],
stripe_subscription_id: session["subscription"],
email: session.dig("customer_details", "email")
stripe_customer_id: sess["customer"],
stripe_subscription_id: sess["subscription"],
email: sess.dig("customer_details", "email")
}.to_json
http.request(req)
res = http.request(req)
body = JSON.parse(res.body)
Customer.find_or_create_by(stripe_customer_id: sess["customer"]) do |c|
c.stripe_subscription_id = sess["subscription"]
c.email = sess.dig("customer_details", "email")
c.slug = body["slug"]
c.status = body["status"] || "provisioning"
end
end
render json: { status: "ok" }

View file

@ -0,0 +1,14 @@
class DashboardController < ApplicationController
def show
if params[:session_id].present?
stripe_session = Stripe::Checkout::Session.retrieve(params[:session_id])
@customer = Customer.find_by(stripe_customer_id: stripe_session.customer)
elsif params[:email].present?
@customer = Customer.find_by(email: params[:email])
else
redirect_to root_path
end
rescue Stripe::StripeError
redirect_to root_path
end
end

View file

@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end

2
app/models/customer.rb Normal file
View file

@ -0,0 +1,2 @@
class Customer < ApplicationRecord
end

View file

@ -0,0 +1,126 @@
<% content_for :title, "My Devices — Crimata" %>
<style>
.dashboard-header { margin-bottom: 2.5rem; }
.dashboard-header h1 { font-size: 2rem; font-weight: 700; letter-spacing: -0.5px; }
.dashboard-header p { color: #666; margin-top: 0.4rem; font-size: 0.95rem; }
.devices { display: grid; gap: 1rem; }
.device-card {
border: 1px solid #e5e5e5;
border-radius: 12px;
padding: 1.5rem;
display: flex;
align-items: center;
gap: 1.25rem;
background: #fafafa;
}
.device-card:hover { border-color: #ccc; background: #fff; }
.device-icon {
width: 52px; height: 52px;
background: #111;
border-radius: 10px;
display: flex; align-items: center; justify-content: center;
flex-shrink: 0;
}
.device-icon svg { width: 28px; height: 28px; fill: #fff; }
.device-info { flex: 1; min-width: 0; }
.device-name { font-weight: 600; font-size: 1rem; margin-bottom: 0.2rem; }
.device-meta { font-size: 0.85rem; color: #888; display: flex; gap: 1rem; flex-wrap: wrap; }
.device-meta span { display: flex; align-items: center; gap: 0.3rem; }
.device-status { flex-shrink: 0; }
.badge {
display: inline-flex; align-items: center; gap: 0.4rem;
font-size: 0.78rem; font-weight: 600;
padding: 0.3rem 0.75rem;
border-radius: 20px;
}
.badge.provisioning { background: #fff3cd; color: #856404; }
.badge.active { background: #d1fae5; color: #065f46; }
.badge .dot {
width: 6px; height: 6px; border-radius: 50%;
background: currentColor;
}
.device-action { flex-shrink: 0; }
.device-action a {
font-size: 0.85rem; color: #2563eb; text-decoration: none; font-weight: 500;
}
.device-action a:hover { text-decoration: underline; }
.device-action .disabled { color: #bbb; cursor: default; font-size: 0.85rem; }
.empty-state {
text-align: center; padding: 4rem 0; color: #888;
}
.empty-state h2 { font-size: 1.25rem; font-weight: 600; color: #333; margin-bottom: 0.5rem; }
.setup-card {
border: 1px dashed #e5e5e5;
border-radius: 12px;
padding: 2rem;
text-align: center;
color: #888;
}
.setup-card .spinner {
width: 32px; height: 32px;
border: 3px solid #eee;
border-top-color: #111;
border-radius: 50%;
animation: spin 0.8s linear infinite;
margin: 0 auto 1rem;
}
@keyframes spin { to { transform: rotate(360deg); } }
.setup-card p { font-size: 0.9rem; margin-top: 0.4rem; }
</style>
<div class="dashboard-header">
<h1>My Devices</h1>
<p><%= @customer&.email %></p>
</div>
<div class="devices">
<% if @customer.nil? %>
<div class="setup-card">
<div class="spinner"></div>
<strong>Setting up your device&hellip;</strong>
<p>This usually takes a minute. Refresh to check progress.</p>
</div>
<% else %>
<div class="device-card">
<div class="device-icon">
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path d="M20 3H4a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h7v2H8v2h8v-2h-3v-2h7a1 1 0 0 0 1-1V4a1 1 0 0 0-1-1zm-1 12H5V5h14v10z"/>
</svg>
</div>
<div class="device-info">
<div class="device-name"><%= @customer.slug || "Your device" %></div>
<div class="device-meta">
<% if @customer.slug.present? %>
<span>&#127758; <%= @customer.slug %>.crimata.com</span>
<% end %>
</div>
</div>
<div class="device-status">
<% badge_class = @customer.status == "active" ? "active" : "provisioning" %>
<% badge_label = { "active" => "Active", "provisioning" => "Provisioning", "failed" => "Failed" }.fetch(@customer.status, "Pending") %>
<span class="badge <%= badge_class %>">
<span class="dot"></span><%= badge_label %>
</span>
</div>
<div class="device-action">
<% if @customer.slug.present? %>
<a href="https://<%= @customer.slug %>.crimata.com" target="_blank">Open &rarr;</a>
<% else %>
<span class="disabled">Not ready yet</span>
<% end %>
</div>
</div>
<% end %>
</div>

View file

@ -1,8 +1,3 @@
#!/bin/bash -e
# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
./bin/rails db:prepare
fi
exec "${@}"

View file

@ -4,7 +4,7 @@ require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
# require "active_job/railtie"
# require "active_record/railtie"
require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
# require "action_mailer/railtie"

View file

@ -1,84 +1,14 @@
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
url: <%= ENV["DATABASE_URL"] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: website_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: website
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: website_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: website_production
username: website
password: <%= ENV["WEBSITE_DATABASE_PASSWORD"] %>

View file

@ -43,10 +43,10 @@ Rails.application.configure do
config.active_support.disallowed_deprecation_warnings = []
# Raise an error on page load if there are pending migrations.
# config.active_record.migration_error = :page_load
config.active_record.migration_error = :page_load
# Highlight code that triggered database queries in logs.
# config.active_record.verbose_query_logs = true
config.active_record.verbose_query_logs = true
# Raises error for missing translations.

View file

@ -61,7 +61,7 @@ Rails.application.configure do
config.active_support.report_deprecations = false
# Do not dump schema after migrations.
# config.active_record.dump_schema_after_migration = false
config.active_record.dump_schema_after_migration = false
# Enable DNS rebinding protection and other `Host` header attacks.
# config.hosts = [

View file

@ -2,6 +2,7 @@ Rails.application.routes.draw do
root "pages#home"
get "/pricing", to: "pages#pricing"
get "/success", to: "pages#success"
get "/dashboard", to: "dashboard#show"
post "/checkout/create_session", to: "checkout#create_session"
post "/checkout/webhook", to: "checkout#webhook"

View file

@ -0,0 +1,15 @@
class CreateCustomers < ActiveRecord::Migration[7.1]
def change
create_table :customers, if_not_exists: true do |t|
t.string :stripe_customer_id
t.string :stripe_subscription_id
t.string :email
t.string :slug
t.timestamps
end
add_column :customers, :created_at, :datetime, null: false, default: -> { "CURRENT_TIMESTAMP" } unless column_exists?(:customers, :created_at)
add_column :customers, :updated_at, :datetime, null: false, default: -> { "CURRENT_TIMESTAMP" } unless column_exists?(:customers, :updated_at)
end
end

View file

@ -0,0 +1,5 @@
class AddStatusToCustomers < ActiveRecord::Migration[7.1]
def change
add_column :customers, :status, :string, default: "pending" unless column_exists?(:customers, :status)
end
end

View file

@ -9,6 +9,9 @@ console_command = '/rails/bin/rails console'
[build]
[deploy]
release_command = "./bin/rails db:migrate"
[http_service]
internal_port = 3000
force_https = true