Skip to content

Commit

Permalink
refactor: use poly assoc for task comments
Browse files Browse the repository at this point in the history
  • Loading branch information
satikaj committed Sep 8, 2024
1 parent 8b205ed commit b0e2321
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 24 deletions.
4 changes: 1 addition & 3 deletions app/models/comments/assessment_comment.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions app/models/comments/scorm_comment.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
class ScormComment < TaskComment
belongs_to :test_attempt, optional: false

before_create do
self.content_type = :scorm
end

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
Expand Down
3 changes: 3 additions & 0 deletions app/models/comments/task_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/overseer_assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/test_attempt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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.find_each do |comment|
if comment.commentable_id.present?
comment.update(commentable_type: 'OverseerAssessment')
end
end

add_index :task_comments, [:commentable_type, :commentable_id]
end
end
7 changes: 0 additions & 7 deletions db/migrate/20240601103707_add_test_attempt_link_to_comment.rb

This file was deleted.

7 changes: 3 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions test/api/test_attempts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit b0e2321

Please sign in to comment.