From 59af79f97ddd90bf482649bdab2345d6d98fbe8f Mon Sep 17 00:00:00 2001 From: Angel Aviel Domaoan Date: Fri, 28 Jan 2022 04:58:04 +0000 Subject: [PATCH] Implement multiple annomarks for annotations --- app/controllers/doctrine/annotations_controller.rb | 2 +- app/models/annomark.rb | 3 ++- app/models/annotation.rb | 6 ++---- app/models/annotation_annomark.rb | 7 +++++++ ...20128044651_remove_annomark_id_in_annotations.rb | 5 +++++ .../20220128044720_create_annotation_annomarks.rb | 13 +++++++++++++ 6 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 app/models/annotation_annomark.rb create mode 100644 db/migrate/20220128044651_remove_annomark_id_in_annotations.rb create mode 100644 db/migrate/20220128044720_create_annotation_annomarks.rb diff --git a/app/controllers/doctrine/annotations_controller.rb b/app/controllers/doctrine/annotations_controller.rb index 4ec173b..9fc9c1b 100644 --- a/app/controllers/doctrine/annotations_controller.rb +++ b/app/controllers/doctrine/annotations_controller.rb @@ -49,6 +49,6 @@ class Doctrine::AnnotationsController < ApplicationController private def resource_params - params.permit(:document_id, :annomark_id, :phil_rep, :editor_notes, :rank) + params.permit(:document_id, :phil_rep, :editor_notes, :rank, annomark_ids: []) end end diff --git a/app/models/annomark.rb b/app/models/annomark.rb index 7c705c7..aa67e64 100644 --- a/app/models/annomark.rb +++ b/app/models/annomark.rb @@ -4,5 +4,6 @@ class Annomark < ApplicationRecord validates :name, :code, presence: true validates :code, uniqueness: { scope: %i[name] } - has_many :annotations, dependent: :restrict_with_error + has_many :annotation_annomarks, dependent: :destroy + has_many :annotations, through: :annotation_annomarks end diff --git a/app/models/annotation.rb b/app/models/annotation.rb index 2538411..30129e8 100644 --- a/app/models/annotation.rb +++ b/app/models/annotation.rb @@ -1,8 +1,6 @@ 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 @@ -10,8 +8,8 @@ class Annotation < ApplicationRecord 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] } + has_many :annotation_annomarks, dependent: :destroy + has_many :annomarks, through: :annotation_annomarks def documents annotation_documents.collect(&:document) diff --git a/app/models/annotation_annomark.rb b/app/models/annotation_annomark.rb new file mode 100644 index 0000000..c7c349d --- /dev/null +++ b/app/models/annotation_annomark.rb @@ -0,0 +1,7 @@ +class AnnotationAnnomark < ApplicationRecord + belongs_to :annotation, optional: false + + belongs_to :annomark, optional: false + + validates :annomark_id, uniqueness: { scope: %i[annotation_id] } +end diff --git a/db/migrate/20220128044651_remove_annomark_id_in_annotations.rb b/db/migrate/20220128044651_remove_annomark_id_in_annotations.rb new file mode 100644 index 0000000..69f2a50 --- /dev/null +++ b/db/migrate/20220128044651_remove_annomark_id_in_annotations.rb @@ -0,0 +1,5 @@ +class RemoveAnnomarkIdInAnnotations < ActiveRecord::Migration[6.1] + def change + safety_assured { remove_column :annotations, :annomark_id, :integer, null: false } + end +end diff --git a/db/migrate/20220128044720_create_annotation_annomarks.rb b/db/migrate/20220128044720_create_annotation_annomarks.rb new file mode 100644 index 0000000..c6f6612 --- /dev/null +++ b/db/migrate/20220128044720_create_annotation_annomarks.rb @@ -0,0 +1,13 @@ +class CreateAnnotationAnnomarks < ActiveRecord::Migration[6.1] + def change + create_table :annotation_annomarks do |t| + t.references :annotation, null: false + t.references :annomark, null: false + + t.timestamps + end + + add_index :annotation_annomarks, %i[annotation_id annomark_id], + name: "index_annotation_annotation_marks_uniquness", unique: true + end +end