From 6792aa2570d8f36ebd0f4384db456243bb5595f5 Mon Sep 17 00:00:00 2001 From: PencilAmazing <16854231+PencilAmazing@users.noreply.github.com> Date: Sun, 22 Dec 2024 11:25:02 -0500 Subject: [PATCH] Cancel with a button, icons --- app/controllers/locker_rentals_controller.rb | 30 ++++++++++++++------ app/controllers/lockers_controller.rb | 15 ++++++++++ app/views/lockers/index.html.erb | 5 ++-- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/controllers/locker_rentals_controller.rb b/app/controllers/locker_rentals_controller.rb index 94e6cb56e..35fe1573c 100644 --- a/app/controllers/locker_rentals_controller.rb +++ b/app/controllers/locker_rentals_controller.rb @@ -37,13 +37,21 @@ def create end end + def update + @locker_rental = LockerRental.find(params[:id]) + if @locker_rental.update(locker_rental_params) + flash[:notice] = "Locker rental updated" + else + flash[:alert] = "Failed to update locker rental" + end + end + private def locker_rental_params if current_user.admin? - params - .require(:locker_rental) - .permit( + admin_params = + params.require(:locker_rental).permit( :locker_type_id, # admin can assign and approve requests :rented_by_id, @@ -51,12 +59,16 @@ def locker_rental_params :state, :owned_until ) - .reverse_merge( - rented_by_id: - User.find_by( - username: params.dig(:locker_rental, :rented_by_username) - )&.id - ) + + # FIXME replace that search with a different one, return ID instead + # If username is given (since search can do that) + rented_by_user = + User.find_by(username: params.dig(:locker_rental, :rented_by_username)) + if rented_by_user + # then convert to user id + admin_params.reverse_merge!(rented_by_id: rented_by_user.id) + end + admin_params else # people pick where they want a locker params diff --git a/app/controllers/lockers_controller.rb b/app/controllers/lockers_controller.rb index 728f2eb4e..fda2846a0 100644 --- a/app/controllers/lockers_controller.rb +++ b/app/controllers/lockers_controller.rb @@ -2,6 +2,8 @@ class LockersController < ApplicationController before_action :current_user before_action :signed_in + helper_method :rental_state_icon + def index @locker_types = LockerType.all @all_locker_rentals = LockerRental.all @@ -23,4 +25,17 @@ def create def types end + + private + + def rental_state_icon(state) + case state + when "active" + "fa-lock" + when "cancelled" + "fa-clock-o text-danger" + else + "" + end + end end diff --git a/app/views/lockers/index.html.erb b/app/views/lockers/index.html.erb index 2f0a0a165..b9ff9bdc7 100644 --- a/app/views/lockers/index.html.erb +++ b/app/views/lockers/index.html.erb @@ -57,12 +57,13 @@