Skip to content

Commit

Permalink
added profile; use random on desk selection for quick reservations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Bartels committed Dec 19, 2023
1 parent 3cb9b2e commit 22bcdf4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ If you like to keep your DB contents after a restart, remove the comments for vo
- (personal api token)
- omniauthable
- mails
- tests
29 changes: 29 additions & 0 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class ProfilesController < ApplicationController
before_action :set_profile

# GET /profile
def profile
end

# PATCH/PUT /profile
def update
if @profile.update(profile_params)
redirect_to profile_path, notice: "Your profile was successfully updated."
else
render :edit
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = current_user
end

# Only allow a trusted parameter "white list" through.
def profile_params
params.require(:user).permit(:page, :first_name, :middle_name, :last_name, :tenant_id)
end
end
2 changes: 1 addition & 1 deletion app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def create
# 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 = Desk.includes(room: { floor: { location: :tenant } }).all.order("RANDOM()").load_async.to_a
available_desks.delete_if { |desk| desk.room.floor.location.tenant.id != current_user.tenant.id }

if available_desks.size > 0
Expand Down
14 changes: 14 additions & 0 deletions app/views/profiles/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
= simple_form_for(@profile, as: :user, method: :patch, url: profile_path) do |f|
.row
.col-md-12

= f.input :email, disabled: true, hint: 'You cannot change your email.'
= f.input :first_name
= f.input :middle_name
= f.input :last_name
= f.input :tenant_id, collection: Tenant.all, label_method: :name, value_method: :id

= f.input :current_position, collection: [:trainee, :apprentice, :employee, :lead, :management], disabled: true, hint: 'You cannot change your position. Please ask an admin if this is wrong.'
= f.input :quota_max_reservations, disabled: true, hint: 'You can create as many reservations as this.'

= f.button :submit, class: 'btn btn-success'
4 changes: 4 additions & 0 deletions app/views/profiles/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%h2
Profile

= render 'form', profile: @profile
3 changes: 2 additions & 1 deletion app/views/shared/_navbar.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
%ul.navbar-nav.px-3.full-width
- if user_signed_in?
%li.nav-item.text-nowrap
= link_to "#{current_user.first_name} #{current_user.last_name} (#{current_user.email}) logout", destroy_user_session_path, method: :delete, class: 'nav-link float-right'
= link_to "- logout", destroy_user_session_path, method: :delete, class: 'nav-link float-right'
= link_to "#{current_user.first_name} #{current_user.last_name} (#{current_user.email}) -", profile_path, class: 'nav-link float-right'
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
resources :limitations, except: :show
resources :reservations, except: :show

get "profile" => "profiles#show"
patch "profile" => "profiles#update"
put "profile" => "profiles#update"

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check
Expand Down

0 comments on commit 22bcdf4

Please sign in to comment.