Browse Source

Add essential models

pull/3/head
Angel Aviel Domaoan 4 years ago
parent
commit
552ededc96
  1. 8
      app/models/annomark.rb
  2. 29
      app/models/annotation.rb
  3. 9
      app/models/annotation_document.rb
  4. 2
      app/models/cdao/jurisprudence.rb
  5. 3
      app/models/cdao/subject.rb
  6. 40
      app/models/doctrine.rb
  7. 7
      app/models/doctrine_subject.rb
  8. 13
      db/migrate/20220120043115_create_annomarks.rb
  9. 16
      db/migrate/20220120043914_create_doctrines.rb
  10. 13
      db/migrate/20220120044038_create_doctrine_subjects.rb
  11. 19
      db/migrate/20220120044454_create_annotations.rb
  12. 16
      db/migrate/20220120070035_create_annotation_documents.rb
  13. 33
      db/seeds.rb

8
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

29
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

9
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

2
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

3
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] }

40
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

7
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

13
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

16
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

13
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

19
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

16
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

33
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
Loading…
Cancel
Save