Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Dependent destroy #120

Merged
merged 26 commits into from

3 participants

@anjonas
Owner

Känner mig klar med dependent destroy relationerna, kolla på koden och se om jag har missat någonting.

anjonas and others added some commits
@anjonas anjonas Implement dependent destroy between student and student_registered_fo…
…r_course with test #98
f055f3a
@anjonas anjonas Implement dependent destory between given_course and studen_registere…
…d_for_course with test
6985cc2
@anjonas anjonas Implement dependent destroy between given_course and lab_group with test c340fb0
@anjonas anjonas Implement dependent destroy between user and student, and test #98 f5109b8
@anjonas anjonas Implement dependent destroy between user and examiner, with test #98 e263e22
@anjonas anjonas Implement dependent destroy between user and assistant, with test #98 00218ab
@anjonas anjonas Implement dependent destroy between lab_group and lab_has_group, with…
… test #98
7b0607e
@anjonas anjonas Implement dependent destroy between lab and default_deadline, with test 40eebca
@anjonas anjonas Implement dependent destroy between lab and lab_has_group, with test #98
348a524
@anjonas anjonas Implement dependent destroy between assistant and assistant_registere…
…d_for_course with test #98
5b235af
@anjonas anjonas Implement dependent destroy between given_course and assistant_regist…
…erd_to_given_course, with test #98
3be9f43
@anjonas anjonas Implement dependent destroy between user and administrator with test #98
9552a4e
@oleander oleander Lab should be removed when given course is destroyed [#98] 17b8662
@oleander oleander Merge branch 'master' into dependent-destroy
Conflicts:
	app/models/student.rb
	app/models/user.rb
	spec/models/user_spec.rb
45b9938
@oleander oleander Fix migration error in user model f023c50
@oleander oleander Fix migration error in student model 02771ec
@oleander oleander Merge branch 'master' into dependent-destroy
Conflicts:
	app/models/lab.rb
643e19f
@oleander oleander Lab must reload before #destroy is applied, due to ActiveRecord::Base…
….inverse_of
13a9065
@anjonas anjonas Remove columns lab_id, lab_group_id from extended_deadlines and add c…
…olumn lab_has_group_id
b0ca274
@anjonas anjonas Fix tests in extended_deadlines_spec.rb not passing 8c7131c
@anjonas anjonas Implement dependent destroy between lab_has_group and extended_deadl…
…ine with test #98
1364965
@anjonas anjonas Implement dependent destroy between course and given_course with test…
… close #98
b014722
@anjonas anjonas Merge branch 'master' of github.com:water/mainline into dependent-des…
…troy

Conflicts:
	spec/models/lab_spec.rb
	spec/models/student_spec.rb
a76a172
@oleander
Owner

Kanske lite mycket jobb men har du lust att sammanfatta vilka modeller du har jobbat med?

Te.x
User => Student
Plockas en User bort så försvinner även Student

@jesjos
Owner

+1 på @oleander

@anjonas
Owner

course => given_course

lab_has_group => extended_deadline

user => administrator
user => student
user => assistant
user => examiner

assistant => assistant_registered_to_given_course

lab => lab_has_group
lab => default_deadline

lab_group => lab_has_group

given_course => assistant_registered_to_given_course
given_course => lab_group
given_course => student_registered_for_course
given_course => lab

student => student_registered_for_course

@oleander
Owner

Tycker de ser bra ut. :+1: från mig.

@jesjos
Owner

Ja, approved!

@oleander
Owner

Jag tror inte någon annan har några invändningar. Du kan merga in koden i master, @anjonas.

@jesjos
Owner

@anjonas Verkar som att vi alla är överens, du kan utföra mergen.

anjonas added some commits
@anjonas anjonas Merge branch 'master' into dependent-destroy
Conflicts:
	app/models/assistant.rb
	app/models/lab.rb
	db/schema.rb
	spec/models/given_course_spec.rb
	spec/models/lab_group_spec.rb
	spec/models/lab_has_group_spec.rb
9c35220
@anjonas anjonas Change in dependent destroy test 035a5d4
@anjonas
Owner

Sådär nu har jag mergat in master in i dependent-destroy, och när jag kör testerna så misslyckas dependent-destroy på User. Exempel:

User dependent destroy should not be possible for a student to exist without a user
     Failure/Error: user.destroy
     ActiveRecord::StatementInvalid:
       PG::Error: ERROR:  column repositories.owner_id does not exist
       LINE 1: ...ELECT "repositories".* FROM "repositories"  WHERE "repositor...
                                                                    ^
       : SELECT "repositories".* FROM "repositories"  WHERE "repositories"."owner_id" = 14 AND "repositories"."owner_type" = 'User'

De andra misslyckade testerna ger samma felmeddelande. Någon som har en ide om vad som är fel?

@oleander
Owner

@anjonas Du får ta och pusha upp din kod.

@anjonas anjonas merged commit 250e5c2 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 15, 2012
  1. @anjonas
  2. @anjonas
  3. @anjonas
  4. @anjonas
  5. @anjonas
  6. @anjonas
  7. @anjonas
Commits on Mar 16, 2012
  1. @anjonas
  2. @anjonas
  3. @anjonas
  4. @anjonas

    Implement dependent destroy between given_course and assistant_regist…

    anjonas authored
    …erd_to_given_course, with test #98
  5. @anjonas
Commits on Mar 19, 2012
  1. @oleander
  2. @oleander

    Merge branch 'master' into dependent-destroy

    oleander authored
    Conflicts:
    	app/models/student.rb
    	app/models/user.rb
    	spec/models/user_spec.rb
  3. @oleander
  4. @oleander
Commits on Mar 20, 2012
  1. @oleander

    Merge branch 'master' into dependent-destroy

    oleander authored
    Conflicts:
    	app/models/lab.rb
  2. @oleander
Commits on Mar 21, 2012
  1. @anjonas
  2. @anjonas
  3. @anjonas
  4. @anjonas
  5. @anjonas

    Merge branch 'master' of github.com:water/mainline into dependent-des…

    anjonas authored
    …troy
    
    Conflicts:
    	spec/models/lab_spec.rb
    	spec/models/student_spec.rb
Commits on Mar 28, 2012
  1. @anjonas

    Merge branch 'master' into dependent-destroy

    anjonas authored
    Conflicts:
    	app/models/assistant.rb
    	app/models/lab.rb
    	db/schema.rb
    	spec/models/given_course_spec.rb
    	spec/models/lab_group_spec.rb
    	spec/models/lab_has_group_spec.rb
  2. @anjonas
Commits on Mar 29, 2012
  1. @anjonas
This page is out of date. Refresh to see the latest.
View
8 app/models/assistant.rb
@@ -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  app/models/course.rb
@@ -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
View
7 app/models/extended_deadline.rb
@@ -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
View
8 app/models/given_course.rb
@@ -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
8 app/models/lab.rb
@@ -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
2  app/models/lab_group.rb
@@ -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
View
1  app/models/lab_has_group.rb
@@ -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
2  app/models/student.rb
@@ -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
15 app/models/user.rb
@@ -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
View
11 db/migrate/20120321152700_remove_lab_id_and_lab_group_id_from_extended_deadline.rb
@@ -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
View
9 db/migrate/20120321153606_add_lab_has_group_id_to_extended_deadlines.rb
@@ -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 db/schema.rb
@@ -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
3  spec/factories.rb
@@ -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
View
9 spec/models/assistant_spec.rb
@@ -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
View
9 spec/models/course_spec.rb
@@ -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
View
36 spec/models/extended_deadlines_spec.rb
@@ -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
View
35 spec/models/given_course_spec.rb
@@ -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
+
View
12 spec/models/lab_group_spec.rb
@@ -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
View
12 spec/models/lab_has_group_spec.rb
@@ -87,6 +87,16 @@
end
end
+
+ describe "dependent destroy" do
+ it "should not be possible for a extended_deadline to exist without a lab_has_group" do
+ lhg = Factory.create(:lab_has_group)
+ ee = Factory.create(:extended_deadline, lab_has_group: lhg)
+ lhg.destroy
+ lambda{ee.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+
describe "factories" do
it "should not raise error when group is set" do
group = Factory.create(:lab_group)
@@ -191,4 +201,4 @@
end
end
end
-end
+end
View
18 spec/models/lab_spec.rb
@@ -158,6 +158,24 @@
lab.description.should eq(lab.lab_description.description)
end
end
+
+
+ describe "dependent destroy" do
+ it "should not be possible for a lab_default_dealine to exist without a lab" do
+ lab = Factory.create(:lab)
+ ldd = Factory.create(:default_deadline, lab: lab)
+ lab.reload.destroy
+ lambda{ldd.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it "should not be possible for a lab_has_group to exist without a lab" do
+ lab = Factory.create(:lab)
+ lhg = Factory.create(:lab_has_group, lab: lab)
+ lab.destroy
+ lambda{lhg.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+
describe "add a group" do
let(:lab) { create(:lab, active: true) }
View
10 spec/models/student_spec.rb
@@ -58,6 +58,16 @@
end
end
+
+ describe "Dependent destroy" do
+ it "should not be possible for student_registered_for_course to exist without a student" do
+ student = Factory.create(:student)
+ src = Factory.create(:student_registered_for_course, student: student)
+ student.destroy
+ lambda{src.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+
describe "#register" do
it "should register to course" do
student.register!({
View
30 spec/models/user_spec.rb
@@ -40,6 +40,36 @@
create(:user).token.should_not equal(create(:user).token)
end
end
+
+ describe "dependent destroy" do
+ it "should not be possible for a student to exist without a user" do
+ user = Factory.create(:user)
+ s = Factory.create(:student, user: user)
+ user.destroy
+ lambda{s.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it "should not be possible for a examiner to exist without a user" do
+ user = Factory.create(:user)
+ exa = Factory.create(:examiner, user: user)
+ user.destroy
+ lambda{exa.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it "should not be possible for a assistant to exist without a user" do
+ user = Factory.create(:user)
+ assistant = Factory.create(:assistant, user: user)
+ user.destroy
+ lambda{assistant.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+
+ it "should not be possible for a administrator to exist without a user" do
+ user = Factory.create(:user)
+ admin = Factory.create(:administrator, user: user)
+ user.destroy
+ lambda{admin.reload}.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
describe "relations" do
it "should have a student" do
Something went wrong with that request. Please try again.