Description:
update models to satisfy rails 4
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r618:45e8321629ae - - 7 files changed: 12 inserted, 16 deleted

@@ -1,8 +1,8
1 class Contest < ActiveRecord::Base
1 class Contest < ActiveRecord::Base
2
2
3 has_and_belongs_to_many :users
3 has_and_belongs_to_many :users
4 has_and_belongs_to_many :problems
4 has_and_belongs_to_many :problems
5
5
6 - scope :enabled, :conditions => {:enabled => true}
6 + scope :enabled, -> { where(enabled: true) }
7
7
8 end
8 end
@@ -1,5 +1,4
1 class Login < ActiveRecord::Base
1 class Login < ActiveRecord::Base
2 belongs_to :user
2 belongs_to :user
3
3
4 - attr_accessible :ip_address, :logged_in_at, :user_id
5 end
4 end
@@ -1,57 +1,57
1 class Problem < ActiveRecord::Base
1 class Problem < ActiveRecord::Base
2
2
3 belongs_to :description
3 belongs_to :description
4 has_and_belongs_to_many :contests, :uniq => true
4 has_and_belongs_to_many :contests, :uniq => true
5 has_many :test_pairs, :dependent => :delete_all
5 has_many :test_pairs, :dependent => :delete_all
6 has_many :testcases, :dependent => :destroy
6 has_many :testcases, :dependent => :destroy
7
7
8 validates_presence_of :name
8 validates_presence_of :name
9 - validates_format_of :name, :with => /^\w+$/
9 + validates_format_of :name, :with => /\A\w+\z/
10 validates_presence_of :full_name
10 validates_presence_of :full_name
11
11
12 scope :available, :conditions => {:available => true}
12 scope :available, :conditions => {:available => true}
13
13
14 DEFAULT_TIME_LIMIT = 1
14 DEFAULT_TIME_LIMIT = 1
15 DEFAULT_MEMORY_LIMIT = 32
15 DEFAULT_MEMORY_LIMIT = 32
16
16
17 def self.find_available_problems
17 def self.find_available_problems
18 Problem.available.all(:order => "date_added DESC, name ASC")
18 Problem.available.all(:order => "date_added DESC, name ASC")
19 end
19 end
20
20
21 def self.create_from_import_form_params(params, old_problem=nil)
21 def self.create_from_import_form_params(params, old_problem=nil)
22 org_problem = old_problem || Problem.new
22 org_problem = old_problem || Problem.new
23 import_params, problem = Problem.extract_params_and_check(params,
23 import_params, problem = Problem.extract_params_and_check(params,
24 org_problem)
24 org_problem)
25
25
26 if !problem.errors.empty?
26 if !problem.errors.empty?
27 return problem, 'Error importing'
27 return problem, 'Error importing'
28 end
28 end
29
29
30 problem.full_score = 100
30 problem.full_score = 100
31 problem.date_added = Time.new
31 problem.date_added = Time.new
32 problem.test_allowed = true
32 problem.test_allowed = true
33 problem.output_only = false
33 problem.output_only = false
34 problem.available = false
34 problem.available = false
35
35
36 if not problem.save
36 if not problem.save
37 return problem, 'Error importing'
37 return problem, 'Error importing'
38 end
38 end
39
39
40 import_to_db = params.has_key? :import_to_db
40 import_to_db = params.has_key? :import_to_db
41
41
42 importer = TestdataImporter.new(problem)
42 importer = TestdataImporter.new(problem)
43
43
44 if not importer.import_from_file(import_params[:file],
44 if not importer.import_from_file(import_params[:file],
45 import_params[:time_limit],
45 import_params[:time_limit],
46 import_params[:memory_limit],
46 import_params[:memory_limit],
47 import_params[:checker_name],
47 import_params[:checker_name],
48 import_to_db)
48 import_to_db)
49 problem.errors.add(:base,'Import error.')
49 problem.errors.add(:base,'Import error.')
50 end
50 end
51
51
52 return problem, importer.log_msg
52 return problem, importer.log_msg
53 end
53 end
54
54
55 def self.download_file_basedir
55 def self.download_file_basedir
56 return "#{Rails.root}/data/tasks"
56 return "#{Rails.root}/data/tasks"
57 end
57 end
@@ -1,3 +1,3
1 class SubmissionViewLog < ActiveRecord::Base
1 class SubmissionViewLog < ActiveRecord::Base
2 - attr_accessible :submission_id, :user_id
2 + #attr_accessible :submission_id, :user_id
3 end
3 end
@@ -1,68 +1,67
1 #
1 #
2 # A TestRequest is a composition of submission with user's testdata.
2 # A TestRequest is a composition of submission with user's testdata.
3 #
3 #
4 # Note about TestRequest#problem: Usually, A TestRequest has to be
4 # Note about TestRequest#problem: Usually, A TestRequest has to be
5 # associated with a problem, so that execution environment can be
5 # associated with a problem, so that execution environment can be
6 # determined. However, to be more flexible, we have to ensure that
6 # determined. However, to be more flexible, we have to ensure that
7 # it works as well with problem=nil. In this case, we shall provide
7 # it works as well with problem=nil. In this case, we shall provide
8 # a "default" execution environment for it. This can be done
8 # a "default" execution environment for it. This can be done
9 # seamlessly by using TestRequest#problem_name or
9 # seamlessly by using TestRequest#problem_name or
10 # TestRequest#name_of(problem) when retrieving the name of the
10 # TestRequest#name_of(problem) when retrieving the name of the
11 # problem: #name_of would return problem.name when problem!=nil and
11 # problem: #name_of would return problem.name when problem!=nil and
12 # it would return "default" when problem=nil, #problem_name just
12 # it would return "default" when problem=nil, #problem_name just
13 # call #name_of.
13 # call #name_of.
14 #
14 #
15
15
16 require 'fileutils'
16 require 'fileutils'
17
17
18 class TestRequest < Task
18 class TestRequest < Task
19 -
19 + self.table_name = "test_requests"
20 - set_table_name "test_requests"
21
20
22 belongs_to :user
21 belongs_to :user
23 belongs_to :problem
22 belongs_to :problem
24 belongs_to :submission
23 belongs_to :submission
25
24
26 validates_presence_of :submission
25 validates_presence_of :submission
27 validate :must_have_valid_problem
26 validate :must_have_valid_problem
28
27
29 def problem_name
28 def problem_name
30 TestRequest.name_of(self.problem)
29 TestRequest.name_of(self.problem)
31 end
30 end
32
31
33 def language
32 def language
34 self.submission.language
33 self.submission.language
35 end
34 end
36
35
37 def self.get_inqueue_and_change_status(status)
36 def self.get_inqueue_and_change_status(status)
38 # since there will be only one grader grading TestRequest
37 # since there will be only one grader grading TestRequest
39 # we do not need locking (hopefully)
38 # we do not need locking (hopefully)
40
39
41 test_request = TestRequest.find(:first,
40 test_request = TestRequest.find(:first,
42 :order => "created_at",
41 :order => "created_at",
43 :conditions => {:status=> Task::STATUS_INQUEUE})
42 :conditions => {:status=> Task::STATUS_INQUEUE})
44 if test_request!=nil
43 if test_request!=nil
45 test_request.status = status
44 test_request.status = status
46 test_request.save!
45 test_request.save!
47 end
46 end
48
47
49 test_request
48 test_request
50 end
49 end
51
50
52 # interfacing with form
51 # interfacing with form
53 def self.new_from_form_params(user,params)
52 def self.new_from_form_params(user,params)
54 test_request = TestRequest.new
53 test_request = TestRequest.new
55 test_request.user = user
54 test_request.user = user
56 begin
55 begin
57 problem = Problem.find(params[:problem_id])
56 problem = Problem.find(params[:problem_id])
58 rescue ActiveRecord::RecordNotFound
57 rescue ActiveRecord::RecordNotFound
59 problem = nil
58 problem = nil
60 end
59 end
61 test_request.problem = problem
60 test_request.problem = problem
62 if problem!=nil
61 if problem!=nil
63 test_request.submission =
62 test_request.submission =
64 Submission.find_by_user_problem_number(user.id,
63 Submission.find_by_user_problem_number(user.id,
65 problem.id,
64 problem.id,
66 params[:submission_number])
65 params[:submission_number])
67 else
66 else
68 test_request.submission = nil
67 test_request.submission = nil
@@ -1,4 +1,4
1 class Testcase < ActiveRecord::Base
1 class Testcase < ActiveRecord::Base
2 belongs_to :problem
2 belongs_to :problem
3 - attr_accessible :group, :input, :num, :score, :sol
3 + #attr_accessible :group, :input, :num, :score, :sol
4 end
4 end
@@ -1,78 +1,76
1 require 'digest/sha1'
1 require 'digest/sha1'
2 require 'net/pop'
2 require 'net/pop'
3 require 'net/https'
3 require 'net/https'
4 require 'net/http'
4 require 'net/http'
5 require 'json'
5 require 'json'
6
6
7 class User < ActiveRecord::Base
7 class User < ActiveRecord::Base
8
8
9 has_and_belongs_to_many :roles
9 has_and_belongs_to_many :roles
10
10
11 - has_many :test_requests, :order => "submitted_at DESC"
11 + has_many :test_requests, -> {order(submitted_at: DESC)}
12
12
13 - has_many :messages,
13 + has_many :messages, -> { order(created_at: DESC) },
14 :class_name => "Message",
14 :class_name => "Message",
15 - :foreign_key => "sender_id",
15 + :foreign_key => "sender_id"
16 - :order => 'created_at DESC'
17
16
18 - has_many :replied_messages,
17 + has_many :replied_messages, -> { order(created_at: DESC) },
19 :class_name => "Message",
18 :class_name => "Message",
20 - :foreign_key => "receiver_id",
19 + :foreign_key => "receiver_id"
21 - :order => 'created_at DESC'
22
20
23 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
21 has_one :contest_stat, :class_name => "UserContestStat", :dependent => :destroy
24
22
25 belongs_to :site
23 belongs_to :site
26 belongs_to :country
24 belongs_to :country
27
25
28 - has_and_belongs_to_many :contests, :uniq => true, :order => 'name'
26 + has_and_belongs_to_many :contests, -> { order(:name); uniq}
29
27
30 - scope :activated_users, :conditions => {:activated => true}
28 + scope :activated_users, -> {where activated: true}
31
29
32 validates_presence_of :login
30 validates_presence_of :login
33 validates_uniqueness_of :login
31 validates_uniqueness_of :login
34 validates_format_of :login, :with => /\A[\_A-Za-z0-9]+\z/
32 validates_format_of :login, :with => /\A[\_A-Za-z0-9]+\z/
35 validates_length_of :login, :within => 3..30
33 validates_length_of :login, :within => 3..30
36
34
37 validates_presence_of :full_name
35 validates_presence_of :full_name
38 validates_length_of :full_name, :minimum => 1
36 validates_length_of :full_name, :minimum => 1
39
37
40 validates_presence_of :password, :if => :password_required?
38 validates_presence_of :password, :if => :password_required?
41 validates_length_of :password, :within => 4..20, :if => :password_required?
39 validates_length_of :password, :within => 4..20, :if => :password_required?
42 validates_confirmation_of :password, :if => :password_required?
40 validates_confirmation_of :password, :if => :password_required?
43
41
44 validates_format_of :email,
42 validates_format_of :email,
45 :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
43 :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
46 :if => :email_validation?
44 :if => :email_validation?
47 validate :uniqueness_of_email_from_activated_users,
45 validate :uniqueness_of_email_from_activated_users,
48 :if => :email_validation?
46 :if => :email_validation?
49 validate :enough_time_interval_between_same_email_registrations,
47 validate :enough_time_interval_between_same_email_registrations,
50 :if => :email_validation?
48 :if => :email_validation?
51
49
52 # these are for ytopc
50 # these are for ytopc
53 # disable for now
51 # disable for now
54 #validates_presence_of :province
52 #validates_presence_of :province
55
53
56 attr_accessor :password
54 attr_accessor :password
57
55
58 before_save :encrypt_new_password
56 before_save :encrypt_new_password
59 before_save :assign_default_site
57 before_save :assign_default_site
60 before_save :assign_default_contest
58 before_save :assign_default_contest
61
59
62 # this is for will_paginate
60 # this is for will_paginate
63 cattr_reader :per_page
61 cattr_reader :per_page
64 @@per_page = 50
62 @@per_page = 50
65
63
66 def self.authenticate(login, password)
64 def self.authenticate(login, password)
67 user = find_by_login(login)
65 user = find_by_login(login)
68 if user
66 if user
69 return user if user.authenticated?(password)
67 return user if user.authenticated?(password)
70 if user.authenticated_by_cucas?(password) or user.authenticated_by_pop3?(password)
68 if user.authenticated_by_cucas?(password) or user.authenticated_by_pop3?(password)
71 user.password = password
69 user.password = password
72 user.save
70 user.save
73 return user
71 return user
74 end
72 end
75 end
73 end
76 end
74 end
77
75
78 def authenticated?(password)
76 def authenticated?(password)
You need to be logged in to leave comments. Login now