diff --git a/app/models/comments/assessment_comment.rb b/app/models/comments/assessment_comment.rb index 4944bf5ae..5c281d32c 100644 --- a/app/models/comments/assessment_comment.rb +++ b/app/models/comments/assessment_comment.rb @@ -1,13 +1,11 @@ class AssessmentComment < TaskComment - belongs_to :overseer_assessment, optional: false - before_create do self.content_type = :assessment end def serialize(user) json = super(user) - json[:overseer_assessment_id] = self.overseer_assessment_id + json[:overseer_assessment_id] = self.commentable_id json end end diff --git a/app/models/comments/scorm_comment.rb b/app/models/comments/scorm_comment.rb index 16502f50a..df7bcc9f5 100644 --- a/app/models/comments/scorm_comment.rb +++ b/app/models/comments/scorm_comment.rb @@ -1,6 +1,4 @@ class ScormComment < TaskComment - belongs_to :test_attempt, optional: false - before_create do self.content_type = :scorm end @@ -8,8 +6,8 @@ class ScormComment < TaskComment def serialize(user) json = super(user) json[:test_attempt] = { - id: self.test_attempt_id, - success_status: self.test_attempt.success_status + id: self.commentable_id, + success_status: self.commentable.success_status } json end diff --git a/app/models/comments/task_comment.rb b/app/models/comments/task_comment.rb index 36acb4e89..c74883d01 100644 --- a/app/models/comments/task_comment.rb +++ b/app/models/comments/task_comment.rb @@ -20,6 +20,9 @@ class TaskComment < ApplicationRecord # Can optionally be a reply to a comment belongs_to :task_comment, optional: true + # Can be a comment for different types of entities e.g. Test Attempt, Overseer Assessment + belongs_to :commentable, polymorphic: true, optional: true + validates :task, presence: true validates :user, presence: true validates :recipient, presence: true diff --git a/app/models/overseer_assessment.rb b/app/models/overseer_assessment.rb index afca988ad..bfbb8cb4b 100644 --- a/app/models/overseer_assessment.rb +++ b/app/models/overseer_assessment.rb @@ -3,7 +3,7 @@ class OverseerAssessment < ApplicationRecord belongs_to :task, optional: false has_one :project, through: :task - has_many :assessment_comments, dependent: :destroy + has_many :assessment_comments, as: :commentable, dependent: :destroy validates :status, presence: true validates :task_id, presence: true @@ -93,7 +93,7 @@ def add_assessment_comment(text = 'Automated Assessment Started') comment.user = tutor comment.comment = text comment.recipient = project.student - comment.overseer_assessment = self + comment.commentable = self comment.save! comment diff --git a/app/models/test_attempt.rb b/app/models/test_attempt.rb index 9d57c65bf..991841425 100644 --- a/app/models/test_attempt.rb +++ b/app/models/test_attempt.rb @@ -6,7 +6,7 @@ class TestAttempt < ApplicationRecord has_one :task_definition, through: :task - has_one :scorm_comment, dependent: :destroy + has_one :scorm_comment, as: :commentable, dependent: :destroy delegate :role_for, to: :task delegate :student, to: :task @@ -130,7 +130,7 @@ def add_scorm_comment comment.user = task.tutor comment.comment = success_status_description comment.recipient = task.student - comment.test_attempt = self + comment.commentable = self comment.save! comment diff --git a/db/migrate/20240601103707_add_polymorphic_association_to_comment.rb b/db/migrate/20240601103707_add_polymorphic_association_to_comment.rb new file mode 100644 index 000000000..f6828c0b2 --- /dev/null +++ b/db/migrate/20240601103707_add_polymorphic_association_to_comment.rb @@ -0,0 +1,12 @@ +class AddPolymorphicAssociationToComment < ActiveRecord::Migration[7.1] + def change + remove_index :task_comments, :overseer_assessment_id + + add_column :task_comments, :commentable_type, :string + rename_column :task_comments, :overseer_assessment_id, :commentable_id + + TaskComment.where('NOT commentable_id IS NULL').in_batches.update_all(commentable_type: 'OverseerAssessment') + + add_index :task_comments, [:commentable_type, :commentable_id] + end +end diff --git a/db/migrate/20240601103707_add_test_attempt_link_to_comment.rb b/db/migrate/20240601103707_add_test_attempt_link_to_comment.rb deleted file mode 100644 index 51db18b9e..000000000 --- a/db/migrate/20240601103707_add_test_attempt_link_to_comment.rb +++ /dev/null @@ -1,7 +0,0 @@ -class AddTestAttemptLinkToComment < ActiveRecord::Migration[7.1] - def change - # Link to corresponding SCORM test attempt for scorm comments - add_column :task_comments, :test_attempt_id, :integer - add_index :task_comments, :test_attempt_id - end -end diff --git a/db/schema.rb b/db/schema.rb index 786065928..a2b40c6ed 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -221,16 +221,15 @@ t.integer "extension_weeks" t.string "extension_response" t.bigint "reply_to_id" - t.bigint "overseer_assessment_id" - t.integer "test_attempt_id" + t.bigint "commentable_id" + t.string "commentable_type" t.index ["assessor_id"], name: "index_task_comments_on_assessor_id" + t.index ["commentable_type", "commentable_id"], name: "index_task_comments_on_commentable_type_and_commentable_id" t.index ["discussion_comment_id"], name: "index_task_comments_on_discussion_comment_id" - t.index ["overseer_assessment_id"], name: "index_task_comments_on_overseer_assessment_id" t.index ["recipient_id"], name: "fk_rails_1dbb49165b" t.index ["reply_to_id"], name: "index_task_comments_on_reply_to_id" t.index ["task_id"], name: "index_task_comments_on_task_id" t.index ["task_status_id"], name: "index_task_comments_on_task_status_id" - t.index ["test_attempt_id"], name: "index_task_comments_on_test_attempt_id" t.index ["user_id"], name: "index_task_comments_on_user_id" end diff --git a/test/api/test_attempts_test.rb b/test/api/test_attempts_test.rb index 6d8603320..5d5b4422f 100644 --- a/test/api/test_attempts_test.rb +++ b/test/api/test_attempts_test.rb @@ -400,7 +400,7 @@ def test_update_attempt assert attempt.terminated == true assert JSON.parse(attempt.cmi_datamodel)["cmi.completion_status"] == "completed" - tc = ScormComment.find_by(test_attempt_id: attempt.id) + tc = ScormComment.find_by(commentable_id: attempt.id) assert_not_nil tc @@ -421,7 +421,7 @@ def test_update_attempt assert attempt.success_status == true assert JSON.parse(attempt.cmi_datamodel)["cmi.success_status"] == "passed" - tc = ScormComment.find_by(test_attempt_id: attempt.id) + tc = ScormComment.find_by(commentable_id: attempt.id) assert tc.comment == attempt.success_status_description