From 552ededc96707e17b65562469c993693f7107ceb Mon Sep 17 00:00:00 2001 From: Angel Aviel Domaoan Date: Thu, 20 Jan 2022 09:31:24 +0000 Subject: [PATCH] Add essential models --- app/models/annomark.rb | 8 ++++ app/models/annotation.rb | 29 ++++++++++++++ app/models/annotation_document.rb | 9 +++++ app/models/cdao/jurisprudence.rb | 2 + app/models/cdao/subject.rb | 3 ++ app/models/doctrine.rb | 40 +++++++++++++++++++ app/models/doctrine_subject.rb | 7 ++++ db/migrate/20220120043115_create_annomarks.rb | 13 ++++++ db/migrate/20220120043914_create_doctrines.rb | 16 ++++++++ ...20220120044038_create_doctrine_subjects.rb | 13 ++++++ .../20220120044454_create_annotations.rb | 19 +++++++++ ...20120070035_create_annotation_documents.rb | 16 ++++++++ db/seeds.rb | 33 +++++++++++++++ 13 files changed, 208 insertions(+) create mode 100644 app/models/annomark.rb create mode 100644 app/models/annotation.rb create mode 100644 app/models/annotation_document.rb create mode 100644 app/models/doctrine.rb create mode 100644 app/models/doctrine_subject.rb create mode 100644 db/migrate/20220120043115_create_annomarks.rb create mode 100644 db/migrate/20220120043914_create_doctrines.rb create mode 100644 db/migrate/20220120044038_create_doctrine_subjects.rb create mode 100644 db/migrate/20220120044454_create_annotations.rb create mode 100644 db/migrate/20220120070035_create_annotation_documents.rb diff --git a/app/models/annomark.rb b/app/models/annomark.rb new file mode 100644 index 0000000..7c705c7 --- /dev/null +++ b/app/models/annomark.rb @@ -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 diff --git a/app/models/annotation.rb b/app/models/annotation.rb new file mode 100644 index 0000000..2538411 --- /dev/null +++ b/app/models/annotation.rb @@ -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 diff --git a/app/models/annotation_document.rb b/app/models/annotation_document.rb new file mode 100644 index 0000000..322a40e --- /dev/null +++ b/app/models/annotation_document.rb @@ -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 diff --git a/app/models/cdao/jurisprudence.rb b/app/models/cdao/jurisprudence.rb index 9a62328..4c6454f 100644 --- a/app/models/cdao/jurisprudence.rb +++ b/app/models/cdao/jurisprudence.rb @@ -1,6 +1,8 @@ class Cdao::Jurisprudence < Cdao::Base self.table_name = "jurisprudences" + has_many :doctrines, as: :document, dependent: :restrict_with_error + alias_attribute :doc_date, :docdate searchable do diff --git a/app/models/cdao/subject.rb b/app/models/cdao/subject.rb index b739703..6bb402f 100644 --- a/app/models/cdao/subject.rb +++ b/app/models/cdao/subject.rb @@ -10,6 +10,9 @@ class Cdao::Subject < Cdao::Base has_many :libraries, through: :library_subjects alias_attribute :producttitles, :libraries + has_many :doctrine_subjects, class_name: "::DoctrineSubject", dependent: :destroy + has_many :doctrines, through: :doctrine_subjects + validates :name, presence: true validates :name, uniqueness: { scope: %i[ancestry] } diff --git a/app/models/doctrine.rb b/app/models/doctrine.rb new file mode 100644 index 0000000..35c5b9b --- /dev/null +++ b/app/models/doctrine.rb @@ -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 diff --git a/app/models/doctrine_subject.rb b/app/models/doctrine_subject.rb new file mode 100644 index 0000000..c5abe6e --- /dev/null +++ b/app/models/doctrine_subject.rb @@ -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 diff --git a/db/migrate/20220120043115_create_annomarks.rb b/db/migrate/20220120043115_create_annomarks.rb new file mode 100644 index 0000000..72a1b42 --- /dev/null +++ b/db/migrate/20220120043115_create_annomarks.rb @@ -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 diff --git a/db/migrate/20220120043914_create_doctrines.rb b/db/migrate/20220120043914_create_doctrines.rb new file mode 100644 index 0000000..d6dcd73 --- /dev/null +++ b/db/migrate/20220120043914_create_doctrines.rb @@ -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 diff --git a/db/migrate/20220120044038_create_doctrine_subjects.rb b/db/migrate/20220120044038_create_doctrine_subjects.rb new file mode 100644 index 0000000..64dfab4 --- /dev/null +++ b/db/migrate/20220120044038_create_doctrine_subjects.rb @@ -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 diff --git a/db/migrate/20220120044454_create_annotations.rb b/db/migrate/20220120044454_create_annotations.rb new file mode 100644 index 0000000..6bfe919 --- /dev/null +++ b/db/migrate/20220120044454_create_annotations.rb @@ -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 diff --git a/db/migrate/20220120070035_create_annotation_documents.rb b/db/migrate/20220120070035_create_annotation_documents.rb new file mode 100644 index 0000000..1f14701 --- /dev/null +++ b/db/migrate/20220120070035_create_annotation_documents.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index f3a0480..674ab62 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,36 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + +if Rails.env.development? + +annomarks = { + "A" => "Affirmed", + "C" => "Cited", + "D" => "Distinguished", + "F" => "Footnote", + "I" => "Illustrative", + "M" => "Modified", + "O" => "Overruled", + "CO" => "Concurring Opinion", + "SO" => "Seperated Opinion" +} + +annomarks.each { |k, v| Annomark.create(name: v, code: k) } if Annomark.count.zero? + +Doctrine.destroy_all + +doctrine = Doctrine.new +doctrine.document = Cdao::Jurisprudence.first +doctrine.content = "Dasdsadsadsa" +doctrine.save +doctrine.subject_ids = Cdao::Subject.first(3).map(&:id) + +doctrine = Doctrine.first +annotation = doctrine.annotations.new +annotation.document = Cdao::Jurisprudence.second +annotation.annomark = Annomark.first +annotation.save + + +end \ No newline at end of file