Class: Studio::EmailsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/studio/emails_controller.rb

Overview

/admin/emails — the standard transactional-email page every Studio app gets, modelled on the living style guide (/admin/style): a plain host-inherited controller whose view is a bare content wrapper, so it renders inside each host's application layout and picks up that app's navbar and theme.

It lists Studio::EmailCatalog's registry — one row per registered email, each showing its live banner and whether that banner is the INHERITED engine default or an APP-OWNED override — and writes an override through the shared crop modal. Replaces /admin/email_images, which now redirects here.

An app whose host never set Studio.s3_bucket_prefix cannot store an override. That is a read-only page, not an error: uploads_available? gates the write actions and the view explains why, so the page still shows what each email is currently sending.

Constant Summary collapse

MAX_BYTES =
8.megabytes

Instance Method Summary collapse

Instance Method Details

#destroyObject

DELETE /admin/emails/:key — drop this app's override and fall back to the inherited default.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/studio/emails_controller.rb', line 69

def destroy
  # rescue_and_log, like #update: destroy is a WRITE path (it drops an
  # ImageCache row and deletes the S3 object behind it), and the bare rescue
  # below turns any failure into a friendly alert. Without the log that
  # failure is invisible — the admin sees "try again" and nothing reaches
  # ErrorLog to say why.
  rescue_and_log do
    reverted = Studio::EmailCatalog.revert(@key)
    notice = if reverted
               "#{Studio::EmailCatalog.label(@key)} reverted to the inherited default."
             else
               "#{Studio::EmailCatalog.label(@key)} was already using the inherited default."
             end
    redirect_to admin_emails_path, notice: notice, status: :see_other
  end
rescue StandardError
  redirect_to admin_emails_path, alert: "Couldn't revert the image. Please try again.", status: :see_other
end

#indexObject



23
24
25
26
# File 'app/controllers/studio/emails_controller.rb', line 23

def index
  @entries = Studio::EmailCatalog.entries
  @uploads_available = Studio::EmailCatalog.uploads_available?
end

#rawObject

GET /admin/emails/:key/raw — the rendered email itself, as the iframe source on #show. Layout-less on purpose: this response IS the email.

A preview builder is host code run against whatever sample data this environment happens to hold, so it is expected to fail sometimes. It renders the failure as a readable page inside the iframe rather than 500ing, so one broken builder costs one preview, not the manager.



44
45
46
47
48
49
# File 'app/controllers/studio/emails_controller.rb', line 44

def raw
  html = Studio::EmailCatalog.preview_html(@key)
  return render(html: preview_unavailable_html.html_safe, layout: false) if html.nil?

  render html: html.html_safe, layout: false
end

#showObject

GET /admin/emails/:key — one email: its banner, its type, and a live preview built from the host's sample data.



30
31
32
33
34
35
# File 'app/controllers/studio/emails_controller.rb', line 30

def show
  @entry = Studio::EmailCatalog.entry(@key)
  @subject = Studio::EmailCatalog.preview_subject(@key)
  @preview_error = Studio::EmailCatalog.preview_error(@key)
  @uploads_available = Studio::EmailCatalog.uploads_available?
end

#updateObject

PATCH /admin/emails/:key — upload/replace this app's own banner.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/studio/emails_controller.rb', line 52

def update
  file = params[:image]
  unless valid_image?(file)
    message = file.blank? ? "Choose an image to upload." : "Use a PNG, JPG, or WebP under 8 MB."
    return redirect_to admin_emails_path, alert: message, status: :see_other
  end

  rescue_and_log do
    Studio::EmailCatalog.store(@key, io: file, content_type: file.content_type)
    redirect_to admin_emails_path, notice: "#{Studio::EmailCatalog.label(@key)} banner updated.", status: :see_other
  end
rescue StandardError
  redirect_to admin_emails_path, alert: "Couldn't save the image. Please try again.", status: :see_other
end