Move methods to model

This commit is contained in:
Maximilian Grundke
2018-02-28 13:41:47 +01:00
parent 7b93caf4b3
commit 6e022528a0
2 changed files with 27 additions and 22 deletions

View File

@ -14,43 +14,34 @@ class RequestForCommentsController < ApplicationController
def index
@search = RequestForComment
.last_per_user(2)
.joins('join "submissions" s on s.id = request_for_comments.submission_id
left outer join "files" f on f.context_id = s.id
left outer join "comments" on comments.file_id = f.id')
.group('request_for_comments.id, request_for_comments.user_id, request_for_comments.exercise_id,
request_for_comments.file_id, request_for_comments.question, request_for_comments.created_at,
request_for_comments.updated_at, request_for_comments.user_type, request_for_comments.solved,
request_for_comments.full_score_reached, request_for_comments.submission_id, request_for_comments.row_number') # ugly, but rails wants it this way
.select('request_for_comments.*, max(comments.updated_at) as last_comment')
.with_last_activity
.search(params[:q])
@request_for_comments = @search.result.order('created_at DESC').paginate(page: params[:page], total_entries: @search.result.length)
@request_for_comments = @search.result
.order('created_at DESC')
.paginate(page: params[:page], total_entries: @search.result.length)
authorize!
end
def get_my_comment_requests
@search = RequestForComment
.with_last_activity
.where(user_id: current_user.id)
.joins('join "submissions" s on s.id = request_for_comments.submission_id
left outer join "files" f on f.context_id = s.id
left outer join "comments" on comments.file_id = f.id')
.group('request_for_comments.id')
.select('request_for_comments.*, max(comments.updated_at) as last_comment')
.search(params[:q])
@request_for_comments = @search.result.order('created_at DESC').paginate(page: params[:page])
@request_for_comments = @search.result
.order('created_at DESC')
.paginate(page: params[:page])
render 'index'
end
def get_rfcs_with_my_comments
@search = RequestForComment
.with_last_activity
.joins(:comments) # we don't need to outer join here, because we know the user has commented on these
.where(comments: {user_id: current_user.id})
.joins('join "submissions" s on s.id = request_for_comments.submission_id
left outer join "files" f on f.context_id = s.id
left outer join "comments" as c on c.file_id = f.id')
.group('request_for_comments.id')
.select('request_for_comments.*, max(c.updated_at) as last_comment')
.search(params[:q])
@request_for_comments = @search.result.order('last_comment DESC').paginate(page: params[:page])
@request_for_comments = @search.result
.order('last_comment DESC')
.paginate(page: params[:page])
render 'index'
end

View File

@ -10,7 +10,13 @@ class RequestForComment < ActiveRecord::Base
scope :unsolved, -> { where(solved: [false, nil]) }
def self.last_per_user(n = 5)
from("(#{row_number_user_sql}) as request_for_comments").where("row_number <= ?", n)
from("(#{row_number_user_sql}) as request_for_comments")
.where("row_number <= ?", n)
.group('request_for_comments.id, request_for_comments.user_id, request_for_comments.exercise_id,
request_for_comments.file_id, request_for_comments.question, request_for_comments.created_at,
request_for_comments.updated_at, request_for_comments.user_type, request_for_comments.solved,
request_for_comments.full_score_reached, request_for_comments.submission_id, request_for_comments.row_number')
# ugly, but necessary
end
# not used right now, finds the last submission for the respective user and exercise.
@ -46,6 +52,14 @@ class RequestForComment < ActiveRecord::Base
commenters.uniq {|user| user.id}
end
def self.with_last_activity
self.joins('join "submissions" s on s.id = request_for_comments.submission_id
left outer join "files" f on f.context_id = s.id
left outer join "comments" c on c.file_id = f.id')
.group('request_for_comments.id')
.select('request_for_comments.*, max(c.updated_at) as last_comment')
end
def to_s
"RFC-" + self.id.to_s
end