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/request_for_comment.rb b/app/models/request_for_comment.rb index aca99938..2107c31e 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 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/show.html.erb b/app/views/request_for_comments/show.html.erb index a1ffb5ff..606327bb 100644 --- a/app/views/request_for_comments/show.html.erb +++ b/app/views/request_for_comments/show.html.erb @@ -21,6 +21,11 @@ <%= t('request_for_comments.no_question') %> <% end %> + <% if (policy(@request_for_comment).mark_as_solved? and not @request_for_comment.solved?) %> + + <% else %> + + <% end %>