Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reuse problems across contests.

  • Loading branch information...
commit 20de12d191357ca758e32d5e0059a5989af96e32 1 parent 5d79356
@andmej authored
View
18 app/models/contest.rb
@@ -1,13 +1,11 @@
class Contest < ActiveRecord::Base
has_and_belongs_to_many :teams
- has_many :problems, :dependent => :destroy
+ has_and_belongs_to_many :problems
has_many :submissions, :dependent => :destroy
-
+
validates :name, :start_date, :end_date, :presence => true
validate :start_date_must_be_before_end_date
-
- accepts_nested_attributes_for :problems, :reject_if => proc { |attr| attr["number"].blank? and attr["judge_url"].blank? }, :allow_destroy => :true
-
+
def teams_attributes=(attributes)
attributes.reject! do |index, team_attributes|
team_attributes["username"].blank? or ActiveRecord::ConnectionAdapters::Column.value_to_boolean(team_attributes.delete("_destroy"))
@@ -18,6 +16,16 @@ def teams_attributes=(attributes)
end
end
+ def problems_attributes=(attributes)
+ attributes.reject! do |index, problem_attributes|
+ problem_attributes["number"].blank? and problem_attributes["judge_url"].blank? or ActiveRecord::ConnectionAdapters::Column.value_to_boolean(problem_attributes.delete("_destroy"))
+ end
+ self.problems = []
+ self.problems = attributes.collect do |index, problem_attributes|
+ Problem.where(:number => problem_attributes["number"]).first || Problem.new(problem_attributes)
+ end
+ end
+
def duration # in minutes
(end_date - start_date) / 60
end
View
2  app/models/problem.rb
@@ -1,5 +1,5 @@
class Problem < ActiveRecord::Base
validates_presence_of :number, :judge_url
validates_numericality_of :number
- belongs_to :contest
+ has_and_belongs_to_many :contests
end
View
14 db/migrate/20110926232529_create_contests_problems_join_table.rb
@@ -0,0 +1,14 @@
+class CreateContestsProblemsJoinTable < ActiveRecord::Migration
+ def up
+ remove_column :problems, :contest_id
+ create_table :contests_problems, :id => false do |t|
+ t.integer :contest_id
+ t.integer :problem_id
+ end
+ end
+
+ def down
+ drop_table :contests_problems
+ add_column :problems, :contest_id, :integer
+ end
+end
View
8 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110926222914) do
+ActiveRecord::Schema.define(:version => 20110926232529) do
create_table "contests", :force => true do |t|
t.string "name"
@@ -21,6 +21,11 @@
t.datetime "updated_at"
end
+ create_table "contests_problems", :id => false, :force => true do |t|
+ t.integer "contest_id"
+ t.integer "problem_id"
+ end
+
create_table "contests_teams", :id => false, :force => true do |t|
t.integer "contest_id"
t.integer "team_id"
@@ -29,7 +34,6 @@
create_table "problems", :force => true do |t|
t.integer "number"
t.string "judge_url"
- t.integer "contest_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Please sign in to comment.
Something went wrong with that request. Please try again.