13 changed files with 208 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||||||
|
class Annomark < ApplicationRecord |
||||||
|
acts_as_list column: :rank |
||||||
|
|
||||||
|
validates :name, :code, presence: true |
||||||
|
validates :code, uniqueness: { scope: %i[name] } |
||||||
|
|
||||||
|
has_many :annotations, dependent: :restrict_with_error |
||||||
|
end |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
class Annotation < ApplicationRecord |
||||||
|
acts_as_list column: :rank, scope: %i[doctrine_id] |
||||||
|
|
||||||
|
belongs_to :annomark, optional: false |
||||||
|
|
||||||
|
belongs_to :doctrine, optional: false |
||||||
|
|
||||||
|
belongs_to :document, polymorphic: true, optional: false |
||||||
|
|
||||||
|
has_many :annotation_documents, inverse_of: :annotation, dependent: :destroy |
||||||
|
accepts_nested_attributes_for :annotation_documents, allow_destroy: true |
||||||
|
|
||||||
|
validates :annomark_id, |
||||||
|
uniqueness: { scope: %i[doctrine_id document_id document_type phil_rep] } |
||||||
|
|
||||||
|
def documents |
||||||
|
annotation_documents.collect(&:document) |
||||||
|
end |
||||||
|
|
||||||
|
def add_document(document) |
||||||
|
record = annotation_documents.new |
||||||
|
record.document = document |
||||||
|
record.save |
||||||
|
end |
||||||
|
|
||||||
|
def remove_document(document) |
||||||
|
annotation_documents.find_by(document_id: document.id).destroy |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
class AnnotationDocument < ApplicationRecord |
||||||
|
acts_as_list column: :rank, scope: %i[annotation_id] |
||||||
|
|
||||||
|
belongs_to :annotation, optional: false |
||||||
|
|
||||||
|
belongs_to :document, polymorphic: true, optional: false |
||||||
|
|
||||||
|
validates :document_id, uniqueness: { scope: %i[annotation_id] } |
||||||
|
end |
||||||
@ -0,0 +1,40 @@ |
|||||||
|
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 |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
class DoctrineSubject < ApplicationRecord |
||||||
|
acts_as_list column: :rank, scope: %i[doctrine] |
||||||
|
|
||||||
|
belongs_to :doctrine, optional: false |
||||||
|
|
||||||
|
belongs_to :subject, class_name: "Cdao::Subject", optional: false |
||||||
|
end |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
class CreateAnnomarks < ActiveRecord::Migration[6.1] |
||||||
|
def change |
||||||
|
create_table :annomarks do |t| |
||||||
|
t.string :name, null: false, default: "" |
||||||
|
t.string :code, null: false, default: "" |
||||||
|
t.integer :rank, null: false, default: 0 |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
|
||||||
|
add_index :annomarks, %i[name code], uniquness: true |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,16 @@ |
|||||||
|
class CreateDoctrines < ActiveRecord::Migration[6.1] |
||||||
|
def change |
||||||
|
create_table :doctrines do |t| |
||||||
|
t.references :document, null: false, polymorphic: true |
||||||
|
t.text :content, null: false, default: "" |
||||||
|
t.string :content_fingerprint, null: false, default: "" |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
|
||||||
|
add_index :doctrines, |
||||||
|
%i[content_fingerprint document_id document_type], |
||||||
|
name: "index_doctrines_uniqueness", |
||||||
|
unique: true |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,13 @@ |
|||||||
|
class CreateDoctrineSubjects < ActiveRecord::Migration[6.1] |
||||||
|
def change |
||||||
|
create_table :doctrine_subjects do |t| |
||||||
|
t.references :doctrine, null: false |
||||||
|
t.references :subject, null: false |
||||||
|
t.integer :rank, null: false, default: 1 |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
|
||||||
|
add_index :doctrine_subjects, %i[doctrine_id subject_id], unique: true |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
class CreateAnnotations < ActiveRecord::Migration[6.1] |
||||||
|
def change |
||||||
|
create_table :annotations do |t| |
||||||
|
t.references :doctrine, null: false |
||||||
|
t.references :document, null: false, polymorphic: true |
||||||
|
t.references :annomark, null: false |
||||||
|
t.string :phil_rep, null: false, default: "" |
||||||
|
t.text :editor_notes, null: false, default: "" |
||||||
|
t.integer :rank, null: false, default: 1 |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
|
||||||
|
add_index :annotations, |
||||||
|
%i[doctrine_id annomark_id document_id document_type phil_rep], |
||||||
|
name: "index_annotation_uniqueness", |
||||||
|
unique: true |
||||||
|
end |
||||||
|
end |
||||||
@ -0,0 +1,16 @@ |
|||||||
|
class CreateAnnotationDocuments < ActiveRecord::Migration[6.1] |
||||||
|
def change |
||||||
|
create_table :annotation_documents do |t| |
||||||
|
t.references :annotation, null: false |
||||||
|
t.references :document, null: false, polymorphic: true |
||||||
|
t.integer :rank, null: false, default: 1 |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
|
||||||
|
add_index :annotation_documents, |
||||||
|
%i[annotation_id document_id document_type], |
||||||
|
name: "index_annotation_documents_uniqueness", |
||||||
|
unique: true |
||||||
|
end |
||||||
|
end |
||||||
Loading…
Reference in new issue