Skip to content

Commit 1e096b1

Browse files
author
Ben Lerner
committed
prevent deleting sections that have registrations or reg_requests in them. Fixes CodeGrade#296, I think
1 parent 97c6c51 commit 1e096b1

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

app/models/section.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def self.inheritance_column
99
delegate :term, to: :course
1010
has_many :registration_sections
1111
has_many :registrations, through: :registration_sections
12+
has_many :reg_request_sections
13+
has_many :reg_requests, through: :reg_request_sections
1214
has_many :users, through: :registrations
1315

1416
has_many :submission_enabled_toggles
@@ -17,6 +19,28 @@ def self.inheritance_column
1719
validates :instructor, presence: true
1820
validates :meeting_time, length: { minimum: 3 }
1921

22+
before_destroy :ensure_destroyable, prepend: true
23+
24+
def ensure_destroyable
25+
prohibited = nonempty_section?
26+
if prohibited
27+
errors.add(:base, prohibited)
28+
throw :abort
29+
end
30+
end
31+
32+
def nonempty_section?
33+
reg_count = registration_sections.count
34+
reg_req_count = reg_requests.count
35+
if reg_count == 0 && reg_req_count == 0
36+
return false
37+
else
38+
reg_msg = "#{'is'.pluralize(reg_count)} #{reg_count} #{'registration'.pluralize(reg_count)}"
39+
reg_req_msg = "#{reg_req_count} registration #{'request'.pluralize(reg_req_count)}"
40+
return "Cannot delete this section: there #{reg_msg} and #{reg_req_msg} in it"
41+
end
42+
end
43+
2044
def to_s(show_instructor: true, show_time: true, show_type: true)
2145
ans = "#{self.crn}"
2246
ans = "#{ans} : #{self.instructor.last_name}" if show_instructor

app/views/courses/_section.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<%= f.select :type, Section.types.to_a.map{|t| [t[0].humanize, t[0]]} %>
1616
</td>
1717
<td>
18+
<% if f.object.nonempty_section? %>
19+
<button type="button" disabled title="<%= f.object.nonempty_section? %>" class="btn btn-danger">Remove</button>
20+
<% else %>
1821
<%= link_to_remove_association "Remove", f, class: 'btn btn-danger' %>
22+
<% end %>
1923
</td>
2024
</tr>

test/integration/request_reg_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,38 @@ class RequestRegTest < ActionDispatch::IntegrationTest
7474
assert new_section.students.include? @john
7575
end
7676

77+
test "cannot delete sections with registrations in it" do
78+
@lately = create(:user, name: "Johnny Come Lately", first_name: "Johnny", last_name: "Lately")
79+
assert_empty @lately.registrations
80+
reg_req = @cs101.reg_requests.new(role: Registration::roles[:student], user: @lately, lecture_sections: @section.crn.to_s)
81+
reg_req.save!
82+
@lately.reload
83+
assert_empty @lately.registrations
84+
85+
assert_equal [@section], reg_req.sections
86+
sign_in @fred
87+
get user_path(@lately)
88+
assert_response :success
89+
@lately.reload
90+
assert_empty @lately.registrations
91+
assert_not_nil @lately.grouped_registrations
92+
assert_equal 0, @lately.grouped_registrations["student"][:count]
93+
94+
delete accept_course_reg_request_path(@cs101, reg_req)
95+
assert_response :redirect
96+
follow_redirect!
97+
assert_response :success
98+
@lately.reload
99+
assert_not_empty @lately.registrations
100+
assert_not_nil @lately.grouped_registrations
101+
assert_equal 1, @lately.grouped_registrations["student"][:count]
102+
103+
assert_raises(ActiveRecord::RecordNotDestroyed) { @section.destroy! }
104+
assert_not @section.destroyed?
105+
@lately.reload
106+
assert_not_nil @lately.grouped_registrations
107+
end
108+
77109
test "registration via staff page after adding section" do
78110
sign_in @fred
79111
new_section = Section.new(course: @cs101,

0 commit comments

Comments
 (0)