Files
docuseal/app/models/template.rb
T

54 lines
1.6 KiB
Ruby
Raw 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
#
# id :bigint not null, primary key
# deleted_at :datetime
2023-07-09 16:27:07 +03:00
# fields :text not null
2023-05-21 17:59:36 +03:00
# name :string not null
2023-07-09 16:27:07 +03:00
# schema :text not null
2023-05-21 17:59:36 +03:00
# slug :string not null
2023-07-09 16:27:07 +03:00
# submitters :text not null
2023-05-21 17:59:36 +03:00
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# author_id :bigint not null
#
# Indexes
#
2023-05-31 23:19:20 +03:00
# index_templates_on_account_id (account_id)
# index_templates_on_author_id (author_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-05-31 23:19:20 +03:00
class Template < ApplicationRecord
2023-06-08 00:20:42 +03:00
DEFAULT_SUBMITTER_NAME = 'First Submitter'
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
attribute :fields, :string, default: -> { [] }
attribute :schema, :string, default: -> { [] }
2023-06-07 01:16:43 +03:00
attribute :submitters, :string, default: -> { [{ name: DEFAULT_SUBMITTER_NAME, uuid: SecureRandom.uuid }] }
2023-07-14 22:33:42 +03:00
attribute :slug, :string, default: -> { SecureRandom.base58(14) }
2023-05-21 17:59:36 +03:00
serialize :fields, JSON
serialize :schema, JSON
2023-06-07 01:16:43 +03:00
serialize :submitters, 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
scope :active, -> { where(deleted_at: nil) }
end