Create letter template without db (#172)

This commit is contained in:
Max Wofford 2025-11-21 19:15:59 -05:00 committed by GitHub
parent ee06f202a1
commit 9dbe46bffe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 86 additions and 0 deletions

View file

@ -0,0 +1,63 @@
class TemplatePreviewsController < ApplicationController
skip_before_action :authenticate_user!
skip_after_action :verify_authorized
def index
@templates = SnailMail::Components::Registry.available_templates
end
def show
template = params[:id]
include_qr_code = params[:qr].present?
mock_letter = create_mock_letter
pdf = SnailMail::PhlexService.generate_label(mock_letter, { template:, include_qr_code: })
send_data pdf.render, type: "application/pdf", disposition: "inline"
end
private
def create_mock_letter
return_address = OpenStruct.new(
name: "Hack Club",
line_1: "15 Falls Rd",
city: "Shelburne",
state: "VT",
postal_code: "05482",
country: "US",
)
names = [
"Orpheus",
"Heidi Hakkuun",
"Dinobox",
"Arcadius",
"Cap'n Trashbeard",
]
usps_mailer_id = OpenStruct.new(mid: "111111")
sender, recipient = names.sample(2)
OpenStruct.new(
address: SnailMail::Preview::FakeAddress.new(
line_1: "8605 Santa Monica Blvd",
line_2: "Apt. 86294",
city: "West Hollywood",
state: "CA",
postal_code: "90069",
country: "US",
name_line: sender,
),
return_address:,
return_address_name_line: recipient,
postage_type: "stamps",
postage: 0.73,
usps_mailer_id:,
imb_serial_number: "1337",
metadata: {},
rubber_stamps: "here's\n where\n rubber stamps go!",
public_id: "ltr!PR3V13W",
)
end
end

View file

@ -0,0 +1,22 @@
<% content_for :title, "Template Previews" %>
<div class="max-w-4xl mx-auto p-8">
<h1 class="text-3xl font-bold mb-8">Letter Template Previews</h1>
<div class="grid gap-4">
<% @templates.each do |template| %>
<div class="border rounded-lg p-4 bg-white shadow-sm">
<h2 class="text-xl font-semibold mb-2"><%= template.to_s.split("::").last.humanize %></h2>
<p class="text-gray-600 mb-4 font-mono text-sm"><%= template %></p>
<div class="flex gap-3">
<%= link_to "Preview PDF", template_preview_path(template),
target: "_blank",
class: "bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded" %>
<%= link_to "Preview with QR", template_preview_path(template, qr: true),
target: "_blank",
class: "bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded" %>
</div>
</div>
<% end %>
</div>
</div>

View file

@ -705,5 +705,6 @@ Rails.application.routes.draw do
# root "posts#index"
if Rails.env.development?
mount LetterOpenerWeb::Engine, at: "/letter_opener"
resources :template_previews, only: [:index, :show], path: "previews/templates"
end
end