-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask_contributions_controller.rb
More file actions
118 lines (98 loc) · 3.91 KB
/
task_contributions_controller.rb
File metadata and controls
118 lines (98 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true
class TaskContributionsController < ApplicationController
include TaskParameters
before_action :load_and_authorize_base_task
before_action :load_and_authorize_task_contribution, except: %i[index create new]
def approve_changes
if @task.apply_contribution(@task_contribution)
TaskContributionMailer.with(task_contrib: @task_contribution).send_approval_info.deliver_later
redirect_to @task, notice: t('.success'), status: :see_other
else
redirect_to [@task, @task_contribution], alert: t('.error'), status: :see_other
end
end
def discard_changes
duplicate = @task_contribution.decouple
if duplicate
redirect_to duplicate, notice: t('.success'), status: :see_other
else
redirect_to [@task, @task_contribution], alert: t('.error'), status: :see_other
end
end
def reject_changes
duplicate = @task_contribution.decouple
if duplicate
TaskContributionMailer.with(task_contrib: @task_contribution, duplicate:).send_rejection_info.deliver_later
redirect_to @task, notice: t('.success'), status: :see_other
else
redirect_to [@task, @task_contribution], alert: t('.error'), status: :see_other
end
end
def index
@task_contributions = @task.contributions.order(created_at: :desc).paginate(page: params[:page], per_page: per_page_param)
raise Pundit::NotAuthorizedError unless policy(@task_contributions).index? base: @task
end
def show
@task = @task_contribution.suggestion
@files = @task.files
@tests = @task.tests
@model_solutions = @task.model_solutions
@proforma_valid = ProformaService::Validation.call(task: @task)
@user_rating = @task.ratings&.find_by(user: current_user)&.rating
render 'tasks/show'
end
def new
raise Pundit::NotAuthorizedError if current_user.nil?
@task_contribution = TaskContribution.new_for(@task, current_user)
authorize @task_contribution
@task = @task_contribution.suggestion
end
# The function should render the edit form used by TaskController
def edit
@task = @task_contribution.suggestion
render 'tasks/edit'
end
def create # rubocop:disable Metrics/AbcSize
@task_contribution = TaskContribution.new(suggestion_attributes: task_params, base: @task)
@task_contribution.suggestion.assign_attributes(
user: current_user,
access_level: :private,
meta_data: @task.meta_data,
submission_restrictions: @task.submission_restrictions,
external_resources: @task.external_resources,
grading_hints: @task.grading_hints
)
authorize @task_contribution
if @task_contribution.save(context: :force_validations)
TaskContributionMailer.with(task_contrib: @task_contribution).send_contribution_request.deliver_later
redirect_to [@task, @task_contribution],
notice: t('common.notices.object_created', model: TaskContribution.model_name.human),
status: :see_other
else
@task = @task_contribution.suggestion
render 'tasks/new', status: :unprocessable_content
end
end
def update
@task_contribution.suggestion.assign_attributes(task_params.except(:parent_uuid))
if @task_contribution.save(context: :force_validations)
redirect_to [@task, @task_contribution],
notice: t('common.notices.object_updated', model: TaskContribution.model_name.human),
status: :see_other
else
@task = @task_contribution.suggestion
render 'tasks/edit', danger: t('common.errors.changes_not_saved', model: TaskContribution.model_name.human),
status: :unprocessable_content
end
end
private
def load_and_authorize_base_task
@task = Task.find(params[:task_id])
authorize @task, :show?
end
def load_and_authorize_task_contribution
@task_contribution = TaskContribution.find(params[:id])
raise Pundit::NotAuthorizedError unless @task_contribution.base == @task
authorize @task_contribution
end
end