discourse/app/controllers/admin/form_templates_controller.rb
Linca 1198397c0f
FIX: Process templates before previewing (#33848)
The bare template yaml file provided by the user cannot be directly used
for form templates preview, because components such as `TagChooserField`
actually rely on a backend process step. This step exists when users
actually use the template, but is missing when the admin writes and
previews the template.

This commit supplements the missing process step, thus fixing the
problem that tag-chooser may break ember during admin preview
2025-07-29 15:09:02 +08:00

78 lines
2 KiB
Ruby

# frozen_string_literal: true
class Admin::FormTemplatesController < Admin::StaffController
before_action :ensure_form_templates_enabled
def index
form_templates = FormTemplate.all.order(:id)
render_serialized(form_templates, AdminFormTemplateSerializer, root: "form_templates")
end
def new
end
def preview
params.require(:name)
params.require(:template)
if params[:id].present?
template = FormTemplate.find(params[:id])
template.assign_attributes(name: params[:name], template: params[:template])
else
template = FormTemplate.new(name: params[:name], template: params[:template])
end
begin
template.validate!
template.process!(guardian)
render_serialized(template, FormTemplateSerializer, root: "form_template")
rescue FormTemplate::NotAllowed => err
render_json_error(err.message)
end
end
def create
params.require(:name)
params.require(:template)
begin
template = FormTemplate.create!(name: params[:name], template: params[:template])
render_serialized(template, AdminFormTemplateSerializer, root: "form_template")
rescue FormTemplate::NotAllowed => err
render_json_error(err.message)
end
end
def show
template = FormTemplate.find(params[:id])
render_serialized(template, AdminFormTemplateSerializer, root: "form_template")
end
def edit
FormTemplate.find(params[:id])
end
def update
template = FormTemplate.find(params[:id])
begin
template.update!(name: params[:name], template: params[:template])
render_serialized(template, AdminFormTemplateSerializer, root: "form_template")
rescue FormTemplate::NotAllowed => err
render_json_error(err.message)
end
end
def destroy
template = FormTemplate.find(params[:id])
template.destroy!
render json: success_json
end
private
def ensure_form_templates_enabled
raise Discourse::InvalidAccess.new unless SiteSetting.experimental_form_templates
end
end