From 90c5b26f59b5e7b855d02cb7100c6b4f02eef40d Mon Sep 17 00:00:00 2001 From: Samuel Culley Date: Thu, 16 Jul 2026 12:09:26 +0100 Subject: [PATCH] Add rake task to retrieve form documents The task retrieves form documents for a given form based on the provided form_id, tag and language. One form document is retrieved at a time so that the output is always limited to a single JSON form document. If no language is provided, it defaults to using 'en'. --- lib/tasks/forms.rake | 18 ++++++++++ spec/lib/tasks/forms.rake_spec.rb | 55 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/lib/tasks/forms.rake b/lib/tasks/forms.rake index c49831887..4c580fe8b 100644 --- a/lib/tasks/forms.rake +++ b/lib/tasks/forms.rake @@ -174,6 +174,24 @@ namespace :forms do Rails.logger.info "Form #{form.id} (\"#{form.name}\") created by #{creator&.name || 'No creator'} with organisation #{creator&.organisation&.name || 'N/A'}" end end + + desc "Show a form's form_document as JSON" + task :show_form_document, %i[form_id tag language] => :environment do |_, args| + usage_message = "usage: rake forms:show_form_document[, , ]".freeze + + abort usage_message if args[:form_id].blank? || args[:tag].blank? + language = args[:language].presence || "en" + + abort "tag must be one of draft, live or archived" unless %w[draft live archived].include?(args[:tag]) + abort "language must be en or cy" unless %w[en cy].include?(language) + + form = Form.find(args[:form_id]) + + form_document = form.form_documents.find_by(tag: args[:tag], language:) + abort "#{fmt_form(form)} does not have a #{args[:tag]} #{language} form document" if form_document.blank? + + puts JSON.pretty_generate(form_document.as_json) + end end def move_forms(form_ids, group_id) diff --git a/spec/lib/tasks/forms.rake_spec.rb b/spec/lib/tasks/forms.rake_spec.rb index 3c778e0cd..81f159299 100644 --- a/spec/lib/tasks/forms.rake_spec.rb +++ b/spec/lib/tasks/forms.rake_spec.rb @@ -807,4 +807,59 @@ end end end + + describe "forms:show_form_document" do + subject(:task) do + Rake::Task["forms:show_form_document"] + end + + let(:form) { create(:form) } + + it "prints the requested form document as JSON" do + expect { task.invoke(form.id, "draft", "en") } + .to output(/"id": #{form.draft_form_document.id}/).to_stdout + end + + it "prints the requested English form document when no language is given" do + expect { task.invoke(form.id, "draft") } + .to output(/"id": #{form.draft_form_document.id}/).to_stdout + end + + it "aborts with a usage message when arguments are missing" do + expect { + task.invoke(form.id) + }.to raise_error(SystemExit) + .and output(/usage: rake forms:show_form_document\[, , \]/).to_stderr + end + + it "aborts when the tag is invalid" do + expect { + task.invoke(form.id, "invalid", "en") + }.to raise_error(SystemExit) + .and output(/tag must be one of draft, live or archived/).to_stderr + end + + it "aborts when the language is invalid" do + expect { + task.invoke(form.id, "draft", "invalid") + }.to raise_error(SystemExit) + .and output(/language must be en or cy/).to_stderr + end + + it "aborts when the requested form document is missing" do + expect { + task.invoke(form.id, "draft", "cy") + }.to raise_error(SystemExit) + .and output(/form #{form.id} \("#{form.name}"\) does not have a draft cy form document/).to_stderr + end + + context "when a form has a Welsh translation" do + let(:form) { create(:form, :with_welsh_translation) } + + it "prints the requested form document as JSON" do + expect { task.invoke(form.id, "draft", "cy") } + .to output(/"id": #{form.draft_welsh_form_document.id}/).to_stdout + end + end + end end