-
Notifications
You must be signed in to change notification settings - Fork 0
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
pavitgopal
wants to merge
9
commits into
main
Choose a base branch
from
TimeSlotBlocking-Paviths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0739187
test edits
pavitgopal 2433bb6
initial commit
pavitgopal 12bf6ec
implemented propogation across tabs
pavitgopal f72dd3e
added rspec tests
pavitgopal 711dbac
rubocop tests
pavitgopal 0cb7418
Merge branch 'main' into TimeSlotBlocking-Paviths
pavitgopal c23a933
failing one cucmber test
Kingofikings441 cb25371
still need to do the overlapping test
Kingofikings441 1899a2e
cucumber finish
Kingofikings441 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.