Skip to content

Commit

Permalink
[#805 state:resolved] delete student and user group assignments when …
Browse files Browse the repository at this point in the history
…group is deleted
  • Loading branch information
vegantech committed Sep 5, 2014
1 parent fb86edf commit 31a8837
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Group < ActiveRecord::Base
include Pageable
belongs_to :school
has_and_belongs_to_many :students
has_many :user_group_assignments
has_many :user_group_assignments, :dependent => :destroy
has_many :users, :through=>:user_group_assignments
validates_presence_of :title, :school_id
validates_uniqueness_of :title, :scope=>:school_id
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class User < ActiveRecord::Base
has_many :school_team_memberships
has_many :school_teams, :through => :school_team_memberships
has_many :team_consultations,:foreign_key => :requestor_id
has_many :personal_groups
has_many :personal_groups, :dependent => :destroy
has_many :staff_assignments
has_many :checklists
has_many :consultation_forms
Expand Down
20 changes: 17 additions & 3 deletions lib/csv_importer/groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Groups < CSVImporter::Base
:district_school_id =>"Key for school",
:name =>"The name of the group that will appear in SIMS."
}


class << self
def description
"Named collections of students within a school. It could be classroom sections of students, neighborhoods, teams. Students and Users get assigned to them."
Expand Down Expand Up @@ -91,14 +93,25 @@ def update
end

def delete
query ="
delete from g using groups g
query1 ="
delete from gs, uga using groups g
left outer join #{temporary_table_name} tg
on tg.district_group_id = g.district_group_id
inner join schools sch on g.school_id = sch.id and sch.district_id= #{@district.id}
left outer join user_group_assignments uga on uga.group_id = g.id
left outer join groups_students gs on gs.group_id = g.id
where tg.district_school_id is null and sch.district_school_id is not null and g.district_group_id != ''
"
ActiveRecord::Base.connection.update query

query2 ="
delete from g using groups g
left outer join #{temporary_table_name} tg
on tg.district_group_id = g.district_group_id
inner join schools sch on g.school_id = sch.id and sch.district_id= #{@district.id}
where tg.district_school_id is null and sch.district_school_id is not null and g.district_group_id != ''
"
ActiveRecord::Base.connection.update query1
ActiveRecord::Base.connection.update query2
end

def insert
Expand All @@ -114,6 +127,7 @@ def insert
)
ActiveRecord::Base.connection.update query
end

end
end

18 changes: 9 additions & 9 deletions spec/lib/csv_importer/groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
District.delete_all


@district = Factory(:district)
@school_with_link = Factory(:school, :district_school_id => '2', :district_id => @district.id)
@another_linked_school = Factory(:school, :district_school_id => '1', :district_id => @district.id)
@school_no_link = Factory(:school, :district_id => @district.id)
@manual_group = Factory(:group, :school_id => @school_with_link.id, :title => "Manual group at linked school")
@manual_group2 = Factory(:group, :school_id => @school_no_link.id, :title => "Manual group at unlinked school")
@existing_group = Factory(:group, :school_id => @school_no_link.id, :title => "Existing Group", :district_group_id => 'existing_group')
@existing_group2 = Factory(:group, :school_id => @school_with_link.id, :title => "Existing Group fix name", :district_group_id => 'existing_group')
@group_to_remove = Factory(:group, :school_id => @school_with_link.id, :title => "Group to Remove", :district_group_id => 'Remove Me')
@district = FactoryGirl.create(:district)
@school_with_link = FactoryGirl.create(:school, :district_school_id => '2', :district_id => @district.id)
@another_linked_school = FactoryGirl.create(:school, :district_school_id => '1', :district_id => @district.id)
@school_no_link = FactoryGirl.create(:school, :district_id => @district.id)
@manual_group = FactoryGirl.create(:group, :school_id => @school_with_link.id, :title => "Manual group at linked school")
@manual_group2 = FactoryGirl.create(:group, :school_id => @school_no_link.id, :title => "Manual group at unlinked school")
@existing_group = FactoryGirl.create(:group, :school_id => @school_no_link.id, :title => "Existing Group", :district_group_id => 'existing_group')
@existing_group2 = FactoryGirl.create(:group, :school_id => @school_with_link.id, :title => "Existing Group fix name", :district_group_id => 'existing_group')
@group_to_remove = FactoryGirl.create(:group, :school_id => @school_with_link.id, :title => "Group to Remove", :district_group_id => 'Remove Me')
@school_no_link.groups.collect(&:title).to_set.should == ["Manual group at unlinked school", "Existing Group"].to_set

@school_no_link.groups.reload.collect(&:title).to_set.should == ["Manual group at unlinked school", "Existing Group"].to_set
Expand Down

0 comments on commit 31a8837

Please sign in to comment.