Skip to content

Commit d17136a

Browse files
committed
create regenerate action
1 parent ff4b739 commit d17136a

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

app/controllers/admin/groups_controller.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,28 @@ def index
77
@event_groups = @event.event_groups
88
end
99

10+
# An action to regenerate
11+
# -> a controller method
12+
# -> button (to call the action)
13+
# -> a route
14+
# -> isolate generate behaviour in its own method
15+
1016
def generate
17+
fill_groups
18+
@event_groups = @event.event_groups
19+
redirect_to admin_event_groups_path(@event), notice: "Groups successfully generated"
20+
end
1121

22+
def regenerate
23+
@event_groups = @event.event_groups
24+
@event_groups.destroy_all
25+
fill_groups
26+
redirect_to admin_event_groups_path(@event), notice: "Groups successfully regenerated"
27+
end
28+
29+
private
30+
31+
def fill_groups
1232
# create a group for each pair of coaches
1333
@coaches = @event.coach_applications.approved.to_a
1434
@coaches.each_slice(2).with_index do |group, index|
@@ -26,14 +46,8 @@ def generate
2646
event_group.applications << attendee_group unless attendee_group.empty?
2747
end
2848
end
29-
30-
@event_groups = @event.event_groups
31-
32-
redirect_to admin_event_groups_path(@event), notice: "Groups successfully generated"
3349
end
3450

35-
private
36-
3751
def find_event
3852
@event = Event.find(params[:event_id])
3953
end

app/views/admin/groups/index.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<% if @event.has_groups? %>
22

3+
<%= button_to "Regenerate groups", regenerate_admin_event_groups_path(@event), method: :post %>
34
<% @event_groups.each do |event_group| %>
45
<h2><%= event_group.name %></h2>
56
<table>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
resources :groups, only: [:index] do
5050
collection do
5151
post :generate
52+
post :regenerate
5253
end
5354
end
5455
put :complete

0 commit comments

Comments
 (0)