Skip to content

Commit

Permalink
Quick reservation feature; began implementing room draw;
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Bartels committed Dec 19, 2023
1 parent 1b4d336 commit 3cb9b2e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
28 changes: 26 additions & 2 deletions app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index
if current_user.admin?
@reservations = Reservation.includes([:desk, :user]).all.order("start_date ASC").page(@page)
else
@reservations = Reservation.limit_user(current_user.id).order("start_date ASC").page(@page)
@reservations = Reservation.limit_user(current_user.id).includes([:desk, :user]).order("start_date ASC").page(@page)
end
respond_to do |format|
format.html
Expand All @@ -36,12 +36,36 @@ def create
return
end

# quick reservation
if patched_params["desk_id"].nil?
# the .to_a is important, as we do not want to call .delete on the collection of Desks
available_desks = Desk.includes(room: { floor: { location: :tenant } }).all.load_async.to_a
available_desks.delete_if { |desk| desk.room.floor.location.tenant.id != current_user.tenant.id }

if available_desks.size > 0
available_desks.each do |desk|
# try to save
patched_params["desk_id"] = desk.id
@reservation = Reservation.new(patched_params)
if @reservation.save
redirect_to root_path, notice: "Created new quick reservation."
return
end
end
# we have tried every desk, and found none saving successfully
redirect_to root_path, alert: "Could not find a suitable desk for quick reservation. Please select manually."
return
else
redirect_to root_path, alert: "Could not find a suitable desk for quick reservation. Please select manually."
return
end
end

@reservation = Reservation.new(patched_params)

if @reservation.save
redirect_to reservations_path, notice: "Reservation was successfully created."
else
puts reservation_params
render :new
end
end
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

class RoomsController < ApplicationController
before_action :set_room, only: [:edit, :update, :destroy]
before_action :access_granted
before_action :set_room, only: [:show, :edit, :update, :destroy]
before_action :access_granted, only: [:new, :edit, :create, :update, :destroy]

# GET /rooms
def index
Expand All @@ -13,6 +13,11 @@ def index
end
end

# GET /rooms/1
def show
puts @room
end

# GET /rooms/new
def new
@room = Room.new
Expand Down
13 changes: 13 additions & 0 deletions app/views/application/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@


.d-flex.flex-column.bd-hightlight.mb-3.w-50.ml-3
.d-flex.flow-row.mb-3
.d-flex.flex-column.mb-3.w-100
.card.mb-auto
.card-body
%h5.card-title
Quick reservation
%i.fa-solid.fa-circle-info
%p Choose a date you would like to reserve a desk on. It will pick a random free desk from your tenant.
= simple_form_for Reservation.new do |f|
= f.input :start_date, as: :datetime, html5: true
= f.input :user_id, as: :hidden, input_html: { value: @current_user.id }
= f.button :submit, class: 'btn btn-success'

.d-flex.flow-row.mb-3
.d-flex.flex-column.mb-3.w-100
.card.mb-auto
Expand Down
2 changes: 1 addition & 1 deletion app/views/rooms/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
%td
= room.floor.name
%td
= room.name
= link_to "#{room.name}", room_path(room)
%td
.d-flex.justify-content-end
- if @current_role == "admin"
Expand Down
4 changes: 4 additions & 0 deletions app/views/rooms/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%h2
Room
= @room.name

6 changes: 3 additions & 3 deletions app/views/shared/_sidebar.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
= link_to root_path, class: 'list-group-item list-group-item-action' do
%i.fa-solid.fa-chart-line
Dashboard
= link_to rooms_path, class: 'list-group-item list-group-item-action' do
%i.fa-solid.fa-people-roof
Rooms
= link_to reservations_path, class: 'list-group-item list-group-item-action' do
%i.fa-solid.fa-bookmark
Reservations
Expand All @@ -18,9 +21,6 @@
= link_to floors_path, class: 'list-group-item list-group-item-action' do
%i.fa-solid.fa-stairs
Floors
= link_to rooms_path, class: 'list-group-item list-group-item-action' do
%i.fa-solid.fa-people-roof
Rooms
= link_to desks_path, class: 'list-group-item list-group-item-action' do
%i.fa-solid.fa-desktop
Desks
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
resources :tenants, except: :show
resources :locations, except: :show
resources :floors, except: :show
resources :rooms, except: :show
resources :rooms
resources :desks, except: :show
resources :limitations, except: :show
resources :reservations, except: :show
Expand Down

0 comments on commit 3cb9b2e

Please sign in to comment.