diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb
index f32e1e1e..ae54665a 100644
--- a/app/controllers/templates_controller.rb
+++ b/app/controllers/templates_controller.rb
@@ -1,10 +1,6 @@
# frozen_string_literal: true
class TemplatesController < ApplicationController
- TEMPLATE_FIELDS = %i[id author_id folder_id external_id name slug
- schema fields submitters variables_schema preferences
- shared_link source archived_at created_at updated_at].freeze
-
load_and_authorize_resource :template
def show
@@ -31,19 +27,7 @@ class TemplatesController < ApplicationController
def new; end
def edit
- ActiveRecord::Associations::Preloader.new(
- records: [@template],
- associations: [{ schema_documents: [:blob, { preview_images_attachments: :blob }] }]
- ).call
-
- @template_data =
- @template.as_json(only: TEMPLATE_FIELDS).merge(
- documents: @template.schema_documents.as_json(
- only: %i[id uuid],
- methods: %i[metadata signed_key],
- include: { preview_images: { only: %i[id], methods: %i[url metadata filename] } }
- )
- ).to_json
+ @template_data = Templates.serialize_for_builder(@template)
render :edit, layout: 'plain'
end
diff --git a/app/controllers/templates_preview_controller.rb b/app/controllers/templates_preview_controller.rb
index e132b131..602fc622 100644
--- a/app/controllers/templates_preview_controller.rb
+++ b/app/controllers/templates_preview_controller.rb
@@ -4,18 +4,7 @@ class TemplatesPreviewController < ApplicationController
load_and_authorize_resource :template
def show
- ActiveRecord::Associations::Preloader.new(
- records: [@template],
- associations: [{ schema_documents: { preview_images_attachments: :blob } }]
- ).call
-
- @template_data =
- @template.as_json.merge(
- documents: @template.schema_documents.as_json(
- methods: %i[metadata signed_key],
- include: { preview_images: { methods: %i[url metadata filename] } }
- )
- ).to_json
+ @template_data = Templates.serialize_for_builder(@template)
render :show, layout: 'plain'
end
diff --git a/app/views/templates/edit.html.erb b/app/views/templates/edit.html.erb
index 3e7b9701..5ef4c41f 100644
--- a/app/views/templates/edit.html.erb
+++ b/app/views/templates/edit.html.erb
@@ -6,4 +6,4 @@
<%= button_to nil, user_configs_path, method: :post, params: { user_config: { key: UserConfig::SHOW_APP_TOUR, value: true } }, class: 'hidden', id: 'start_tour_button' %>
<% end %>
<% end %>
-
+
diff --git a/app/views/templates_preview/show.html.erb b/app/views/templates_preview/show.html.erb
index 01d4c932..79925471 100644
--- a/app/views/templates_preview/show.html.erb
+++ b/app/views/templates_preview/show.html.erb
@@ -1 +1 @@
-
+
diff --git a/lib/templates.rb b/lib/templates.rb
index ad45a3fc..df015808 100644
--- a/lib/templates.rb
+++ b/lib/templates.rb
@@ -3,6 +3,10 @@
module Templates
COLOR_REGEXP = /\A(#(?:[0-9a-f]{3}|[0-9a-f]{6})|[a-z]+)\z/i
+ TEMPLATE_BUILDER_FIELDS = %i[id author_id folder_id external_id name slug
+ schema fields submitters variables_schema preferences
+ shared_link source archived_at created_at updated_at].freeze
+
EXPIRATION_DURATIONS = {
one_day: 1.day,
two_days: 2.days,
@@ -91,4 +95,16 @@ module Templates
Time.current + EXPIRATION_DURATIONS[default_expire_at_duration]
end
end
+
+ def serialize_for_builder(template)
+ data = template.as_json(only: TEMPLATE_BUILDER_FIELDS)
+
+ data['documents'] = template.schema_documents.preload(:blob, { preview_images_attachments: :blob }).as_json(
+ only: %i[id uuid],
+ methods: %i[metadata signed_key],
+ include: { preview_images: { only: %i[id], methods: %i[url metadata filename] } }
+ )
+
+ data
+ end
end