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
|
|
|
#
|
2024-02-01 00:34:35 +02:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
|
# archived_at :datetime
|
|
|
|
|
# fields :text not null
|
|
|
|
|
# name :string not null
|
2024-04-17 16:08:58 +03:00
|
|
|
# preferences :text not null
|
2024-02-01 00:34:35 +02:00
|
|
|
# schema :text not null
|
|
|
|
|
# slug :string not null
|
|
|
|
|
# source :text not null
|
|
|
|
|
# submitters :text not null
|
|
|
|
|
# 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
|
|
|
|
|
#
|
2024-06-24 13:27:14 +03:00
|
|
|
# index_templates_on_account_id (account_id)
|
|
|
|
|
# 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'
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
2024-02-03 22:37:17 +02:00
|
|
|
has_many :template_sharings, dependent: :destroy
|
2024-12-08 01:37:20 +02:00
|
|
|
has_many :template_accesses, dependent: :destroy
|
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
|
|
|
|
2024-04-11 16:36:53 +03:00
|
|
|
delegate :name, to: :folder, prefix: true
|
|
|
|
|
|
2024-02-01 00:34:35 +02:00
|
|
|
def application_key
|
|
|
|
|
external_id
|
|
|
|
|
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
|