From 2cd474ceb0c4ab94eccd19d9fd3df90b596a366e Mon Sep 17 00:00:00 2001 From: Jan Renz Date: Tue, 21 Apr 2015 10:52:22 +0200 Subject: [PATCH] Avoid error if no submission is present (on first load of editor) --- app/controllers/comments_controller.rb | 40 +++++++++++++---------- app/controllers/submissions_controller.rb | 2 +- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 2ad52355..6d670497 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,27 +16,31 @@ class CommentsController < ApplicationController #if admin, show all comments. #check whether user is the author of the passed file_id, if so, show all comments. otherwise, only show comments of auther and own comments file = CodeOcean::File.find(params[:file_id]) - submission = Submission.find(file.context_id) + #there might be no submission yet, so dont use find + submission = Submission.find_by(id: file.context_id) + if submission + is_admin = false + if current_user.respond_to? :external_id + user_id = current_user.external_id + else + user_id = current_user.id + is_admin = current_user.role == 'admin' + end - is_admin = false - if current_user.respond_to? :external_id - user_id = current_user.external_id + if(is_admin || user_id == submission.user_id) + # fetch all comments for this file + @comments = Comment.where(file_id: params[:file_id]) + else + @comments = Comment.where(file_id: params[:file_id], user_id: user_id) + end + + #@comments = Comment.where(file_id: params[:file_id]) + + #add names to comments + @comments.map{|comment| comment.username = Xikolo::UserClient.get(comment.user_id.to_s)[:display_name]} else - user_id = current_user.id - is_admin = current_user.role == 'admin' + @comments = Comment.where(file_id: -1) #we need an empty relation here end - - if(is_admin || user_id == submission.user_id) - # fetch all comments for this file - @comments = Comment.where(file_id: params[:file_id]) - else - @comments = Comment.where(file_id: params[:file_id], user_id: user_id) - end - - #@comments = Comment.where(file_id: params[:file_id]) - - #add names to comments - @comments.map{|comment| comment.username = Xikolo::UserClient.get(comment.user_id.to_s)[:display_name]} authorize! end diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index c270480b..5e0ff61e 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -28,7 +28,7 @@ class SubmissionsController < ApplicationController # copy each annotation and set the target_file.id unless(params[:annotations_arr].nil?) params[:annotations_arr].each do | annotation | - comment = Comment.new(:user_id => annotation[1][:user_id], :file_id => annotation[1][:file_id], :user_type => 'InternalUser', :row => annotation[1][:row], :column => annotation[1][:column], :text => annotation[1][:text]) + comment = Comment.new(:user_id => annotation[1][:user_id], :file_id => annotation[1][:file_id], :user_type => current_user.user_type, :row => annotation[1][:row], :column => annotation[1][:column], :text => annotation[1][:text]) source_file = CodeOcean::File.find(annotation[1][:file_id]) #comment = Comment.new(annotation[1].permit(:user_id, :file_id, :user_type, :row, :column, :text, :created_at, :updated_at))