@@ -9,27 +9,15 @@ def index
99
1010 templates = paginate ( templates . preload ( :author , folder : :parent_folder ) )
1111
12- schema_documents =
13- ActiveStorage ::Attachment . where ( record_id : templates . map ( &:id ) ,
14- record_type : 'Template' ,
15- name : :documents ,
16- uuid : templates . flat_map { |t | t . schema . pluck ( 'attachment_uuid' ) } )
17- . preload ( :blob )
18-
19- preview_image_attachments =
20- ActiveStorage ::Attachment . joins ( :blob )
21- . where ( blob : { filename : [ '0.png' , '0.jpg' ] } )
22- . where ( record_id : schema_documents . map ( &:id ) ,
23- record_type : 'ActiveStorage::Attachment' ,
24- name : :preview_images )
25- . preload ( :blob )
12+ schema_documents , dynamic_documents , preview_image_attachments = preload_relations ( templates )
2613
2714 expires_at = Accounts . link_expires_at ( current_account )
2815
2916 render json : {
3017 data : templates . map do |t |
3118 Templates ::SerializeForApi . call ( t ,
3219 schema_documents : schema_documents . select { |e | e . record_id == t . id } ,
20+ dynamic_documents :,
3321 preview_image_attachments :,
3422 expires_at :)
3523 end ,
@@ -88,6 +76,41 @@ def destroy
8876
8977 private
9078
79+ def preload_relations ( templates )
80+ schema_documents =
81+ ActiveStorage ::Attachment . where ( record_id : templates . map ( &:id ) ,
82+ record_type : 'Template' ,
83+ name : :documents ,
84+ uuid : templates . flat_map { |t | t . schema . pluck ( 'attachment_uuid' ) } )
85+ . preload ( :blob )
86+
87+ dynamic_document_uuids =
88+ templates . flat_map { |t | t . schema . select { |item | item [ 'dynamic' ] } . pluck ( 'attachment_uuid' ) }
89+
90+ dynamic_documents =
91+ if dynamic_document_uuids . present?
92+ DynamicDocument . where ( template : templates . map ( &:id ) )
93+ . where ( uuid : dynamic_document_uuids )
94+ . preload ( current_version : { document_attachment : :blob } )
95+ . select ( :id , :uuid , :template_id , :sha1 , :created_at , :updated_at )
96+ else
97+ DynamicDocument . none
98+ end
99+
100+ preview_attachment_ids =
101+ schema_documents . map ( &:id ) + dynamic_documents . filter_map { |d | d . current_version &.document_attachment &.id }
102+
103+ preview_image_attachments =
104+ ActiveStorage ::Attachment . joins ( :blob )
105+ . where ( blob : { filename : [ '0.png' , '0.jpg' ] } )
106+ . where ( record_id : preview_attachment_ids ,
107+ record_type : 'ActiveStorage::Attachment' ,
108+ name : :preview_images )
109+ . preload ( :blob )
110+
111+ [ schema_documents , dynamic_documents , preview_image_attachments ]
112+ end
113+
91114 def filter_templates ( templates , params )
92115 templates = Templates . search ( current_user , templates , params [ :q ] )
93116 templates = params [ :archived ] . in? ( [ 'true' , true ] ) ? templates . archived : templates . active
0 commit comments