Files

100 lines
3.6 KiB
Ruby
Raw Permalink Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
# == Schema Information
#
2023-05-31 23:19:20 +03:00
# Table name: templates
2023-05-21 17:59:36 +03:00
#
2025-09-12 12:17:26 +03:00
# id :bigint not null, primary key
# archived_at :datetime
# fields :text not null
# name :string not null
# preferences :text not null
# schema :text not null
# shared_link :boolean default(FALSE), not null
# slug :string not null
# source :text not null
# submitters :text not null
# variables_schema :text
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# author_id :bigint not null
# external_id :string
# folder_id :bigint not null
2023-05-21 17:59:36 +03:00
#
# Indexes
#
2025-06-17 10:37:25 +03:00
# index_templates_on_account_id (account_id)
# index_templates_on_account_id_and_folder_id_and_id (account_id,folder_id,id) WHERE (archived_at IS NULL)
2025-06-18 11:42:23 +03:00
# index_templates_on_account_id_and_id_archived (account_id,id) WHERE (archived_at IS NOT NULL)
2025-06-17 10:37:25 +03:00
# index_templates_on_author_id (author_id)
# index_templates_on_external_id (external_id)
# index_templates_on_folder_id (folder_id)
# index_templates_on_slug (slug) UNIQUE
2023-05-21 17:59:36 +03:00
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
# fk_rails_... (author_id => users.id)
2023-09-21 00:23:38 +03:00
# fk_rails_... (folder_id => template_folders.id)
2023-05-21 17:59:36 +03:00
#
2023-05-31 23:19:20 +03:00
class Template < ApplicationRecord
2023-11-21 23:08:59 +02:00
DEFAULT_SUBMITTER_NAME = 'First Party'
2023-06-07 01:16:43 +03:00
2023-05-21 17:59:36 +03:00
belongs_to :author, class_name: 'User'
belongs_to :account
2023-09-21 00:23:38 +03:00
belongs_to :folder, class_name: 'TemplateFolder'
2025-06-30 11:44:55 +03:00
has_one :search_entry, as: :record, inverse_of: :record, dependent: :destroy if SearchEntry.table_exists?
2025-06-05 13:16:55 +03:00
2023-09-21 00:23:38 +03:00
before_validation :maybe_set_default_folder, on: :create
2023-05-21 17:59:36 +03:00
2024-04-17 16:08:58 +03:00
attribute :preferences, :string, default: -> { {} }
2023-05-21 17:59:36 +03:00
attribute :fields, :string, default: -> { [] }
attribute :schema, :string, default: -> { [] }
2024-09-28 01:21:35 +03:00
attribute :submitters, :string, default: -> { [{ name: I18n.t(:first_party), uuid: SecureRandom.uuid }] }
2023-07-14 22:33:42 +03:00
attribute :slug, :string, default: -> { SecureRandom.base58(14) }
2023-08-19 22:08:17 +03:00
attribute :source, :string, default: 'native'
2023-05-21 17:59:36 +03:00
2024-04-17 16:08:58 +03:00
serialize :preferences, coder: JSON
2023-12-29 18:50:14 +00:00
serialize :fields, coder: JSON
2025-09-12 12:17:26 +03:00
serialize :variables_schema, coder: JSON
2023-12-29 18:50:14 +00:00
serialize :schema, coder: JSON
serialize :submitters, coder: JSON
2023-05-21 17:59:36 +03:00
has_many_attached :documents
2023-07-26 20:35:53 +03:00
has_many :schema_documents, ->(e) { where(uuid: e.schema.pluck('attachment_uuid')) },
class_name: 'ActiveStorage::Attachment', dependent: :destroy, as: :record, inverse_of: :record
2023-05-21 17:59:36 +03:00
has_many :submissions, dependent: :destroy
has_many :template_sharings, dependent: :destroy
2024-12-08 01:37:20 +02:00
has_many :template_accesses, dependent: :destroy
2026-05-07 09:48:25 +03:00
has_many :template_versions, dependent: :destroy
2026-02-12 09:22:08 +02:00
has_many :dynamic_documents, dependent: :destroy
has_many :dynamic_document_versions, through: :dynamic_documents, source: :versions
has_many :schema_dynamic_documents, lambda { |e|
2026-05-27 21:25:25 +03:00
where(uuid: e.schema.select { |item| item['dynamic'] }.pluck('attachment_uuid'))
2026-02-12 09:22:08 +02:00
}, class_name: 'DynamicDocument', dependent: :destroy, inverse_of: :template
2023-05-21 17:59:36 +03:00
2023-12-30 00:37:03 +02:00
scope :active, -> { where(archived_at: nil) }
scope :archived, -> { where.not(archived_at: nil) }
2023-09-21 00:23:38 +03:00
2026-06-28 10:52:54 +03:00
scope :select_for_list, -> { select(:id, :name, :author_id, :account_id, :created_at, :archived_at, :folder_id) }
2024-02-01 00:34:35 +02:00
def application_key
external_id
end
2025-07-20 08:28:19 +03:00
def folder_name
folder.full_name
end
2023-09-21 00:23:38 +03:00
private
def maybe_set_default_folder
2023-09-22 02:20:38 +03:00
self.folder ||= account.default_template_folder
2023-09-21 00:23:38 +03:00
end
2023-05-21 17:59:36 +03:00
end