/
submission.rb
59 lines (44 loc) · 1.28 KB
/
submission.rb
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
class Submission
include ActiveModel::Validations
include ActiveModel::Naming
attr_reader :code
validates :code, presence: { message: 'не сте предали код' }
validate :task_must_be_open
validate :code_is_parsable_and_compliant_with_restrictions
def initialize(user, task, code)
@user = user
@task = task
@code = code
end
def submit
return false unless valid?
solution = Solution.for(@user, @task)
if solution
Revision.create! solution: solution, code: @code unless solution.code == @code
else
solution = Solution.create! user_id: @user.id, task_id: @task.id
Revision.create! solution: solution, code: @code
end
true
end
def violating_restrictions?
violations.present?
end
def violations
return unless Language.can_lint?
@violations ||= Language.lint(code, @task.restrictions_hash).join("\n")
end
private
def task_must_be_open
errors.add :base, 'задачата е затворена' if @task.closed?
end
def code_is_parsable_and_compliant_with_restrictions
unless Language.parsing? code
errors.add :code, 'имате синтактична грешка'
return
end
if violating_restrictions?
errors.add :code, violations
end
end
end