class Doctrine < ApplicationRecord has_many :doctrine_jurisprudences, dependent: :destroy has_many :jurisprudences, through: :doctrine_jurisprudences has_many :doctrine_subjects, dependent: :destroy has_many :subjects, through: :doctrine_subjects 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 = ids.map(&:to_i) 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 def plain_content Sanitize.clean(content).strip end searchable do text :content integer :id integer :document_id integer :subject_ids, multiple: true integer :user_ids, multiple: true do paper_trail.originator.to_i end join(:phil_rep, :target => Annotation, :type => :string, :join => { :from => :annotation_doctrine_id, :to => :id }) date :created_at end end