Skip to content

Commit 7e32ad0

Browse files
committed
add coaches to events
1 parent 9b1ead1 commit 7e32ad0

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

app/controllers/admin/groups_controller.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ def index
88
end
99

1010
def generate
11+
# This needs to be wrapped in a transaction
1112
@attendees = @event.applications.application_selected.confirmed
1213
@attendees.each_slice(6).with_index do |group, index|
13-
event_group = EventGroup.create(event: @event, name: "Group #{index}")
14+
event_group = EventGroup.create(event: @event, name: "Group #{index + 1}")
1415
group.each do |application|
1516
event_group.applications << application
1617
end
1718
end
1819

20+
@coaches = @event.coach_applications.approved.to_a
21+
@event.event_groups.each do |event_group|
22+
# Check if we are assinging the last 2 coaches
23+
coach_group = @coaches.pop(2)
24+
event_group.coach_applications << coach_group unless coach_group.empty?
25+
end
26+
1927
@event_groups = @event.event_groups
2028

2129
redirect_to admin_event_groups_path(@event), notice: "Groups successfully generated"

app/models/coach_application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class CoachApplication < ApplicationRecord
55
accepts_nested_attributes_for :event
66

77
scope :to_contact, -> { where(contacted_at: nil, state: :approved) }
8+
scope :approved, ->{ where(state: :approved) }
89

910
enum state: { pending: 0, approved: 1, rejected: 2, cancelled: 3 }
1011

app/models/event_group.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class EventGroup < ApplicationRecord
22
belongs_to :event
3-
has_and_belongs_to_many :coaches
3+
has_and_belongs_to_many :coach_applications, join_table: "event_groups_coach_applications"
44
has_and_belongs_to_many :applications, join_table: "event_groups_applications"
55
end

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<% if @event.has_groups? %>
2-
has Groups
32

4-
<% @event_groups.each do |event_group| %>
5-
<h2><%= event_group.name %></h2>
6-
<table>
7-
<thead>
8-
<tr>
9-
<th>Attendees</th>
10-
<th>Coaches</th>
11-
</tr>
12-
</thead>
13-
<tbody>
14-
<tr>
15-
<td>
16-
<%= event_group.applications.map(&:name).join(", ") %>
17-
</td>
18-
<td>
19-
</td>
20-
</tr>
21-
</tbody>
22-
</table>
23-
<%end %>
3+
<% @event_groups.each do |event_group| %>
4+
<h2><%= event_group.name %></h2>
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Attendees</th>
9+
<th>Coaches</th>
10+
</tr>
11+
</thead>
12+
<tbody>
13+
<tr>
14+
<td>
15+
<%= event_group.applications.map(&:name).join(", ") %>
16+
</td>
17+
<td>
18+
19+
</td>
20+
</tr>
21+
</tbody>
22+
</table>
23+
<%end %>
2424

2525
<% else %>
2626
<%= button_to "Generate groups", generate_admin_event_groups_path(@event), method: :post %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ChangeEventGroupsCoachesToEventGroupsCoachApplications < ActiveRecord::Migration[5.2]
2+
def change
3+
rename_table :event_groups_coaches, :event_groups_coach_applications
4+
end
5+
end

db/schema.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_04_23_182618) do
13+
ActiveRecord::Schema.define(version: 2020_04_29_185626) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -88,13 +88,13 @@
8888
t.index ["event_group_id"], name: "index_event_groups_applications_on_event_group_id"
8989
end
9090

91-
create_table "event_groups_coaches", force: :cascade do |t|
91+
create_table "event_groups_coach_applications", force: :cascade do |t|
9292
t.bigint "coach_application_id"
9393
t.bigint "event_group_id"
9494
t.datetime "created_at", null: false
9595
t.datetime "updated_at", null: false
96-
t.index ["coach_application_id"], name: "index_event_groups_coaches_on_coach_application_id"
97-
t.index ["event_group_id"], name: "index_event_groups_coaches_on_event_group_id"
96+
t.index ["coach_application_id"], name: "index_event_groups_coach_applications_on_coach_application_id"
97+
t.index ["event_group_id"], name: "index_event_groups_coach_applications_on_event_group_id"
9898
end
9999

100100
create_table "events", id: :serial, force: :cascade do |t|
@@ -152,6 +152,6 @@
152152
add_foreign_key "event_groups", "events"
153153
add_foreign_key "event_groups_applications", "applications"
154154
add_foreign_key "event_groups_applications", "event_groups"
155-
add_foreign_key "event_groups_coaches", "coach_applications"
156-
add_foreign_key "event_groups_coaches", "event_groups"
155+
add_foreign_key "event_groups_coach_applications", "coach_applications"
156+
add_foreign_key "event_groups_coach_applications", "event_groups"
157157
end

0 commit comments

Comments
 (0)