add dynamic documents

This commit is contained in:
Pete Matsyburka
2026-02-12 09:22:08 +02:00
parent b50d982497
commit 37196ff89f
65 changed files with 4372 additions and 485 deletions
+1 -1
View File
@@ -13,7 +13,7 @@
# Indexes
#
# index_document_generation_events_on_submitter_id (submitter_id)
# index_document_generation_events_on_submitter_id_and_event_name (submitter_id,event_name) UNIQUE WHERE ((event_name)::text = ANY ((ARRAY['start'::character varying, 'complete'::character varying])::text[]))
# index_document_generation_events_on_submitter_id_and_event_name (submitter_id,event_name) UNIQUE WHERE ((event_name)::text = ANY (ARRAY[('start'::character varying)::text, ('complete'::character varying)::text]))
#
# Foreign Keys
#
+36
View File
@@ -0,0 +1,36 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: dynamic_documents
#
# id :bigint not null, primary key
# body :text not null
# head :text
# sha1 :text not null
# uuid :uuid not null
# created_at :datetime not null
# updated_at :datetime not null
# template_id :bigint not null
#
# Indexes
#
# index_dynamic_documents_on_template_id (template_id)
#
# Foreign Keys
#
# fk_rails_... (template_id => templates.id)
#
class DynamicDocument < ApplicationRecord
belongs_to :template
has_many_attached :attachments
has_many :versions, class_name: 'DynamicDocumentVersion', dependent: :destroy
before_validation :set_sha1
def set_sha1
self.sha1 = Digest::SHA1.hexdigest(body)
end
end
+30
View File
@@ -0,0 +1,30 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: dynamic_document_versions
#
# id :bigint not null, primary key
# areas :text not null
# sha1 :string not null
# created_at :datetime not null
# updated_at :datetime not null
# dynamic_document_id :bigint not null
#
# Indexes
#
# idx_on_dynamic_document_id_sha1_3503adf557 (dynamic_document_id,sha1) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (dynamic_document_id => dynamic_documents.id)
#
class DynamicDocumentVersion < ApplicationRecord
belongs_to :dynamic_document
has_one_attached :document
attribute :areas, :string, default: -> { [] }
serialize :areas, coder: JSON
end
+1 -1
View File
@@ -20,7 +20,7 @@
#
# index_email_events_on_account_id_and_event_datetime (account_id,event_datetime)
# index_email_events_on_email (email)
# index_email_events_on_email_event_types (email) WHERE ((event_type)::text = ANY ((ARRAY['bounce'::character varying, 'soft_bounce'::character varying, 'permanent_bounce'::character varying, 'complaint'::character varying, 'soft_complaint'::character varying])::text[]))
# index_email_events_on_email_event_types (email) WHERE ((event_type)::text = ANY (ARRAY[('bounce'::character varying)::text, ('soft_bounce'::character varying)::text, ('permanent_bounce'::character varying)::text, ('complaint'::character varying)::text, ('soft_complaint'::character varying)::text]))
# index_email_events_on_emailable (emailable_type,emailable_id)
# index_email_events_on_message_id (message_id)
#
+1 -1
View File
@@ -12,7 +12,7 @@
#
# Indexes
#
# index_lock_events_on_event_name_and_key (event_name,key) UNIQUE WHERE ((event_name)::text = ANY ((ARRAY['start'::character varying, 'complete'::character varying])::text[]))
# index_lock_events_on_event_name_and_key (event_name,key) UNIQUE WHERE ((event_name)::text = ANY (ARRAY[('start'::character varying)::text, ('complete'::character varying)::text]))
# index_lock_events_on_key (key)
#
class LockEvent < ApplicationRecord
+52 -3
View File
@@ -10,7 +10,7 @@
# name :text
# preferences :text not null
# slug :string not null
# source :text not null
# source :string not null
# submitters_order :string not null
# template_fields :text
# template_schema :text
@@ -75,6 +75,17 @@ class Submission < ApplicationRecord
->(e) { where(uuid: (e.template_schema.presence || e.template.schema).pluck('attachment_uuid')) },
through: :template, source: :documents_attachments
has_many :template_schema_static_documents,
->(e) { where(uuid: e.template_schema.reject { |s| s['dynamic'] }.pluck('attachment_uuid')) },
through: :template, source: :documents_attachments
has_many :template_schema_dynamic_document_versions,
->(e) { where(sha1: e.template_schema.select { |s| s['dynamic'] }.pluck('dynamic_document_sha1')) },
through: :template, source: :dynamic_document_versions
has_many :template_schema_dynamic_document_attachments,
through: :template_schema_dynamic_document_versions, source: :document_attachment
scope :active, -> { where(archived_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }
scope :pending, lambda {
@@ -110,13 +121,51 @@ class Submission < ApplicationRecord
end
def schema_documents
if template_id?
template_schema_documents
return documents_attachments unless template_id?
dynamic_count = template_schema&.count { |e| e['dynamic'] }.to_i
if template.variables_schema.blank?
if dynamic_count > 0
if dynamic_count == template_schema.size
template_schema_dynamic_document_attachments
else
template_schema_dynamic_and_static_document_attachments
end
else
template_schema_documents
end
elsif dynamic_count > 0 && dynamic_count != template_schema.size
template_schema_submission_dynamic_and_static_document_attachments
else
documents_attachments
end
end
def template_schema_submission_dynamic_and_static_document_attachments
@template_schema_submission_dynamic_and_static_document_attachments ||=
ActiveStorage::Attachment.where(
ActiveStorage::Attachment.arel_table[:id].in(
template_schema_static_documents.select(:id).arel.union(
:all,
documents_attachments.select(:id).arel
)
)
)
end
def template_schema_dynamic_and_static_document_attachments
@template_schema_dynamic_and_static_document_attachments ||=
ActiveStorage::Attachment.where(
ActiveStorage::Attachment.arel_table[:id].in(
template_schema_static_documents.select(:id).arel.union(
:all,
template_schema_dynamic_document_attachments.select(:id).arel
)
)
)
end
def fields_uuid_index
@fields_uuid_index ||= (template_fields || template.fields).index_by { |f| f['uuid'] }
end
+1 -1
View File
@@ -20,7 +20,7 @@
# index_submission_events_on_created_at (created_at)
# index_submission_events_on_submission_id (submission_id)
# index_submission_events_on_submitter_id (submitter_id)
# index_submissions_events_on_sms_event_types (account_id,created_at) WHERE ((event_type)::text = ANY ((ARRAY['send_sms'::character varying, 'send_2fa_sms'::character varying])::text[]))
# index_submissions_events_on_sms_event_types (account_id,created_at) WHERE ((event_type)::text = ANY (ARRAY[('send_sms'::character varying)::text, ('send_2fa_sms'::character varying)::text]))
#
# Foreign Keys
#
+6
View File
@@ -70,6 +70,12 @@ class Template < ApplicationRecord
has_many :submissions, dependent: :destroy
has_many :template_sharings, dependent: :destroy
has_many :template_accesses, dependent: :destroy
has_many :dynamic_documents, dependent: :destroy
has_many :dynamic_document_versions, through: :dynamic_documents, source: :versions
has_many :schema_dynamic_documents, lambda { |e|
where(uuid: e.schema.select { |e| e['dynamic'] }.pluck('attachment_uuid'))
}, class_name: 'DynamicDocument', dependent: :destroy, inverse_of: :template
scope :active, -> { where(archived_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }