Module: Crm::Publications::PdfActions
- Extended by:
- ActiveSupport::Concern
- Included in:
- Crm::PublicationsController
- Defined in:
- app/controllers/crm/publications/pdf_actions.rb
Overview
Conversational-PDF and translation actions for Crm::PublicationsController,
extracted to keep the controller under the class-length budget:
create_pdf_variation — Sunny chat session seeded with the current PDF
translate — DeepL translation via the job monitor (page flow)
edit_typst — Sunny chat session for Typst-master editing
Instance Method Summary collapse
-
#create_pdf_variation ⇒ void
Starts a conversational "PDF variation" session: a scoped Sunny chat locked to the PDF tools and pre-seeded with this publication's current PDF.
-
#edit_typst ⇒ void
Starts a conversational Typst-master editing session for a publication that HAS a Typst master (uploads.typst_source — documents authored via Sunny's pdf_typst_generate).
-
#translate_pdf ⇒ void
Translates the publication's PDF into another language via DeepL's document API.
Instance Method Details
#create_pdf_variation ⇒ void
This method returns an undefined value.
Starts a conversational "PDF variation" session: a scoped Sunny chat locked to
the PDF tools and pre-seeded with this publication's current PDF. The user
describes the change in plain language; Sunny inspects/edits/generates and
stages the result back into the PDF studio, where it can be imported as a new
revision or an in-place replacement of this publication.
18 19 20 21 |
# File 'app/controllers/crm/publications/pdf_actions.rb', line 18 def create_pdf_variation start_pdf_session!(no_pdf_error: 'This publication has no PDF to vary.', error_prefix: 'Could not start a PDF session') end |
#edit_typst ⇒ void
This method returns an undefined value.
Starts a conversational Typst-master editing session for a publication that
HAS a Typst master (uploads.typst_source — documents authored via Sunny's
pdf_typst_generate). The studio chat is pre-seeded to use the
pdf_typst_edit tool: Sunny reads the current master, applies the requested
change to the SOURCE, recompiles, and stages a revision for review.
78 79 80 81 82 83 84 85 86 |
# File 'app/controllers/crm/publications/pdf_actions.rb', line 78 def edit_typst start_pdf_session!( no_pdf_error: 'This publication has no Typst master source — author one via "Create PDF Variation" (Sunny\'s pdf_typst_generate), or use the standard variation flow.', error_prefix: 'Could not start a Typst editing session', title: "Edit Typst — ", opening: ->(pub) { "The publication \"#{pub.name}\" (#{pub.sku}) has a Typst master source. Use pdf_typst_edit to read the current .typ source, then apply this change and recompile: " }, pdf_check: :typst_master ) end |
#translate_pdf ⇒ void
This method returns an undefined value.
Translates the publication's PDF into another language via DeepL's document
API. Page-initiated flow (deliberately NOT the Sunny chat flow): a small
form picks the target language, the worker is enqueued directly, and the
user lands on the job monitor (jobs#show), which polls and redirects to the
staged result in the PDF studio on completion — review, SKU, locales and
import all happen there. Chat-initiated translations use the same worker
via the pdf_translate tool and report back in the conversation instead.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/controllers/crm/publications/pdf_actions.rb', line 32 def translate_pdf load_publication (:update, @publication) (:read, GeneratedPdf) # studio access — a direct POST must not bypass the UI gate return redirect_with_error('This publication has no PDF to translate.') unless @publication.literature&. return redirect_with_error('PDF translation is not configured in this environment (no DeepL credential).') unless DeepLClient.configured? if request.get? # renders the language-picker form @existing_translations = @publication.translated_publications.limit(10) render(:translate) and return end lang = params.fetch(:lang, 'fr-CA').presence || 'fr-CA' begin lang = DeepLClient.canonical_document_locale(lang) rescue DeepLClient::UnsupportedLanguage => e return render_translate_error(ERB::Util.html_escape(e.)) end existing = @publication.translation_for_language(lang) if existing && !params.fetch(:confirm_duplicate, nil).to_b # Duplicate guard — one language variant per language is the norm; # creating a second needs an explicit override. @duplicate = existing @existing_translations = @publication.translated_publications.limit(10) flash.now[:warning] = "A #{lang} translation already exists for this publication — review it before creating another." render(:translate, status: :unprocessable_content) and return end jid = PublicationTranslationWorker.perform_async( @publication.id, lang, 'created_by_id' => current_account&.id, 'source_record_type' => 'Item', 'source_record_id' => @publication.id ) redirect_to_job_or_fallback(jid, publication_path(@publication)) end |