Move methods to model
This commit is contained in:
@ -14,43 +14,34 @@ class RequestForCommentsController < ApplicationController
|
|||||||
def index
|
def index
|
||||||
@search = RequestForComment
|
@search = RequestForComment
|
||||||
.last_per_user(2)
|
.last_per_user(2)
|
||||||
.joins('join "submissions" s on s.id = request_for_comments.submission_id
|
.with_last_activity
|
||||||
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')
|
|
||||||
.search(params[:q])
|
.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!
|
authorize!
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_my_comment_requests
|
def get_my_comment_requests
|
||||||
@search = RequestForComment
|
@search = RequestForComment
|
||||||
|
.with_last_activity
|
||||||
.where(user_id: current_user.id)
|
.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])
|
.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'
|
render 'index'
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_rfcs_with_my_comments
|
def get_rfcs_with_my_comments
|
||||||
@search = RequestForComment
|
@search = RequestForComment
|
||||||
|
.with_last_activity
|
||||||
.joins(:comments) # we don't need to outer join here, because we know the user has commented on these
|
.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})
|
.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])
|
.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'
|
render 'index'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -10,7 +10,13 @@ class RequestForComment < ActiveRecord::Base
|
|||||||
scope :unsolved, -> { where(solved: [false, nil]) }
|
scope :unsolved, -> { where(solved: [false, nil]) }
|
||||||
|
|
||||||
def self.last_per_user(n = 5)
|
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
|
end
|
||||||
|
|
||||||
# not used right now, finds the last submission for the respective user and exercise.
|
# 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}
|
commenters.uniq {|user| user.id}
|
||||||
end
|
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
|
def to_s
|
||||||
"RFC-" + self.id.to_s
|
"RFC-" + self.id.to_s
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user