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 @@ <%= "#{rental.locker_type.short_form}##{rental.locker_specifier}" %> <%= link_to rental.rented_by.username, user_path(rental.rented_by) %> - <%= rental.state.humanize %> + <%= tag.i class: "fa #{rental_state_icon(rental.state)}" %> <%= rental.state.humanize %> <%= rental.updated_at&.to_date %> <%= rental.owned_until&.to_date || 'Unapproved yet' %> <%= button_to 'End rental', - locker_rental_path(rental, params: { locker_rental: { id: rental.id, state: :cancelled } }), + locker_rental_path(rental), data: { confirm: 'Are you sure you want to cancel this locker rental?' }, + params: { locker_rental: { state: :cancelled } }, method: :put, class: 'btn btn-danger' %>