You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
959 B
40 lines
959 B
class Doctrine < ApplicationRecord |
|
belongs_to :document, polymorphic: true, optional: false |
|
|
|
has_many :annotations, dependent: :destroy |
|
|
|
validates :content, presence: true |
|
validates :content_fingerprint, uniqueness: { scope: %i[document_id document_type] } |
|
|
|
before_validation do |
|
self.content_fingerprint = Digest::SHA256.hexdigest(content) |
|
end |
|
|
|
def doctrine_subjects |
|
DoctrineSubject.where(doctrine_id: self.id) |
|
end |
|
|
|
def subjects |
|
Cdao::Subject.where(id: doctrine_subjects.map(&:subject_id)) |
|
end |
|
|
|
def subject_ids |
|
subjects.map(&:id) |
|
end |
|
|
|
def subject_ids=(ids) |
|
ids.each do |subject_id| |
|
record = DoctrineSubject.find_or_initialize_by(doctrine_id: self.id, subject_id: subject_id) |
|
|
|
next if record.persisted? |
|
|
|
record.save |
|
end |
|
|
|
DoctrineSubject.where(doctrine_id: self.id).each do |record| |
|
record.destroy unless ids.member?(record.subject_id) |
|
end |
|
|
|
subject_ids |
|
end |
|
end
|
|
|