diff --git a/README.md b/README.md index 8fca4f8..01bbc81 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb new file mode 100644 index 0000000..cb4d641 --- /dev/null +++ b/app/controllers/profiles_controller.rb @@ -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 diff --git a/app/controllers/reservations_controller.rb b/app/controllers/reservations_controller.rb index 5685079..109f757 100644 --- a/app/controllers/reservations_controller.rb +++ b/app/controllers/reservations_controller.rb @@ -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 diff --git a/app/views/profiles/_form.html.haml b/app/views/profiles/_form.html.haml new file mode 100644 index 0000000..1782c4c --- /dev/null +++ b/app/views/profiles/_form.html.haml @@ -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' diff --git a/app/views/profiles/show.html.haml b/app/views/profiles/show.html.haml new file mode 100644 index 0000000..7e8453d --- /dev/null +++ b/app/views/profiles/show.html.haml @@ -0,0 +1,4 @@ +%h2 + Profile + += render 'form', profile: @profile diff --git a/app/views/shared/_navbar.html.haml b/app/views/shared/_navbar.html.haml index 9e4fbd8..0a50dd9 100644 --- a/app/views/shared/_navbar.html.haml +++ b/app/views/shared/_navbar.html.haml @@ -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' diff --git a/config/routes.rb b/config/routes.rb index 4c0f6ad..1eca377 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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