Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time slot blocking paviths #44

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions app/controllers/room_bookings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
# frozen_string_literal: true

# Room Bookings Controller
class RoomBookingsController < ApplicationController
before_action :set_schedule

def index
schedule_id = params[:schedule_id]
@schedule = Schedule.find(params[:schedule_id])
@rooms = @schedule.rooms.where(is_active: true).where.not(building_code: 'ONLINE')
@tabs = TimeSlot.distinct.pluck(:day)
@active_tab = params[:active_tab] || @tabs[0]
@time_slots = TimeSlot.where(time_slots: { day: @active_tab }).to_a
@time_slots.sort_by! { |ts| Time.parse(ts.start_time) }
@time_slots = TimeSlot.where(day: @active_tab).order(:start_time)

# Fetch room bookings only for the specified schedule
@room_bookings = RoomBooking.joins(:room, :time_slot)
.where(rooms: { schedule_id: }, time_slots: { day: @active_tab })

# Organize room bookings in a hash with room_id and time_slot_id as keys
.where(rooms: { schedule_id: @schedule.id }, time_slots: { day: @active_tab })
@bookings_matrix = @room_bookings.each_with_object({}) do |booking, hash|
hash[[booking.room_id, booking.time_slot_id]] = booking
end
end

before_action :set_schedule

def toggle_availability
room_booking = RoomBooking.find_or_initialize_by(room_id: params[:room_id], time_slot_id: params[:time_slot_id])
new_status = !room_booking.is_available
room_booking.update(is_available: new_status)

overlapping_time_slots = find_overlapping_time_slots(room_booking.time_slot)
overlapping_time_slots.each do |overlapping_slot|
overlapping_booking = RoomBooking.find_or_initialize_by(room_id: params[:room_id], time_slot_id: overlapping_slot.id)
overlapping_booking.update(is_available: new_status)
end

redirect_to schedule_room_bookings_path(@schedule, active_tab: params[:active_tab])
end

private

def set_schedule
@schedule = Schedule.find(params[:schedule_id])
end

def find_overlapping_time_slots(time_slot)
relevant_days = calculate_relevant_days(time_slot.day)

TimeSlot.where(day: relevant_days)
.where('start_time < ? AND end_time > ?', time_slot.end_time, time_slot.start_time)
end

def calculate_relevant_days(current_day)
case current_day
when 'MWF'
%w[MWF MW F]
when 'MW'
%w[MWF MW]
when 'F'
%w[MWF F]
else
[current_day]
end
end
end
27 changes: 12 additions & 15 deletions app/views/room_bookings/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,29 @@
<tr>
<th>Times \ Rooms</th>
<% @rooms.each do |room| %>
<th scope="col" ><%= room.building_code %> <%= room.room_number %></th>

<th scope="col"><%= room.building_code %> <%= room.room_number %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @time_slots.each do |time_slot| %>
<% @time_slots.each do |time_slot| %>
<tr>
<th scope="row" class="table-secondary"><%= time_slot.start_time %> - <%= time_slot.end_time %></th>

<% @rooms.each do |room| %>
<th scope="row" class="table-secondary"><%= time_slot.start_time %> - <%= time_slot.end_time %></th>
<% @rooms.each do |room| %>
<% booking = @bookings_matrix[[room.id, time_slot.id]] %>

<td>
<% if booking %>
<%= booking.is_available ? 'Available' : 'Booked' %> |
<%= booking.is_lab ? 'Lab' : 'Lecture' %>
<% else %>
<% end %>
</td>
<% if booking && !booking.is_available %>
<%= button_to "U", toggle_availability_room_bookings_path, method: :post, params: { room_id: room.id, time_slot_id: time_slot.id, active_tab: @active_tab, schedule_id: @schedule.id }, class: "btn btn-sm btn-success" %>
<% else %>
<%= button_to "BL", toggle_availability_room_bookings_path, method: :post, params: { room_id: room.id, time_slot_id: time_slot.id, active_tab: @active_tab, schedule_id: @schedule.id }, class: "btn btn-sm btn-danger" %>
<% end %>
</tr>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<p class="container h1 text-secondary">No rooms added to this schedule, click on View Data to Add Rooms!</p>
<% end %>
<% end %>
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@

get '/time_slots', to: 'time_slots#index'
resources :room_bookings, only: [:index]
post 'room_bookings/toggle_availability', to: 'room_bookings#toggle_availability'
end

resources :room_bookings do
post 'toggle_availability', on: :collection
end

# Show Time Slot View
Expand Down
15 changes: 14 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 20_241_029_040_851) do
ActiveRecord::Schema[7.2].define(version: 20_241_030_180_433) do
create_table 'courses', force: :cascade do |t|
t.string 'course_number'
t.integer 'max_seats'
Expand Down Expand Up @@ -50,6 +50,16 @@
t.index ['schedule_id'], name: 'index_instructors_on_schedule_id'
end

create_table 'room_blocks', force: :cascade do |t|
t.integer 'room_id', null: false
t.integer 'time_slot_id', null: false
t.boolean 'is_blocked', default: true
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.index ['room_id'], name: 'index_room_blocks_on_room_id'
t.index ['time_slot_id'], name: 'index_room_blocks_on_time_slot_id'
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the migration for this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we decided to use room_bookings instead, because I didn't realize that we had the ability to block off times and rooms through that.

