diff --git a/app/controllers/request_for_comments_controller.rb b/app/controllers/request_for_comments_controller.rb index 555dad09..72653ab2 100644 --- a/app/controllers/request_for_comments_controller.rb +++ b/app/controllers/request_for_comments_controller.rb @@ -1,5 +1,5 @@ class RequestForCommentsController < ApplicationController - before_action :set_request_for_comment, only: [:show, :edit, :update, :destroy] + before_action :set_request_for_comment, only: [:show, :edit, :update, :destroy, :mark_as_solved] skip_after_action :verify_authorized @@ -20,6 +20,18 @@ class RequestForCommentsController < ApplicationController render 'index' end + def mark_as_solved + authorize! + @request_for_comment.solved = true + respond_to do |format| + if @request_for_comment.save + format.json { render :show, status: :ok, location: @request_for_comment } + else + format.json { render json: @request_for_comment.errors, status: :unprocessable_entity } + end + end + end + # GET /request_for_comments/1 # GET /request_for_comments/1.json def show @@ -70,6 +82,6 @@ class RequestForCommentsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def request_for_comment_params - params.require(:request_for_comment).permit(:exercise_id, :file_id, :question, :requested_at).merge(user_id: current_user.id, user_type: current_user.class.name) + params.require(:request_for_comment).permit(:exercise_id, :file_id, :question, :requested_at, :solved).merge(user_id: current_user.id, user_type: current_user.class.name) end end diff --git a/app/models/code_ocean/file.rb b/app/models/code_ocean/file.rb index 58440f1b..22c6e877 100644 --- a/app/models/code_ocean/file.rb +++ b/app/models/code_ocean/file.rb @@ -35,6 +35,7 @@ module CodeOcean has_many :files has_many :testruns + has_many :comments alias_method :descendants, :files mount_uploader :native_file, FileUploader diff --git a/app/models/external_user.rb b/app/models/external_user.rb index 8038d2dc..24baa284 100644 --- a/app/models/external_user.rb +++ b/app/models/external_user.rb @@ -6,8 +6,10 @@ class ExternalUser < ActiveRecord::Base def displayname result = name - if(consumer.name == 'openHPI') - result = Xikolo::UserClient.get(external_id.to_s)[:display_name] + Rails.cache.fetch("#{cache_key}/displayname", expires_in: 12.hours) do + if(consumer.name == 'openHPI') + result = Xikolo::UserClient.get(external_id.to_s)[:display_name] + end end result end diff --git a/app/models/request_for_comment.rb b/app/models/request_for_comment.rb index aca99938..57b9a079 100644 --- a/app/models/request_for_comment.rb +++ b/app/models/request_for_comment.rb @@ -1,7 +1,7 @@ class RequestForComment < ActiveRecord::Base + include Creation belongs_to :exercise belongs_to :file, class_name: 'CodeOcean::File' - belongs_to :user, polymorphic: true before_create :set_requested_timestamp @@ -25,12 +25,16 @@ class RequestForComment < ActiveRecord::Base limit 1").first end + def comments_count + submission.files.map { |file| file.comments.size}.sum + end + def to_s "RFC-" + self.id.to_s end private def self.row_number_user_sql - select("id, user_id, exercise_id, file_id, question, requested_at, created_at, updated_at, user_type, row_number() OVER (PARTITION BY user_id ORDER BY created_at DESC) as row_number").to_sql + select("id, user_id, exercise_id, file_id, question, requested_at, created_at, updated_at, user_type, solved, row_number() OVER (PARTITION BY user_id ORDER BY created_at DESC) as row_number").to_sql end end diff --git a/app/policies/request_for_comment_policy.rb b/app/policies/request_for_comment_policy.rb index cf252338..f592e3bd 100644 --- a/app/policies/request_for_comment_policy.rb +++ b/app/policies/request_for_comment_policy.rb @@ -1,5 +1,8 @@ class RequestForCommentPolicy < ApplicationPolicy - + def author? + @user == @record.author + end + private :author? def create? everyone @@ -13,6 +16,10 @@ class RequestForCommentPolicy < ApplicationPolicy define_method(action) { admin? } end + def mark_as_solved? + admin? || author? + end + def edit? admin? end diff --git a/app/views/request_for_comments/index.html.slim b/app/views/request_for_comments/index.html.slim index 6d4e059c..3bdbe6d0 100644 --- a/app/views/request_for_comments/index.html.slim +++ b/app/views/request_for_comments/index.html.slim @@ -4,18 +4,28 @@ h1 = RequestForComment.model_name.human(count: 2) table.table.sortable thead tr + th + i class="fa fa-lightbulb-o" aria-hidden="true" title = t('request_for_comments.solved') align="right" th = t('activerecord.attributes.request_for_comments.exercise') th = t('activerecord.attributes.request_for_comments.question') + th + i class="fa fa-comment" aria-hidden="true" title = t('request_for_comments.comments') align="center" th = t('activerecord.attributes.request_for_comments.username') th = t('activerecord.attributes.request_for_comments.requested_at') tbody - @request_for_comments.each do |request_for_comment| tr data-id=request_for_comment.id + - if request_for_comment.solved? + td + span class="fa fa-check" aria-hidden="true" + - else + td = '' td = link_to(request_for_comment.exercise.title, request_for_comment) - if request_for_comment.has_attribute?(:question) && request_for_comment.question td = truncate(request_for_comment.question, length: 200) - else td = '-' + td = request_for_comment.comments_count td = request_for_comment.user.displayname td = t('shared.time.before', time: distance_of_time_in_words_to_now(request_for_comment.requested_at)) diff --git a/app/views/request_for_comments/show.html.erb b/app/views/request_for_comments/show.html.erb index a1ffb5ff..6ef176bd 100644 --- a/app/views/request_for_comments/show.html.erb +++ b/app/views/request_for_comments/show.html.erb @@ -14,13 +14,24 @@ %> <%= user.displayname %> | <%= @request_for_comment.requested_at %>
+