Description:
fixed bug: increament of number on old records in submission
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@98 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r46:c9ffe114dd77 - - 1 file changed: 3 inserted, 2 deleted
@@ -1,25 +1,25 | |||||
|
1 | class Submission < ActiveRecord::Base |
|
1 | class Submission < ActiveRecord::Base |
|
2 |
|
2 | ||
|
3 | belongs_to :language |
|
3 | belongs_to :language |
|
4 | belongs_to :problem |
|
4 | belongs_to :problem |
|
5 | belongs_to :user |
|
5 | belongs_to :user |
|
6 |
|
6 | ||
|
7 | validates_presence_of :source |
|
7 | validates_presence_of :source |
|
8 | validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long' |
|
8 | validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long' |
|
9 | validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short' |
|
9 | validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short' |
|
10 | validate :must_specify_language |
|
10 | validate :must_specify_language |
|
11 | validate :must_have_valid_problem |
|
11 | validate :must_have_valid_problem |
|
12 |
|
12 | ||
|
13 | - before_save :assign_latest_number |
|
13 | + before_save :assign_latest_number_if_new_recond |
|
14 |
|
14 | ||
|
15 | def self.find_last_by_user_and_problem(user_id, problem_id) |
|
15 | def self.find_last_by_user_and_problem(user_id, problem_id) |
|
16 | last_sub = find(:first, |
|
16 | last_sub = find(:first, |
|
17 | :conditions => {:user_id => user_id, |
|
17 | :conditions => {:user_id => user_id, |
|
18 | :problem_id => problem_id}, |
|
18 | :problem_id => problem_id}, |
|
19 | :order => 'submitted_at DESC') |
|
19 | :order => 'submitted_at DESC') |
|
20 | return last_sub |
|
20 | return last_sub |
|
21 | end |
|
21 | end |
|
22 |
|
22 | ||
|
23 | def self.find_all_last_by_problem(problem_id) |
|
23 | def self.find_all_last_by_problem(problem_id) |
|
24 | # need to put in SQL command, maybe there's a better way |
|
24 | # need to put in SQL command, maybe there's a better way |
|
25 | Submission.find_by_sql("SELECT * FROM submissions " + |
|
25 | Submission.find_by_sql("SELECT * FROM submissions " + |
@@ -102,18 +102,19 | |||||
|
102 | problem = Problem.find(self.problem_id) |
|
102 | problem = Problem.find(self.problem_id) |
|
103 | else |
|
103 | else |
|
104 | problem = Submission.find_problem_in_source(self.source) |
|
104 | problem = Submission.find_problem_in_source(self.source) |
|
105 | end |
|
105 | end |
|
106 | if problem==nil |
|
106 | if problem==nil |
|
107 | errors.add_to_base("must specify problem") |
|
107 | errors.add_to_base("must specify problem") |
|
108 | elsif !problem.available |
|
108 | elsif !problem.available |
|
109 | errors.add_to_base("must specify valid problem") |
|
109 | errors.add_to_base("must specify valid problem") |
|
110 | end |
|
110 | end |
|
111 | end |
|
111 | end |
|
112 |
|
112 | ||
|
113 | # callbacks |
|
113 | # callbacks |
|
114 | - def assign_latest_number |
|
114 | + def assign_latest_number_if_new_recond |
|
|
115 | + return if !self.new_record? | ||
|
115 | latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id) |
|
116 | latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id) |
|
116 | self.number = (latest==nil) ? 1 : latest.number + 1; |
|
117 | self.number = (latest==nil) ? 1 : latest.number + 1; |
|
117 | end |
|
118 | end |
|
118 |
|
119 | ||
|
119 | end |
|
120 | end |
You need to be logged in to leave comments.
Login now