create_table 'room_bookings', force: :cascade do |t|
t.integer 'room_id', null: false
t.integer 'time_slot_id', null: false
Expand Down Expand Up @@ -100,6 +110,7 @@
t.string 'slot_type'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.string 'day_change'
end

create_table 'users', force: :cascade do |t|
Expand All @@ -117,6 +128,8 @@
add_foreign_key 'instructor_preferences', 'courses'
add_foreign_key 'instructor_preferences', 'instructors'
add_foreign_key 'instructors', 'schedules'
add_foreign_key 'room_blocks', 'rooms'
add_foreign_key 'room_blocks', 'time_slots'
add_foreign_key 'room_bookings', 'rooms'
add_foreign_key 'room_bookings', 'time_slots'
add_foreign_key 'rooms', 'schedules'
Expand Down
98 changes: 80 additions & 18 deletions spec/controllers/room_bookings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,100 @@
let!(:room_booking2) { create(:room_booking, room: room2, time_slot: time_slot2, is_available: false) }

before do
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John',
last_name: 'Doe')
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John', last_name: 'Doe')
allow(controller).to receive(:logged_in?).and_return(true)
controller.instance_variable_set(:@current_user, @user)
end

describe 'GET #index' do
context 'when room_booking already exists' do
before do
get :index, params: { schedule_id: schedule.id }
end
before do
get :index, params: { schedule_id: schedule.id }
end

it 'returns a successful response' do
expect(response).to have_http_status(:success)
end

it 'assigns @rooms' do
expect(assigns(:rooms)).to match_array([room1, room2])
end

it 'assigns @time_slots' do
expect(assigns(:time_slots)).to match_array([time_slot1, time_slot2])
end

it 'assigns @bookings_matrix with room_booking data' do
bookings_matrix = assigns(:bookings_matrix)
expect(bookings_matrix[[room1.id, time_slot1.id]]).to eq(room_booking1)
expect(bookings_matrix[[room2.id, time_slot2.id]]).to eq(room_booking2)
end

it 'renders the index template' do
expect(response).to render_template(:index)
end
end

it 'returns a successful response' do
expect(response).to have_http_status(:success)
describe 'POST #toggle_availability' do
context 'when booking is currently available' do
it 'toggles availability to false' do
expect do
post :toggle_availability, params: { room_id: room1.id, time_slot_id: time_slot1.id, schedule_id: schedule.id }
room_booking1.reload
end.to change { room_booking1.is_available }.from(true).to(false)

expect(response).to redirect_to(schedule_room_bookings_path(schedule, active_tab: nil))
end

it 'assigns @rooms' do
expect(assigns(:rooms)).to match_array([room1, room2])
it 'toggles availability for overlapping bookings' do
overlapping_slot = create(:time_slot, day: 'Monday', start_time: '09:30', end_time: '10:30')
create(:room_booking, room: room1, time_slot: overlapping_slot, is_available: true)

expect do
post :toggle_availability, params: { room_id: room1.id, time_slot_id: time_slot1.id, schedule_id: schedule.id }
end.to change { RoomBooking.find_by(room: room1, time_slot: overlapping_slot).is_available }.from(true).to(false)

overlapping_booking = RoomBooking.find_by(room: room1, time_slot: overlapping_slot)
expect(overlapping_booking.is_available).to eq(false)

expect(response).to redirect_to(schedule_room_bookings_path(schedule, active_tab: nil))
end
end

context 'when booking is currently blocked' do
before { room_booking1.update(is_available: false) }

it 'assigns @time_slots' do
expect(assigns(:time_slots)).to match_array([time_slot1, time_slot2])
it 'toggles availability to true' do
expect do
post :toggle_availability, params: { room_id: room1.id, time_slot_id: time_slot1.id, schedule_id: schedule.id }
room_booking1.reload
end.to change { room_booking1.is_available }.from(false).to(true)

expect(response).to redirect_to(schedule_room_bookings_path(schedule, active_tab: nil))
end

it 'assigns @bookings_matrix with room_booking data' do
bookings_matrix = assigns(:bookings_matrix)
expect(bookings_matrix[[room1.id, time_slot1.id]]).to eq(room_booking1)
expect(bookings_matrix[[room2.id, time_slot2.id]]).to eq(room_booking2)
it 'toggles availability for overlapping bookings' do
overlapping_slot = create(:time_slot, day: 'Monday', start_time: '09:30', end_time: '10:30')
create(:room_booking, room: room1, time_slot: overlapping_slot, is_available: false)

expect do
post :toggle_availability, params: { room_id: room1.id, time_slot_id: time_slot1.id, schedule_id: schedule.id }
end.to change { RoomBooking.find_by(room: room1, time_slot: overlapping_slot).is_available }.from(false).to(true)

overlapping_booking = RoomBooking.find_by(room: room1, time_slot: overlapping_slot)
expect(overlapping_booking.is_available).to eq(true)

expect(response).to redirect_to(schedule_room_bookings_path(schedule, active_tab: nil))
end
end
end

it 'renders the index template' do
expect(response).to render_template(:index)
describe 'private methods' do
describe '#calculate_relevant_days' do
it 'returns the correct relevant days' do
expect(controller.send(:calculate_relevant_days, 'MWF')).to match_array(%w[MWF MW F])
expect(controller.send(:calculate_relevant_days, 'MW')).to match_array(%w[MWF MW])
expect(controller.send(:calculate_relevant_days, 'F')).to match_array(%w[MWF F])
expect(controller.send(:calculate_relevant_days, 'TR')).to match_array(['TR'])
end
end
end
Expand Down