Dependent destroy #120

Merged
merged 26 commits into from Mar 29, 2012
Commits
+195 −62
Split
View
@@ -1,6 +1,8 @@
class Assistant < ActiveRecord::Base
- belongs_to :user
- has_many :assistant_registered_to_given_courses
+
+ belongs_to :user, class_name: "User"
+ has_many :assistant_registered_to_given_courses, dependent: :destroy
+
has_many :given_courses, through: :assistant_registered_to_given_courses
has_many :lab_has_groups, through: :assistant_registered_to_given_courses
has_many :lab_groups, through: :lab_has_groups
@@ -11,4 +13,4 @@ class Assistant < ActiveRecord::Base
has_many :students, through: :given_courses
validates_presence_of :user
-end
+end
View
@@ -1,5 +1,6 @@
class Course < ActiveRecord::Base
has_many :course_codes, inverse_of: :course
+ has_many :given_courses, dependent: :destroy
belongs_to :department, inverse_of: :courses
validate :course_codes_exists
@@ -1,8 +1,7 @@
class ExtendedDeadline < ActiveRecord::Base
- belongs_to :lab_group
- belongs_to :lab
+ belongs_to :lab_has_group
- validates_presence_of :lab, :lab_group, :at
+ validates_presence_of :lab_has_group, :at
validate :time_span, :time_difference
# What's the minimum time between
@@ -18,7 +17,7 @@ def time_span
def time_difference
in_valid = ExtendedDeadline.
- where("extended_deadlines.lab_group_id = ?", lab_group_id).
+ where("extended_deadlines.lab_has_group_id = ?", lab_has_group_id).
where("extended_deadlines.id != ?", id.to_i).
any? do |d|
(d.at.to_i - self.at.to_i).abs < MINIMUM_TIME_DIFFERENCE.to_i
@@ -4,14 +4,14 @@ class GivenCourse < ActiveRecord::Base
has_and_belongs_to_many :examiners
- has_many :student_registered_for_courses
+ has_many :student_registered_for_courses, dependent: :destroy
has_many :students, through: :student_registered_for_courses
- has_many :assistant_registered_to_given_courses
+ has_many :assistant_registered_to_given_courses, dependent: :destroy
has_many :assistants, through: :assistant_registered_to_given_courses
- has_many :lab_groups
- has_many :labs
+ has_many :lab_groups, dependent: :destroy
+ has_many :labs, dependent: :destroy
validates_presence_of :course, :examiners, :study_period
View
@@ -2,11 +2,11 @@ class Lab < ActiveRecord::Base
belongs_to :lab_description
belongs_to :given_course
- has_many :lab_has_groups
+ has_many :lab_has_groups, dependent: :destroy
has_many :lab_groups, through: :lab_has_groups
has_many :submissions, through: :lab_has_groups
- has_many :default_deadlines, inverse_of: :lab
-
+ has_many :default_deadlines, dependent: :destroy, inverse_of: :lab
+
has_one :initial_lab_commit_for_lab
has_one :initial_lab_commit, through: :initial_lab_commit_for_lab
@@ -45,4 +45,4 @@ def method_missing(meth, *args, &blk)
super
end
-end
+end
View
@@ -1,6 +1,6 @@
class LabGroup < ActiveRecord::Base
has_and_belongs_to_many :student_registered_for_courses
- has_many :lab_has_groups
+ has_many :lab_has_groups, dependent: :destroy
has_many :labs, through: :lab_has_groups
has_many :submissions, through: :lab_has_groups
belongs_to :given_course
@@ -6,6 +6,7 @@ class LabHasGroup < ActiveRecord::Base
has_one :assistant_registered_to_given_courses_lab_has_group
has_one :assistant_registered_to_given_course, through: :assistant_registered_to_given_courses_lab_has_group
has_one :assistant, through: :assistant_registered_to_given_course
+ has_many :extended_deadlines, dependent: :destroy
validates_presence_of :lab, :lab_group, :repository
validates_uniqueness_of :lab_id, scope: :lab_group_id
View
@@ -1,6 +1,6 @@
class Student < ActiveRecord::Base
belongs_to :user
- has_many :student_registered_for_courses
+ has_many :student_registered_for_courses, dependent: :destroy
has_many :lab_groups, through: :student_registered_for_courses
has_many :labs, through: :lab_groups
has_and_belongs_to_many :given_courses, join_table: "student_registered_for_courses"
View
@@ -4,22 +4,18 @@
class User < ActiveRecord::Base
include UrlLinting
+ has_one :assistant, dependent: :destroy
+ has_one :examiner, dependent: :destroy
+ has_one :student, dependent: :destroy
+ has_one :administrator, dependent: :destroy
has_many :student_registered_for_courses, through: :students
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
- has_many :repositories, :as => :owner, :dependent => :destroy
has_many :committerships, :as => :committer, :dependent => :destroy
has_many :commit_repositories, :through => :committerships, :source => :repository
has_many :ssh_keys, :order => "id desc", :dependent => :destroy
has_many :comments
has_many :email_aliases, :class_name => "Email", :dependent => :destroy
- has_many :events, :order => "events.created_at asc", :dependent => :destroy
- has_many :events_as_target, :class_name => "Event", :as => :target
- has_many :favorites, :dependent => :destroy
- has_one :administrator
- has_one :examiner
- has_one :assistant
- has_one :student
# Virtual attribute for the unencrypted password
attr_accessor :password, :current_password
@@ -380,6 +376,3 @@ def downcase_login
end
end
-class User
- has_one :student
-end
@@ -0,0 +1,11 @@
+class RemoveLabIdAndLabGroupIdFromExtendedDeadline < ActiveRecord::Migration
+ def up
+ remove_column :extended_deadlines, :lab_id
+ remove_column :extended_deadlines, :lab_group_id
+ end
+
+ def down
+ add_column :extended_deadlines, :lab_id, :integer
+ add_column :extended_deadlines, :lab_group_id, :integer
+ end
+end
@@ -0,0 +1,9 @@
+class AddLabHasGroupIdToExtendedDeadlines < ActiveRecord::Migration
+ def up
+ add_column :extended_deadlines, :lab_has_group_id, :integer
+ end
+
+ def down
+ remove_column :extended_deadlines, :lab_has_group_id
+ end
+end
View
@@ -11,7 +11,11 @@
#
# It's strongly recommended to check this file into your version control system.
+<<<<<<< HEAD
+ActiveRecord::Schema.define(:version => 20120321153606) do
+=======
ActiveRecord::Schema.define(:version => 20120327193409) do
+>>>>>>> master
create_table "administrators", :force => true do |t|
t.integer "user_id"
@@ -174,11 +178,10 @@
end
create_table "extended_deadlines", :force => true do |t|
- t.integer "lab_id"
- t.integer "lab_group_id"
t.datetime "at"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "lab_has_group_id"
end
create_table "favorites", :force => true do |t|
View
@@ -19,8 +19,7 @@
factory :extended_deadline do
at 3.days.from_now
- lab_group
- lab
+ lab_has_group
end
factory :student_registered_for_course do
@@ -44,4 +44,13 @@
create(:assistant, students: [create(:student)]).should have(1).students
end
end
+
+ describe "dependent destroy" do
+ it "should no be possible for a assistant_registered_to_given_course to exist without a assistant" do
+ assis = Factory.create(:assistant)
+ ar = Factory.create(:assistant_registered_to_given_course, assistant: assis)
+ assis.destroy
+ lambda{ar.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
end
@@ -29,4 +29,13 @@
CourseCode.find_by_code("TDA123").should_not be_nil
end
end
+
+ describe "dependent destroy" do
+ it "should no be possible for a given_course to exist without a course" do
+ c = Factory.create(:course_with_course_code)
+ gc = Factory.create(:given_course, course: c)
+ c.destroy
+ lambda{gc.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
end
@@ -8,26 +8,22 @@
build(:extended_deadline, at: 3.days.ago).should_not be_valid
end
- it "should have a lab" do
- build(:extended_deadline, lab: nil).should_not be_valid
- end
-
- it "should have a lab group" do
- build(:extended_deadline, lab_group: nil).should_not be_valid
+ it "should have a lab_has_group" do
+ build(:extended_deadline, lab_has_group: nil).should_not be_valid
end
it "should not be have two similar deadlines" do
- group1 = create(:lab_group)
- group2 = create(:lab_group)
+ group1 = create(:lab_has_group)
+ group2 = create(:lab_has_group)
create(:extended_deadline, {
at: 3.days.from_now,
- lab_group: group1
+ lab_has_group: group1
})
build(:extended_deadline, {
at: 3.days.from_now,
- lab_group: group1
+ lab_has_group: group1
}).should_not be_valid
# build(:extended_deadline, {
@@ -37,38 +33,30 @@
end
it "should validate #at with respect to ExtendedDeadline::MINIMUM_TIME_DIFFERENCE" do
- group = create(:lab_group)
- lab = create(:active_lab)
+ group = create(:lab_has_group)
max_diff = ExtendedDeadline::MINIMUM_TIME_DIFFERENCE
at = 1.day.from_now + max_diff + 1.minute
create(:extended_deadline, {
at: 1.day.from_now,
- lab_group: group,
- lab: lab
+ lab_has_group: group,
})
create(:extended_deadline, {
at: at,
- lab_group: group,
- lab: lab
+ lab_has_group: group,
}).should be_valid
build(:extended_deadline, {
at: at,
- lab_group: group,
- lab: lab
+ lab_has_group: group,
}).should_not be_valid
end
end
describe "relations" do
- it "belongs to a lab" do
- create(:extended_deadline).lab.should_not be_nil
- end
-
- it "belongs to a lab group" do
- create(:extended_deadline).lab_group.should_not be_nil
+ it "belongs to a lab_has_group" do
+ create(:extended_deadline).lab_has_group.should_not be_nil
end
end
end
@@ -46,7 +46,37 @@
create(:given_course, labs: [create(:lab)]).should have_at_least(1).labs
end
end
-
+
+ describe "dependant destroy" do
+ it "should no be possible for a student_registered_for_course to exist without a given_course" do
+ gc = Factory.create(:given_course)
+ str = Factory.create(:student_registered_for_course, given_course: gc)
+ gc.destroy
+ lambda{str.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it "should not be possible for a lab_group to exist without a given_course" do
+ gc = Factory.create(:given_course)
+ lg = Factory.create(:lab_group, given_course: gc)
+ gc.destroy
+ lambda{lg.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it "should not be possible for a assistant_registered_to_given_course to exist without a given_course" do
+ gc = Factory.create(:given_course)
+ argc = Factory.create(:assistant_registered_to_given_course, given_course: gc)
+ gc.destroy
+ lambda{argc.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it "should remove lab when self is destroyed" do
+ lab = create(:lab, active: true)
+ gc = create(:given_course, labs: [lab])
+ gc.destroy
+ lambda{lab.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+
describe "#register_student" do
let(:student) { create(:student) }
let(:given_course) { create(:given_course) }
@@ -55,4 +85,5 @@
given_course.should have(1).students
end
end
-end
+end
+
@@ -51,6 +51,16 @@
list2.should eq(list1)
end
end
+
+
+ describe "dependent destroy" do
+ it "should not be possible for a lab_has_group to exist without a lab_group" do
+ lg = Factory.create(:lab_group)
+ lhg = Factory.create(:lab_has_group, lab_group: lg)
+ lg.destroy
+ lambda{lhg.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
describe "adding a student" do
let(:group) { create(:lab_group) }
@@ -64,4 +74,4 @@
group.should have(1).students
end
end
-end
+end
Oops, something went wrong.