From 0e387ffda20fe27ec731afab1e4e9b1232bed624 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 22 Sep 2023 12:55:20 +0200 Subject: [PATCH] Add association for Comments and RequestForComments For the submission and comments, I mainly decided to use a `has_one` association. Based on the database schema, multiple request for comments could be allowed (i.e., for each file or submission), but this won't happen practically (since we always create new submissions and files). Hence, the `has_one` association is representing our relationship better. --- app/controllers/user_exercise_feedbacks_controller.rb | 4 ++-- app/helpers/action_cable_helper.rb | 2 +- app/models/code_ocean/file.rb | 1 + app/models/comment.rb | 6 ++---- app/models/exercise.rb | 1 + app/models/request_for_comment.rb | 4 ---- app/models/submission.rb | 1 + app/models/user.rb | 2 ++ app/views/request_for_comments/_list_entry.html.slim | 2 +- app/views/request_for_comments/index.html.slim | 2 +- 10 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/controllers/user_exercise_feedbacks_controller.rb b/app/controllers/user_exercise_feedbacks_controller.rb index e4e51285..319d2b42 100644 --- a/app/controllers/user_exercise_feedbacks_controller.rb +++ b/app/controllers/user_exercise_feedbacks_controller.rb @@ -41,9 +41,9 @@ class UserExerciseFeedbacksController < ApplicationController Sentry.set_extras(params: uef_params) @exercise = Exercise.find(uef_params[:exercise_id]) - rfc = RequestForComment.unsolved.where(exercise_id: @exercise.id, user: current_user).first + rfc = RequestForComment.unsolved.where(exercise: @exercise, user: current_user).first submission = begin - current_contributor.submissions.where(exercise_id: @exercise.id).order('created_at DESC').first + current_contributor.submissions.where(exercise: @exercise).order(created_at: :desc).first rescue StandardError nil end diff --git a/app/helpers/action_cable_helper.rb b/app/helpers/action_cable_helper.rb index 27978041..254ebf8c 100644 --- a/app/helpers/action_cable_helper.rb +++ b/app/helpers/action_cable_helper.rb @@ -22,7 +22,7 @@ module ActionCableHelper def trigger_rfc_action_cable_from_comment # Context: Comment - RequestForComment.find_by(submission: file.context).trigger_rfc_action_cable + request_for_comment.trigger_rfc_action_cable end def trigger_working_times_action_cable diff --git a/app/models/code_ocean/file.rb b/app/models/code_ocean/file.rb index 008555cc..4282a640 100644 --- a/app/models/code_ocean/file.rb +++ b/app/models/code_ocean/file.rb @@ -32,6 +32,7 @@ module CodeOcean has_many :files, class_name: 'CodeOcean::File' has_many :testruns has_many :comments + has_one :request_for_comment has_many :events_synchronized_editor, class_name: 'Event::SynchronizedEditor' alias descendants files diff --git a/app/models/comment.rb b/app/models/comment.rb index 9d67fbfa..4674aec7 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -8,12 +8,10 @@ class Comment < ApplicationRecord attr_accessor :username, :date, :updated, :editable belongs_to :file, class_name: 'CodeOcean::File' + has_one :submission, through: :file, source: :context, source_type: 'Submission' + has_one :request_for_comment, through: :submission # after_save :trigger_rfc_action_cable_from_comment - def request_for_comment - RequestForComment.find_by(submission_id: file.context.id) - end - def only_comment_for_rfc? request_for_comment.comments.one? end diff --git a/app/models/exercise.rb b/app/models/exercise.rb index 136220fc..b3d429e9 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -33,6 +33,7 @@ class Exercise < ApplicationRecord has_many :internal_users, source: :contributor, source_type: 'InternalUser', through: :submissions has_many :programming_groups has_many :pair_programming_waiting_users + has_many :request_for_comments scope :with_submissions, -> { where('id IN (SELECT exercise_id FROM submissions)') } diff --git a/app/models/request_for_comment.rb b/app/models/request_for_comment.rb index 98ce50c6..15078b5c 100644 --- a/app/models/request_for_comment.rb +++ b/app/models/request_for_comment.rb @@ -22,10 +22,6 @@ class RequestForComment < ApplicationRecord # after_save :trigger_rfc_action_cable - def comments_count - submission.files.sum {|file| file.comments.size } - end - def commenters comments.map(&:user).uniq end diff --git a/app/models/submission.rb b/app/models/submission.rb index c515a5e5..7220bff3 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -17,6 +17,7 @@ class Submission < ApplicationRecord has_many :testruns has_many :structured_errors, dependent: :destroy has_many :comments, through: :files + has_one :request_for_comment has_one :user_exercise_feedback has_one :pair_programming_exercise_feedback diff --git a/app/models/user.rb b/app/models/user.rb index 9993e024..6d08f9cb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,7 @@ class User < ApplicationRecord belongs_to :consumer has_many :anomaly_notifications, as: :contributor, dependent: :destroy has_many :authentication_token, dependent: :destroy + has_many :comments, as: :user has_many :study_group_memberships, as: :user has_many :study_groups, through: :study_group_memberships, as: :user has_many :programming_group_memberships, as: :user @@ -21,6 +22,7 @@ class User < ApplicationRecord has_many :testruns, as: :user has_many :interventions, through: :user_exercise_interventions has_many :remote_evaluation_mappings, as: :user + has_many :request_for_comments, as: :user has_many :runners, as: :contributor has_many :events has_many :events_synchronized_editor, class_name: 'Event::SynchronizedEditor' diff --git a/app/views/request_for_comments/_list_entry.html.slim b/app/views/request_for_comments/_list_entry.html.slim index 7237bc37..1c841138 100644 --- a/app/views/request_for_comments/_list_entry.html.slim +++ b/app/views/request_for_comments/_list_entry.html.slim @@ -6,7 +6,7 @@ tr.table-row-clickable data-id=request_for_comment.id data-href=request_for_comm span.fa-solid.fa-check.fa-2x style="color: var(--bs-secondary-text-emphasis);" aria-hidden="true" - else = '' - td.text-center = request_for_comment.comments_count + td.text-center = request_for_comment.comments.size - if request_for_comment.has_attribute?(:question) && request_for_comment.question.present? td.text-primary = truncate(request_for_comment.question, length: 200) - else diff --git a/app/views/request_for_comments/index.html.slim b/app/views/request_for_comments/index.html.slim index 929d34e2..e99b83aa 100644 --- a/app/views/request_for_comments/index.html.slim +++ b/app/views/request_for_comments/index.html.slim @@ -45,7 +45,7 @@ h1 = RequestForComment.model_name.human(count: 2) td = truncate(request_for_comment.question, length: 200) - else td = '-' - td = request_for_comment.comments_count + td = request_for_comment.comments.size td = link_to_if(request_for_comment.user && policy(request_for_comment.user).show?, request_for_comment.user.displayname, request_for_comment.user) td = t('shared.time.before', time: distance_of_time_in_words_to_now(request_for_comment.created_at)) td = t('shared.time.before', time: distance_of_time_in_words_to_now(request_for_comment.last_comment.nil? ? request_for_comment.updated_at : request_for_comment.last_comment))